diff --git a/README.md b/README.md index 6967031..0190e3b 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,8 @@ # spektrixpython -A Python module for interacting with Spektrix API v3. This module is written +A Python 3 module for interacting with Spektrix API v3. This module is written by a third party and is not supported by or affiliated with Spektrix. -For API v3 documentation, visit `https://system.spektrix.com//api/help`, +For API v3 documentation, visit `https://system.spektrix.com//api/v3/help`, replacing `` with your Spektrix system name. ## Usage @@ -14,18 +14,19 @@ replacing `` with your Spektrix system name. ### Example ```python -from spektrixpython import SpektrixRequest +from spektrixpython import SpektrixCredentials, SpektrixRequest -clientname = 'SPEKTRIX CLIENT NAME' -username = 'API USERNAME' -key = 'API KEY' + +credentials = SpektrixCredentials(client_name="SPEKTRIX CLIENT NAME", + api_user="API USERNAME", + api_key="API KEY") # Get a list of events -events = SpektrixRequest('events', clientname, username, key).get() -print events +events = SpektrixRequest(endpoint='events', credentials=credentials).get() +print(events) # Create a new basket -basket = SpektrixRequest('baskets', clientname, username, key).post()['id'] +basket = SpektrixRequest(endpoint='baskets', credentials=credentials).post()['id'] # Add a merchandise item to the newly created basket endpoint = 'baskets/{}/merchandise'.format(basket) @@ -33,7 +34,7 @@ endpoint = 'baskets/{}/merchandise'.format(basket) # Replace this stockItem Id with one from your Spektrix system. payload = {'stockItem':'1AHGJDSMMPLMPPGNLJBQVLBRSKVDLQRPP'} -SpektrixRequest(endpoint, clientname, username, key).post(payload) +SpektrixRequest(endpoint=endpoint, credentials=credentials).post(payload) ``` ## License diff --git a/setup.py b/setup.py index bfec545..e6b3d25 100644 --- a/setup.py +++ b/setup.py @@ -3,12 +3,12 @@ name = 'spektrixpython', packages = ['spektrixpython'], install_requires = ['requests'], - version = '0.2.0', + version = '0.3.0', description = 'A python module for interacting with Spektrix API v3.', author = 'Hugh Topping', author_email = 'hugh@hugh.io', url = 'https://github.com/hughtopping/spektrixpython', - download_url = 'https://github.com/hughtopping/spektrixpython/tarball/0.2.0', + download_url = 'https://github.com/hughtopping/spektrixpython/tarball/0.3.0', keywords = ['spektrix','api','v3'], license='MIT', classifiers = [] diff --git a/spektrixpython/__init__.py b/spektrixpython/__init__.py index eef8dfa..4945e67 100644 --- a/spektrixpython/__init__.py +++ b/spektrixpython/__init__.py @@ -1 +1 @@ -from spektrixpython import SpektrixRequest \ No newline at end of file +from .spektrixpython import SpektrixCredentials, SpektrixRequest \ No newline at end of file diff --git a/spektrixpython/spektrixpython.py b/spektrixpython/spektrixpython.py index 28fbfc9..da1b3d3 100644 --- a/spektrixpython/spektrixpython.py +++ b/spektrixpython/spektrixpython.py @@ -6,13 +6,20 @@ from base64 import b64encode, b64decode +class SpektrixCredentials(object): + def __init__(self, client_name, api_user, api_key): + self.client_name = client_name + self.api_user = api_user + self.api_key = api_key + + class SpektrixRequest(object): - def __init__(self, endpoint, spektrix_system, spektrix_api_user, spektrix_api_key): + def __init__(self, endpoint, credentials): - self.spektrix_api_user = spektrix_api_user - self.spektrix_api_key = spektrix_api_key + self.spektrix_api_user = credentials.api_user + self.spektrix_api_key = credentials.api_key - base_url = 'https://system.spektrix.com/' + spektrix_system + '/api/v3/' + base_url = 'https://system.spektrix.com/' + credentials.client_name + '/api/v3/' self.url = base_url + endpoint def get(self): @@ -56,7 +63,7 @@ def _generate_auth_headers(self, payload=None): string_to_sign = string_to_sign.encode('utf-8') signature = hmac.new(decoded_key, string_to_sign, sha1).digest() - signature = b64encode(signature) + signature = b64encode(signature).decode("utf-8") headers = {} headers['Date'] = date @@ -83,4 +90,4 @@ def _make_request(self, payload=None): response.raise_for_status() - return response.json() \ No newline at end of file + return response.json()