Python calls restful:http://blog.akiban.com/get-restful-with-python/
This article is to refer to the English to do a test, follow-up to add translation.
This post would take a look at our REST API by creating a Python script to interact with the entity endpoint.
One of the key benefits of using standard technologies, formats, and protocols are the incredible amount of existing Toolin G that's get for free. Since we ' ve been busy building our new service, backed by our ever changing database, we haven ' t gotten around to client L Ibraries yet.
However, we do speak JSON through REST over HTTPS. Every language already have battle hardened libraries for working with those. For Python, it doesn ' t get much better than the requests library. Their motto is an HTTP for humans and it makes working with RESTful services a breeze.
Demand:
As in Schemaless SQL, we'll be using a dataspace with a single entity named Hopes.
On the service side, just copy and deploy the hopes DataSpace and make note of the credentials.
On the client side, we just need to install the requests library.
$ pip Install requests
Hello, World
We'll be using a external JSON configuration file, named Config.json, for simplicity. Create the JSON files as below with real values from the deployments page.
{ "Host":"Host", "Port":"Port", "User":"User", "Pass":"Pass"}
The simplest endpoint exposed by the Akiban Server is version. It has no dependencies and would always work if valid credentials is supplied.
We ' ll start our script, named akiban_rest.py, with the code below to load our configuration and execute a version request.
ImportJSONImportrequests 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'])) PrintResp.text
Running that would give output like below, though the specific server_version may vary.
$ python akiban_rest.py
[{"SERVER_NAME": "Akiban Server", "server_version": "1.6.1-snapshot.2606"}]
congrats! You ' re now talking to the Akiban REST API. We ' ll be wrapping the rest of the Entity Resources to provide convenient access from the command line.
Helpers
A few helpers is in order. One for building the URI string, one for pretty-printing we JSON response, and a variable for holding the configured cred Entials.
defURL (endpoint):return 'https://%s:%s/v1%s'% (config['Host'], config['Port'], endpoint)defDump (response):PrintJson.dumps (Json.loads (response.text), indent=2) AUTH= (config['User'], config['Pass'])
Commands and Arguments
The overall script architecture is quite simple. A single function per endpoint we ' re exposing, a maps to name it, and very simple command line argument handling.
Note that a real library would want a number of things that has been omitted for brevity, such as argument checking and s Pecific error messages.
defversion ():returnRequests.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) We can now run the version command.$ python akiban_rest.py version[{"server_version":"1.6.1-snapshot.2606", "server_name":"Akiban Server" }]
GET and POST
Now we can begin interacting with our hopes entity. New methods for retrieving all, or specific, instances with GET and creating a new instance with POST.
defEntity_get_all (Entity):returnRequests.get (URL ('/entity/%s'% entity), auth=AUTH)defEntity_get (Entity, IDS):returnRequests.get (URL ('/entity/%s/%s'% (entity, IDS)), auth=AUTH)defEntity_post (Entity, JSON):returnRequests.post (URL ('/entity/%s'% entity), Data=json, auth=AUTH) Commands= { 'version': Version,' All': Entity_get_all,'Get': Entity_get,'Create': Entity_post}
The Create, get, and Get_all commands now function.
$ python akiban_rest.py Create hopes'{"desc": "Part iv:a New Hope", "date": "2013-04-04 16:58:30", "Bumpcount": 0}'{ "ID": 1}$ python akiban_rest.py create hopes'{"desc": "Another", "Date": "2013-04-04 16:58:35", "Bumpcount": 0}'{ "ID": 2}$ python akiban_rest.py get hopes1[ { "Date":"2013-04-04 16:58:30", "Bumpcount": 0,"ID": 1, "desc":"Part iv:a New Hope"}]$ python akiban_rest.py all hopes[{"Date":"2013-04-04 16:58:30", "Bumpcount": 0,"ID": 1, "desc":"Part iv:a New Hope" }, { "Date":"2013-04-04 16:58:35", "Bumpcount": 0,"ID": 2, "desc":"another" }]
PUT and DELETE
We can now finish off the entity endpoint by adding commands for replacing and deleting. Note that the dump had been tweaked to handle no_content responses, like delete.
defDump (response):ifResponse.status_code! =requests.codes.no_content:PrintJson.dumps (Json.loads (response.text), indent=2) defEntity_put (Entity, IDS, JSON):returnRequests.put (URL ('/entity/%s/%s'% (entity, IDS)), Data=json, auth=AUTH)defEntity_delete (Entity, IDS):returnRequests.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}
and to demonstrate.
$ python akiban_rest.py replace hopes 2'{"id": 2, "desc": "A Better name", "date": "2013-04-04 16:58:35", "Bumpcount":'{ "ID": 2}$ python akiban_rest.py Delete hopes1$ python akiban_rest.py all hopes[{"Date":"2013-04-04 16:58:35", "Bumpcount": 100, "ID": 2, "desc":"A Better name" }]
Summary
We started an empty dataspace and a blank akiban_rest.py file. After loading our example hope and deploying it, we could start-to-interact with it through a simple REST call. Fifty lines of Python later and we now has a respectable command line script for viewing, creating, updating, and Deletin G our entities.
All of the code from this post is available in this Gist. Who knows, this might just grow to our Python client library!
Android Call restful:http://bbs.it-home.org/thread-10447-1-1.html
GET RESTful with Python