Skip to content
January 14, 2011 / Steven Citron-Pousty

Example of Python with JSON RPC

Alright so my brain is really starting to get there with some of this language difference in Python over Java. I am not really close to getting to the cooler features or generators or lambda expressions but I am getting better at getting code to work. So as a PSA here is an example of using Python to interact with the Mashery API. It does some http work, some JSON RPC, some time, and some MD5 stuff. Hope you enjoy and of course would love any feedback.

 1 import json
 2 from json import JSONEncoder
 3 import time
 4 import hashlib
 5 import http.client
 6 
 7 __author__="scitronpousty"
 8 __date__ ="$Jan 11, 2011 10:18:04 AM$"
 9 
10 
11 apiKey = "wewewewewewewewsu"
12 sharedSecret = "97ewewewewewwe"
13 
14 #Not using because we are only doing read only operations
15 #sandboxAPIKey = 
16 #sandboxSharedSecret = 
17 
18 endpoint = "api.mashery.com"
19 #sandboxEndpoint = "api.sandbox.mashery.com"
20 
21 path = "/v2/json-rpc/789"
22 #sandboxPath = 
23 
24 def buildAuthParams():
25     """This function takes our API key and shared secret and uses it to create the signature that mashery wants """
26     authHash = hashlib.md5();
27     #time.time() gets the current time since the epoch (1970) with decimals seconds
28     temp = str.encode(apiKey + sharedSecret + repr(int(time.time())))
29     authHash.update(temp)        
30     return authHash.hexdigest()
31 
32 
33 
34 if __name__ == "__main__":
35     
36     #make an http connection object pointed at the endpoint
37     conn = http.client.HTTPConnection(endpoint)
38     
39     
40     #This the mashery JSON-RPC query code
41     paramsForKeys = JSONEncoder().encode({'method': "object.query", 'params': ["SELECT name, keys.apikey from applications where username = 'scitronpousty'"], 'id':1})
42     
43     #Headers required by mashery
44     headers = {"Content-type": "application/json", "Accept": "text/plain", "Content-length": repr(len(params))}
45     
46     #send the whole shebang over to mashery 
47     conn.request("POST", path + "?apikey=" + apiKey + "&sig=" + buildAuthParams() , params, headers)
48     
49     resp = conn.getresponse()    
50     
51     #response data is a binary string and I want to read it easily
52     responseObject = resp.read().decode()
53     
54     #doing a little pretty formating to get it to look nice on my output terminal
55     print(json.dumps(json.loads(responseObject), sort_keys=True, indent=4))
56     
57     #just being safe here
58     conn.close()
59     
60 
61
About these ads

One Comment

Leave a Comment
  1. dave / Oct 28 2011 12:32 pm

    I cut and pasted your html into a test editor and got this in firebug:

    $ is not defined

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 1,857 other followers

%d bloggers like this: