Skip to content
This repository has been archived by the owner on Dec 8, 2023. It is now read-only.

Commit

Permalink
Merge pull request #4 from jssuzanne/endpoint_exports
Browse files Browse the repository at this point in the history
Endpoint exports
  • Loading branch information
olance authored Feb 7, 2020
2 parents 5ce86ac + 5f6966b commit b510bd1
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ parts/
sdist/
var/
wheels/
pip-wheel-metadata/
*.egg-info/
.installed.cfg
*.egg
Expand Down
4 changes: 4 additions & 0 deletions alma/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,7 @@ def merchants(self) -> endpoints.Merchants:
@property
def orders(self) -> endpoints.Orders:
return self._endpoint("Orders")

@property
def exports(self) -> endpoints.Exports:
return self._endpoint("Exports")
3 changes: 2 additions & 1 deletion alma/endpoints/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
from .base import Base
from .merchants import Merchants
from .orders import Orders
from .payments import Payments
from .exports import Exports
from .payments import Payments
48 changes: 48 additions & 0 deletions alma/endpoints/exports.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
from functools import partial
from datetime import datetime
from io import BytesIO

from . import Base
from ..entities import Export, ExportType, ExportFormat
from ..paginated_results import PaginatedResults


class Exports(Base):
EXPORTS_PATH = "/v1/data-exports"

def create(self, export_type: ExportType, payout_id: str = None,
start: datetime = None, end: datetime = None):
""" Create a new export"""
data = {"type": export_type.value}
if payout_id:
data['payout'] = payout_id

if start:
data['start'] = int(start.timestamp())

if end:
data['end'] = int(end.timestamp())

response = self.request(self.EXPORTS_PATH).set_body(data).post()
return Export(response.json)

def get_file(self, export_id: str, export_format: ExportFormat = None):
request = self.request(f"{self.EXPORTS_PATH}/{export_id}")
if export_format:
request.set_query_params({"format": export_format.value})

response = request.get()
return BytesIO(response.resp.content)

def fetch_all(self, limit: int = 5, **filters):
args = {"limit": limit, **filters}
response = self.request(self.EXPORTS_PATH).set_query_params(args).get()
next_page = partial(self.fetch_all, limit=limit, **filters)
return PaginatedResults(response.json, Export, next_page)

def fetch(self, export_id: str = None, limit: int = 5, **filters):
if export_id is None:
return self.fetch_all(limit=limit, **filters)
else:
response = self.request(f"{self.EXPORTS_PATH}/{export_id}").get()
return Export(response.json)
1 change: 1 addition & 0 deletions alma/entities/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
from .order import Order
from .payment import Payment, PaymentFraudType, PaymentState
from .refund import Refund
from .export import Export, ExportType, ExportFormat
19 changes: 19 additions & 0 deletions alma/entities/export.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from enum import Enum

from . import Base


class ExportType(Enum):
PAYMENTS = "payments"
ACCOUNTING = "accounting"
ACCOUNTING_FOR_PAYOUT = "accounting_for_payout"


class ExportFormat(Enum):
CSV = "csv"
PDF = "pdf"
XLSX = "xlsx"


class Export(Base):
pass

0 comments on commit b510bd1

Please sign in to comment.