Creating RESTful Web Service is so simple using Django Tastypie. It's pretty much just about registering which model you want to expose. Even more amazing is that filtering feature. Read here to get more info about it.
I decided to create Java frontend to replace the existing, which uses Python Gtk. To keep with the existing look and feel I use java-gnome binding. The transition from python gtk to java-gnome is quite smooth. Java-gnome has such a good design, so the only thing that I need to for the transition is just adapting to the java paradigm, which is wonderful.
To get the JSON object from the web service, I chose Apache HTPClient, just to make things simple. I know that there are a lot of java REST client, such as restlet, jersey, etc. I stick with the simplest one.
The next thing is to figure out how to transform JSON result from the RESTful web service to POJO. Here comes GSON to the rescue. I did not want to use python attribute name convention in my java codes. So, this one I need to solve. Fortunately, GSON comes with clever and handy feature named FieldNamingPolicy. Obviously they have really good experience in different programming language hence this feature available.
To give example on how to use it, taken from the site:
SomeObject someObject = new SomeObject("first", "second");
Gson gson = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).create();
String jsonRepresentation = gson.toJson(someObject);
System.out.println(jsonRepresentation);
Awesome! That simple and you can keep the java attribute naming convention and make your java codes look good.
Showing posts with label web service. Show all posts
Showing posts with label web service. Show all posts
Saturday, August 4, 2012
Saturday, November 26, 2011
Interacting with Django Tastypie API Using Python Librarys
If you already have some django web-app, consider extending its functionality with django-tastypie. It has pretty decent docs as well. With django-tastypie you can create RESTful Web Service. I am not going to talk about django-tastypie in this post, instead I would like to share how to interact with django-tastypie api using python's urllib and urllib2 libraries.
The following codes gives some idea about how to get data using the API, with ApiKeyAuthentication method.
import urllib, urllib2
api_url = 'http://localhost:8080/api/app1/model1/?username=apiuser1&api_key=apikey_for_apiuser1'
http_req = urllib2.Request(api_url, headers={'ACCEPT': 'application/json, text/html'})
try:
resp = urllib2.urlopen(http_req)
contents = resp.read()
print contents
except urllib2.HTTPError, error:
print he.getcode()
The codes below gives some idea about how to create data using the API, with ApiKeyAuthentication method.
import urllib, urllib2
import simplejson as json
api_url = 'http://localhost:8080/api/app1/model1/?username=apiuser1&api_key=apikey_for_apiuser1'
json_post = json.dumps({"model_name": "TEST13"})
http_req = urllib2.Request(api_url, json_post, {'Content-Type': 'application/json', 'ACCEPT': 'application/json, text/html'})
try:
response = urllib2.urlopen(http_req)
print response.read()
print response.getcode()
print response.info()
print response.msg
except urllib2.HTTPError as he:
print he.getcode()
print he.info()
print he.msg
except urllib2.URLError as ue:
print ue
Both examples using json data format. Remember to set the appropriate Content-Type and ACCEPT header data if using different data format.
If it is necessary to use https secure connection and proxy, refer to this post to get more information.
The following codes gives some idea about how to get data using the API, with ApiKeyAuthentication method.
import urllib, urllib2
api_url = 'http://localhost:8080/api/app1/model1/?username=apiuser1&api_key=apikey_for_apiuser1'
http_req = urllib2.Request(api_url, headers={'ACCEPT': 'application/json, text/html'})
try:
resp = urllib2.urlopen(http_req)
contents = resp.read()
print contents
except urllib2.HTTPError, error:
print he.getcode()
The codes below gives some idea about how to create data using the API, with ApiKeyAuthentication method.
import urllib, urllib2
import simplejson as json
api_url = 'http://localhost:8080/api/app1/model1/?username=apiuser1&api_key=apikey_for_apiuser1'
json_post = json.dumps({"model_name": "TEST13"})
http_req = urllib2.Request(api_url, json_post, {'Content-Type': 'application/json', 'ACCEPT': 'application/json, text/html'})
try:
response = urllib2.urlopen(http_req)
print response.read()
print response.getcode()
print response.info()
print response.msg
except urllib2.HTTPError as he:
print he.getcode()
print he.info()
print he.msg
except urllib2.URLError as ue:
print ue
Both examples using json data format. Remember to set the appropriate Content-Type and ACCEPT header data if using different data format.
If it is necessary to use https secure connection and proxy, refer to this post to get more information.
Subscribe to:
Posts (Atom)