Skip to content

Instantly share code, notes, and snippets.

@timrichardson
Created April 30, 2015 11:24
Show Gist options
  • Save timrichardson/06532a9898cc44f89423 to your computer and use it in GitHub Desktop.
Save timrichardson/06532a9898cc44f89423 to your computer and use it in GitHub Desktop.
Some examples of Unleashed API in Python 3.4
__author__ = 'tim'
import requests.auth
import binascii
import hashlib
import hmac
import prefs
import json
# tested only with python 3.4
# the prefs file has the API key and ID
# this shows fetching and updating a customer in Unleashed
# I claimed I could do this in 30 minutes but it took a bit longer. The API documentation is not all it could be
# the Sandbox is very helpful. IN JSON mode post your JSON into the field which says XML.
# based on code copyright
# The MIT License (MIT)
# Copyright (c) 2013 Jonathan Sokolowski
# https://github.com/jsok/unleashed
# this is licenced under the same terms
# Copyright (c) Tim Richardson tim@growthpath.com.au
class UnleashedAPI(requests.auth.AuthBase):
def __init__(self):
self.api_key = prefs.api_key.encode('utf-8')
self.api_id = prefs.api_id
self.api_url = 'https://api.unleashedsoftware.com'
def get_query(self, url):
parts = url.split('?')
if len(parts) > 1:
return parts[1]
else:
return ""
def __call__(self, r):
query = self.get_query(r.url) #we encode only the query part
hashed = hmac.new(self.api_key, query.encode('utf-8'), hashlib.sha256)
signature = binascii.b2a_base64(hashed.digest())[:-1]
r.headers['api-auth-signature'] = signature
r.headers['api-auth-id'] = self.api_id
return r
def _get_request(self, method, params=None):
params = params or {}
headers = {
'content-type': 'application/json',
'accept': 'application/json',
}
resp = requests.get(
self.api_url + '/' + method,
headers=headers,
params=params,
auth=self
)
return resp
def _post_request(self, method, data):
headers = {
'content-type': 'application/json',
'accept': 'application/json',
}
resp = requests.post(
self.api_url + '/' + method,
data,
headers=headers,
auth=self
)
return resp
def get_a_customer(self,cust_id):
#return JSON
resp = self._get_request('Customers',params=dict(customerCode=cust_id))
json_parsed = resp.json()
return json_parsed['Items'][0]
def update_a_customer(self,data):
#despite docs, we must always provide CustomerName, Guid,CustomerCode (according to their Sandbox)
resp = self._post_request('Customers/{id}'.format(id=data['Guid']),json.dumps(data))
return resp
# json which works from sandbox
# {"StopCredit": false,"CustomerName":"Ace Outlets","Guid":"68754646-f5b7-466c-9838-01bca7ff3e4c","CustomerCode":"ACE001"}
if __name__ == '__main__':
unleashedAPI = UnleashedAPI()
cust = unleashedAPI.get_a_customer('ACE001')
data = {}
data['Guid'] = cust['Guid']
data['CustomerCode'] = cust['CustomerCode']
data['CustomerName'] = cust['CustomerName']
data['StopCredit'] = True
resp = unleashedAPI.update_a_customer(data)
print(resp)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment