-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: extract payments on club endpoint
This feature is needed for our club. However, this is a new major addition to the module as a whole (new API endpoint club). a couple of major changes: - base class to avoid redundant code - manual tests added - example usage script added Related to #70.
- Loading branch information
Showing
9 changed files
with
205 additions
and
69 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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
username = '[email protected]' | ||
password = 'Pa55w0rd' | ||
club_id = '1234567890' |
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,43 @@ | ||
import aiohttp | ||
|
||
|
||
class AuthenticationError(Exception): | ||
pass | ||
|
||
|
||
class SpondBase: | ||
def __init__(self, username, password, api_url): | ||
self.username = username | ||
self.password = password | ||
self.api_url = api_url | ||
self.clientsession = aiohttp.ClientSession(cookie_jar=aiohttp.CookieJar()) | ||
self.token = None | ||
|
||
@property | ||
def auth_headers(self): | ||
return { | ||
"content-type": "application/json", | ||
"Authorization": f"Bearer {self.token}", | ||
} | ||
|
||
def require_authentication(func: callable): | ||
async def wrapper(self, *args, **kwargs): | ||
if not self.token: | ||
try: | ||
await self.login() | ||
except AuthenticationError as e: | ||
await self.clientsession.close() | ||
raise e | ||
return await func(self, *args, **kwargs) | ||
|
||
return wrapper | ||
|
||
async def login(self): | ||
login_url = f"{self.api_url}login" | ||
data = {"email": self.username, "password": self.password} | ||
async with self.clientsession.post(login_url, json=data) as r: | ||
login_result = await r.json() | ||
self.token = login_result.get("loginToken") | ||
if self.token is None: | ||
err_msg = f"Login failed. Response received: {login_result}" | ||
raise AuthenticationError(err_msg) |
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,59 @@ | ||
import aiohttp | ||
|
||
from .base import SpondBase | ||
|
||
|
||
class SpondClub(SpondBase): | ||
def __init__(self, username, password): | ||
super().__init__(username, password, "https://api.spond.com/club/v1/") | ||
self.transactions = None | ||
|
||
@SpondBase.require_authentication | ||
async def get_transactions( | ||
self, club_id: str, skip: int = None, max_items: int = 100 | ||
): | ||
""" | ||
Retrieves a list of transactions/payments for a specified club. | ||
Parameters | ||
---------- | ||
club_id : str | ||
Identifier for the club. Note that this is different from the Group ID used | ||
in the core API. | ||
max_items : int, optional | ||
The maximum number of transactions to retrieve. Defaults to 100. | ||
skip : int, optional | ||
This endpoint only returns 25 transactions at a time (page scrolling). | ||
Therefore, we need to increment this `skip` param to grab the next | ||
25 etc. Defaults to None. It's better to keep `skip` at None | ||
and specify `max_items` instead. This param is only here for the | ||
recursion implementation | ||
Returns | ||
------- | ||
list of dict | ||
A list of transactions, each represented as a dictionary. | ||
""" | ||
if self.transactions is None: | ||
self.transactions = list() | ||
|
||
url = f"{self.api_url}transactions" | ||
params = None if skip is None else {"skip": skip} | ||
headers = {**self.auth_headers, "X-Spond-Clubid": club_id} | ||
|
||
async with aiohttp.ClientSession() as session: | ||
async with session.get(url, headers=headers, params=params) as response: | ||
if response.status == 200: | ||
t = await response.json() | ||
if len(t) == 0: | ||
return self.transactions | ||
|
||
self.transactions.extend(t) | ||
if len(self.transactions) < max_items: | ||
return await self.get_transactions( | ||
club_id=club_id, | ||
skip=len(t) if skip is None else skip + len(t), | ||
max_items=max_items, | ||
) | ||
|
||
return self.transactions |
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
Oops, something went wrong.