-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
214 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import json | ||
import responses | ||
import pytest | ||
from os import environ | ||
from datetime import datetime, timedelta | ||
|
||
from trycourier.client import Courier | ||
from trycourier.exceptions import CourierAPIException | ||
|
||
@responses.activate | ||
def test_success_get_account(): | ||
responses.add( | ||
responses.GET, | ||
'https://api.courier.com/accounts/account-001', | ||
status=200, | ||
content_type='application/json', | ||
body='{"id":"ACME", "name":"ACME Inc", "properties":{}, "default_preferences":{}, "user_profile":{}}' | ||
) | ||
|
||
c = Courier(auth_token='123456789ABCDF') | ||
r = c.accounts.get_account("account-001") | ||
|
||
assert r == { | ||
"id":"ACME", | ||
"name":"ACME Inc", | ||
"properties":{}, | ||
"default_preferences":{}, | ||
"user_profile":{} | ||
} | ||
|
||
@responses.activate | ||
def test_success_get_accounts(): | ||
responses.add( | ||
responses.GET, | ||
'https://api.courier.com/accounts', | ||
status=200, | ||
content_type='application/json', | ||
body='{"items":[{"id":"ACME", "name":"ACME Inc", "properties":{}, "default_preferences":{}, "user_profile":{}}], "has_more": false}' | ||
) | ||
|
||
c = Courier(auth_token='123456789ABCDF') | ||
r = c.accounts.get_accounts() | ||
|
||
assert r == { | ||
"items": [{ | ||
"id":"ACME", | ||
"name":"ACME Inc", | ||
"properties":{}, | ||
"default_preferences":{}, | ||
"user_profile":{} | ||
}], | ||
"has_more": False | ||
} | ||
|
||
@responses.activate | ||
def test_success_put_account(): | ||
responses.add( | ||
responses.PUT, | ||
'https://api.courier.com/accounts/account-1', | ||
status=200, | ||
content_type='application/json', | ||
body='{"id":"ACME", "name":"ACME Inc", "properties":{}, "default_preferences":{}, "user_profile":{}}' | ||
) | ||
c = Courier(auth_token='123456789ABCDF') | ||
r = c.accounts.put_account( | ||
'account-1', | ||
account={ | ||
"name":"ACME Inc", | ||
"properties":{}, | ||
"default_preferences":{}, | ||
"user_profile":{} | ||
}, | ||
) | ||
|
||
assert r == { | ||
"id":"ACME", | ||
"name":"ACME Inc", | ||
"properties":{}, | ||
"default_preferences":{}, | ||
"user_profile":{} | ||
} | ||
|
||
@responses.activate | ||
def test_success_delete_account(): | ||
responses.add( | ||
responses.DELETE, | ||
'https://api.courier.com/accounts/account-1', | ||
status=204, | ||
content_type='application/json' | ||
) | ||
|
||
c = Courier(auth_token='123456789ABCDF') | ||
c.accounts.delete_account("account-1") | ||
|
||
assert len(responses.calls) == 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
from .exceptions import CourierAPIException | ||
|
||
|
||
class Accounts(): | ||
key = "accounts" | ||
|
||
def __init__(self, base_url, session): | ||
self.base_url = base_url | ||
self.session = session | ||
|
||
@property | ||
def uri(self): | ||
return "%s/%s" % (self.base_url, self.key) | ||
|
||
def get_account(self, account_id): | ||
""" | ||
Gets an account | ||
Args: | ||
account_id: An account to get the details of | ||
Raises: | ||
CourierAPIException: Any error returned by the Courier API | ||
Returns: | ||
dict: Contains account object | ||
""" | ||
url = "%s/%s" % (self.uri, account_id) | ||
resp = self.session.get(url) | ||
|
||
if resp.status_code >= 400: | ||
raise CourierAPIException(resp) | ||
|
||
return resp.json() | ||
|
||
def get_accounts(self, limit=None, starting_after=None): | ||
""" | ||
Lists accounts | ||
Args: | ||
limit: The number of accounts to return (default 20, max 100) | ||
starting_after: Value of next_page from previous response | ||
Raises: | ||
CourierAPIException: Any error returned by the Courier API | ||
Returns: | ||
dict: Contains accounts items and paging info | ||
""" | ||
|
||
params = {} | ||
|
||
if limit: | ||
params['limit'] = limit | ||
|
||
if starting_after: | ||
params['starting_after'] = starting_after | ||
|
||
resp = self.session.get(self.uri, params=params) | ||
|
||
if resp.status_code >= 400: | ||
raise CourierAPIException(resp) | ||
|
||
return resp.json() | ||
|
||
def put_account(self, account_id, account): | ||
""" | ||
Create a new account | ||
Args: | ||
account (dict): Account Defintion - name, parent_account_id, | ||
default_preferences, properties, user_profile, brand_id | ||
Raises: | ||
CourierAPIException: Any error returned by the Courier API | ||
Returns: | ||
dict: Contains an account definition | ||
""" | ||
url = "%s/%s" % (self.uri, account_id) | ||
payload = { | ||
'name': account.get('name'), | ||
'parent_account_id': account.get('parent_account_id'), | ||
'default_preferences': account.get('default_preferences'), | ||
'properties': account.get('properties'), | ||
'user_profile': account.get('user_profile'), | ||
'brand_id': account.get('brand_id') | ||
} | ||
|
||
resp = self.session.put(url, json=payload) | ||
|
||
if resp.status_code >= 400: | ||
raise CourierAPIException(resp) | ||
|
||
return resp.json() | ||
|
||
def delete_account(self, account_id): | ||
""" | ||
Delete an account | ||
Args: | ||
account_id (str): Account you want to delete | ||
Raises: | ||
CourierAPIException: Any error returned by the Courier API | ||
""" | ||
url = "%s/%s" % (self.uri, account_id) | ||
|
||
resp = self.session.delete(url) | ||
|
||
if resp.status_code >= 400: | ||
raise CourierAPIException(resp) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters