Last active
December 15, 2015 19:49
-
-
Save nathanlws/5314640 to your computer and use it in GitHub Desktop.
Akiban REST with Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"host": "<host>", | |
"port": "<port>", | |
"user": "<user>", | |
"pass": "<pass>" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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