Skip to content

Commit

Permalink
Add accounts API (#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
tk26 authored Jul 24, 2023
1 parent e44868d commit 61dd7e8
Show file tree
Hide file tree
Showing 5 changed files with 214 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ This project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased][unreleased]

## [v4.6.0] - 2023-07-24

Adds support for Accounts API

## [v4.5.0] - 2022-07-26

Adds support for Cancel Message API
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setuptools.setup(
name="trycourier",
version="4.5.0",
version="4.6.0",
author="Courier",
author_email="support@courier.com",
description="A Python Package for communicating with the Courier REST API.",
Expand Down
95 changes: 95 additions & 0 deletions tests/test_accounts.py
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
111 changes: 111 additions & 0 deletions trycourier/accounts.py
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)
4 changes: 3 additions & 1 deletion trycourier/client.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from os import environ

from .accounts import Accounts
from .audiences import Audiences
from .audit_events import AuditEvents
from .automations import Automations
Expand All @@ -11,7 +12,7 @@
from .notifications import Notifications
from .profiles import Profiles

__version__ = '4.4.0'
__version__ = '4.6.0'


class Courier(object):
Expand Down Expand Up @@ -58,6 +59,7 @@ def __init__(self,
password = environ.get('COURIER_AUTH_PASSWORD', None)
self.session.init_basic_auth(username, password)

self.accounts = Accounts(self.base_url, self.session)
self.audiences = Audiences(self.base_url, self.session)
self.audit_events = AuditEvents(self.base_url, self.session)
self.automations = Automations(self.base_url, self.session)
Expand Down

0 comments on commit 61dd7e8

Please sign in to comment.