-
Notifications
You must be signed in to change notification settings - Fork 0
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
20 changed files
with
283 additions
and
318 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
__pycache__/ | ||
/venv/ | ||
.env | ||
.env | ||
.env.* |
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,41 @@ | ||
from json import JSONDecoder | ||
from ipaddress import IPv4Address | ||
|
||
from liqpy.util.convert import to_datetime | ||
|
||
|
||
class Decoder(JSONDecoder): | ||
def __init__(self): | ||
super().__init__( | ||
object_hook=self._object_hook, | ||
parse_float=float, | ||
parse_int=int, | ||
parse_constant=None, | ||
strict=True, | ||
object_pairs_hook=None, | ||
) | ||
self.create_date = to_datetime | ||
self.end_date = to_datetime | ||
self.completion_date = to_datetime | ||
self.mpi_eci = int | ||
self.ip = IPv4Address | ||
self.refund_date_last = to_datetime | ||
|
||
def _object_hook(self, o: dict, /) -> dict: | ||
for key, value in o.items(): | ||
try: | ||
fn = getattr(self, key, None) | ||
|
||
if not callable(fn): | ||
continue | ||
|
||
processed = fn(value) | ||
|
||
if processed is not None: | ||
o[key] = processed | ||
|
||
except Exception as e: | ||
raise Exception(f"Failed to post convert {key} parameter.") from e | ||
|
||
return o | ||
|
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,76 @@ | ||
from functools import singledispatchmethod | ||
from dataclasses import asdict | ||
|
||
from base64 import b64encode | ||
from json import JSONEncoder | ||
|
||
from uuid import UUID | ||
from decimal import Decimal | ||
from datetime import date, datetime, UTC | ||
|
||
from liqpy.models.request import FiscalItem, DetailAddenda, SplitRule | ||
|
||
|
||
__all__ = ("Encoder", "JSONEncoder") | ||
|
||
|
||
class Encoder(JSONEncoder): | ||
date_fmt = r"%Y-%m-%d %H:%M:%S" | ||
|
||
def __init__(self) -> None: | ||
super().__init__( | ||
skipkeys=False, | ||
ensure_ascii=True, | ||
check_circular=True, | ||
allow_nan=False, | ||
sort_keys=False, | ||
indent=None, | ||
separators=None, | ||
default=None, | ||
) | ||
|
||
@singledispatchmethod | ||
def default(self, o): | ||
return super().default(o) | ||
|
||
@default.register | ||
def _(self, o: Decimal) -> float: | ||
return round(float(o), 4) | ||
|
||
@default.register | ||
def _(self, o: datetime) -> str: | ||
return o.astimezone(UTC).strftime(self.date_fmt) | ||
|
||
@default.register | ||
def _(self, o: date) -> str: | ||
return o.strftime(self.date_fmt) | ||
|
||
@default.register | ||
def _(self, o: bytes) -> str: | ||
return o.decode("utf-8") | ||
|
||
@default.register | ||
def _(self, o: UUID) -> str: | ||
return str(o) | ||
|
||
@default.register | ||
def _(self, o: DetailAddenda) -> str: | ||
data = { | ||
"airLine": o.air_line, | ||
"ticketNumber": o.ticket_number, | ||
"passengerName": o.passenger_name, | ||
"flightNumber": o.flight_number, | ||
"originCity": o.origin_city, | ||
"destinationCity": o.destination_city, | ||
"departureDate": o.departure_date.strftime(r"%d%m%y"), | ||
} | ||
|
||
return b64encode(self.encode(data).encode()).decode() | ||
|
||
@default.register | ||
def _(self, o: SplitRule) -> dict: | ||
return asdict(o) | ||
|
||
@default.register | ||
def _(self, o: FiscalItem) -> dict: | ||
return asdict(o) |
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
Oops, something went wrong.