Skip to content

Instantly share code, notes, and snippets.

@nathanlws
Last active December 15, 2015 19:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nathanlws/5314640 to your computer and use it in GitHub Desktop.
Save nathanlws/5314640 to your computer and use it in GitHub Desktop.
Akiban REST with Python
{
"host": "<host>",
"port": "<port>",
"user": "<user>",
"pass": "<pass>"
}
import json
import requests
config = {}
with open('config.json') as cfg:
config = json.load(cfg)
resp = requests.get(
'https://%s:%s/v1/version' % (config['host'], config['port']),
auth = (config['user'], config['pass'])
)
print resp.text
import json
import requests
config = {}
with open('config.json') as cfg:
config = json.load(cfg)
def url(endpoint):
return 'https://%s:%s/v1%s' % (config['host'], config['port'], endpoint)
def dump(response):
print json.dumps(json.loads(response.text), indent=2)
AUTH = (config['user'], config['pass'])
dump(requests.get(url('/version'), auth=AUTH))
import sys
import json
import requests
config = {}
with open('config.json') as cfg:
config = json.load(cfg)
AUTH = (config['user'], config['pass'])
def url(endpoint):
return 'https://%s:%s/v1%s' % (config['host'], config['port'], endpoint)
def dump(response):
print json.dumps(json.loads(response.text), indent=2)
def version():
return requests.get(url('/version'), auth=AUTH)
commands = {
'version': version
}
cmd_name = sys.argv[1]
cmd_args = sys.argv[2:]
cmd = commands.get(cmd_name)
resp = cmd(*cmd_args)
dump(resp)
import sys
import json
import requests
config = {}
with open('config.json') as cfg:
config = json.load(cfg)
AUTH = (config['user'], config['pass'])
def url(endpoint):
return 'https://%s:%s/v1%s' % (config['host'], config['port'], endpoint)
def dump(response):
print json.dumps(json.loads(response.text), indent=2)
def version():
return requests.get(url('/version'), auth=AUTH)
def entity_get_all(entity):
return requests.get(url('/entity/%s' % entity), auth=AUTH)
def entity_get(entity, ids):
return requests.get(url('/entity/%s/%s' % (entity, ids)), auth=AUTH)
def entity_post(entity, json):
return requests.post(url('/entity/%s' % entity), data=json, auth=AUTH)
commands = {
'version': version,
'get_all': entity_get_all,
'get': entity_get,
'post': entity_post
}
cmd_name = sys.argv[1]
cmd_args = sys.argv[2:]
cmd = commands.get(cmd_name)
resp = cmd(*cmd_args)
dump(resp)
import sys
import json
import requests
config = {}
with open('config.json') as cfg:
config = json.load(cfg)
AUTH = (config['user'], config['pass'])
def url(endpoint):
return 'https://%s:%s/v1%s' % (config['host'], config['port'], endpoint)
def dump(response):
if response.status_code != requests.codes.no_content:
print json.dumps(json.loads(response.text), indent=2)
def version():
return requests.get(url('/version'), auth=AUTH)
def entity_get_all(entity):
return requests.get(url('/entity/%s' % entity), auth=AUTH)
def entity_get(entity, ids):
return requests.get(url('/entity/%s/%s' % (entity, ids)), auth=AUTH)
def entity_post(entity, json):
return requests.post(url('/entity/%s' % entity), data=json, auth=AUTH)
def entity_put(entity, ids, json):
return requests.put(url('/entity/%s/%s' % (entity, ids)), data=json, auth=AUTH)
def entity_delete(entity, ids):
return requests.delete(url('/entity/%s/%s' % (entity, ids)), auth=AUTH)
commands = {
'version': version,
'all': entity_get_all,
'get': entity_get,
'create': entity_post,
'replace': entity_put,
'delete': entity_delete
}
cmd_name = sys.argv[1]
cmd_args = sys.argv[2:]
cmd = commands.get(cmd_name)
resp = cmd(*cmd_args)
dump(resp)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment