-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #107 from ptmminh/feat/club
feat: extract payments on club endpoint; feat: download attendance report; feat: update user's response to an event
- Loading branch information
Showing
9 changed files
with
333 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 = 'user@name.invalid' | ||
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,45 @@ | ||
from abc import ABC | ||
|
||
import aiohttp | ||
|
||
|
||
class AuthenticationError(Exception): | ||
pass | ||
|
||
|
||
class _SpondBase(ABC): | ||
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,58 @@ | ||
from typing import Optional | ||
|
||
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: Optional[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 = [] | ||
|
||
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 self.clientsession.get(url, headers=headers, params=params) as r: | ||
if r.status == 200: | ||
t = await r.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 |
Oops, something went wrong.