Skip to content

Instantly share code, notes, and snippets.

@bigkraig
Created October 12, 2012 01:02
Show Gist options
  • Save bigkraig/3876759 to your computer and use it in GitHub Desktop.
Save bigkraig/3876759 to your computer and use it in GitHub Desktop.
#!/ usr/bin/env python
# By: Kelcey Damage, 2012
import hashlib, hmac, string, base64, urllib
class SignedAPICall(object):
def __init__(self, api_url, apiKey, secret):
self.api_url = api_url
self.apiKey = apiKey
self.secret = secret
def request(self, args):
args['apiKey'] = self.apiKey
self.params = []
self._sort_request(args)
self._create_signature()
self._build_post_request()
def _sort_request(self, args):
keys = sorted(args.keys())
for key in keys:
self.params.append(key + '=' + urllib.quote_plus(args[key]))
def _create_signature(self):
self.query = '&'.join(self.params)
digest = hmac.new(
self.secret,
msg=self.query.lower(),
digestmod=hashlib.sha1).digest()
self.signature = base64.b64encode(digest)
def _build_post_request(self):
self.query += '&signature=' + urllib.quote_plus(self.signature)
self.value = self.api_url + '?' + self.query
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment