Skip to content

Commit

Permalink
Uses POST and encodes complex arguments correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
quentinsf committed Dec 29, 2010
1 parent 46cf7bb commit 6057178
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
2 changes: 1 addition & 1 deletion __init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__all__ = ['coda','oauth']
__all__ = ['api','oauth']
19 changes: 16 additions & 3 deletions api.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,19 @@
# c = s.get_coda(atok)
# print c.getDisplays()
#
# Note that, unlike normal Python calls, if you're passing arguments you must use keyword arguments
# because the keywords get turned into parameter names. In other words:
# c.removeUser(user_uuid='xxxxxxxxx')
# will work but
# c.removeUser('xxxxxxxxx')
# will not, even though the call only takes one parameter.

CODA_SERVER_URL = 'https://api.codaview.com' # Note no trailing slashe
API_RELATIVE_URL = "external/v2/json/" # Note trailing slash

import os
import oauth
import urllib2
import urllib, urllib2
import sys

# Find a simplejson library somewhere!
Expand Down Expand Up @@ -109,15 +115,22 @@ def __init__(self, access_token_string, api_url, consumer):
def get_url(self, method, parameters={}):
oauth_request = oauth.OAuthRequest.from_consumer_and_token(self.consumer,
token=self.access_token,
http_method='POST',
http_url=self.api_url + method,
parameters=parameters)
oauth_request.sign_request(oauth.OAuthSignatureMethod_HMAC_SHA1(), self.consumer, self.access_token)
return oauth_request.to_url()

def callMethod(self, method, **args):
def callMethod(self, method, **kwargs):
if not method.endswith('/'):
method += '/'
response = urllib2.urlopen(self.get_url(method, args))
params = {}
# Encode argument values as JSON
for k in kwargs:
params[k] = json.dumps(kwargs[k])
url = self.get_url(method, params)
# print "Calling %s with params %s" % (method, params)
response = urllib2.urlopen(url, urllib.urlencode(params))
result = json.loads(response.read())
if result['result'] == 'OK':
return result.get('response', None)
Expand Down

0 comments on commit 6057178

Please sign in to comment.