Skip to content

Commit

Permalink
Upgrade to Python 3 and change the way credentials are passed in
Browse files Browse the repository at this point in the history
  • Loading branch information
hughtopping committed Mar 6, 2018
1 parent b9b16ba commit f5581f7
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 19 deletions.
21 changes: 11 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -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/<clientname>/api/help`,
For API v3 documentation, visit `https://system.spektrix.com/<clientname>/api/v3/help`,
replacing `<clientname>` with your Spektrix system name.

## Usage
Expand All @@ -14,26 +14,27 @@ replacing `<clientname>` 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)

# 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
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '[email protected]',
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 = []
Expand Down
2 changes: 1 addition & 1 deletion spektrixpython/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from spektrixpython import SpektrixRequest
from .spektrixpython import SpektrixCredentials, SpektrixRequest
19 changes: 13 additions & 6 deletions spektrixpython/spektrixpython.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand All @@ -83,4 +90,4 @@ def _make_request(self, payload=None):

response.raise_for_status()

return response.json()
return response.json()

0 comments on commit f5581f7

Please sign in to comment.