Skip to content

Commit

Permalink
Add find_pickup_site for mondial relay and common schema
Browse files Browse the repository at this point in the history
  • Loading branch information
paradoxxxzero committed Sep 13, 2024
1 parent 28c5dad commit 46b0897
Show file tree
Hide file tree
Showing 6 changed files with 169 additions and 7 deletions.
2 changes: 0 additions & 2 deletions roulier.py

This file was deleted.

15 changes: 15 additions & 0 deletions roulier/carriersv2/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,18 @@ def clean_empty(data):

def none_as_empty(data):
return {k: v if v is not None else "" for k, v in data.items()}


def merge(*dicts):
# Recursively merge dictionaries
result = {}
for d in dicts:
for k, v in d.items():
if isinstance(v, dict):
result[k] = merge(result.get(k, {}), v)
else:
if not v and result.get(k):
# Do not override value with empty value
continue
result[k] = v
return result
13 changes: 13 additions & 0 deletions roulier/carriersv2/mondialrelay/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,4 +146,17 @@
"Montage",
"Assurance",
"Instructions",
"Pays",
"NumPointRelais",
"Ville",
"CP",
"Latitude",
"Longitude",
"Taille",
"Poids",
"Action",
"DelaiEnvoi",
"RayonRecherche",
"TypeActivite",
"NombreResultats",
]
103 changes: 99 additions & 4 deletions roulier/carriersv2/mondialrelay/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,18 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
from ..helpers import prefix, clean_empty, REMOVED
from ..schema import (
LabelInput,
Address,
LabelOutput,
Auth,
Service,
Label,
LabelInput,
LabelOutput,
Parcel,
ParcelLabel,
Label,
PickupSite,
PickupSiteInput,
PickupSiteOutput,
PickupSiteSearch,
Service,
)
from .constants import SORTED_KEYS
from hashlib import md5
Expand Down Expand Up @@ -154,6 +158,48 @@ def soap(self):
)


class MondialRelayPickupSiteSearch(PickupSiteSearch):
id: int | None = None
weight: float | None = None
action: str | None = None
delay: int | None = None
searchRadius: int | None = None
actionType: str | None = None
resultsCount: int | None = None

def soap(self):
return clean_empty(
{
"Pays": self.country,
"NumPointRelais": self.id,
"CP": self.zip,
"Latitude": self.lat,
"Longitude": self.lng,
"Poids": self.weight,
"Action": self.action,
"DelaiEnvoi": self.delay,
"RayonRecherche": self.searchRadius,
"TypeActivite": self.actionType,
"NombreResultats": self.resultsCount,
}
)


class MondialRelayPickupSiteInput(PickupSiteInput):
auth: MondialRelayAuth
search: MondialRelayPickupSiteSearch

def soap(self):
return self.auth.sign(
clean_empty(
{
**self.auth.soap(),
**self.search.soap(),
}
)
)


class MondialRelayLabel(Label):
@classmethod
def from_soap(cls, result):
Expand Down Expand Up @@ -181,3 +227,52 @@ class MondialRelayLabelOutput(LabelOutput):
@classmethod
def from_soap(cls, result):
return cls.model_construct(parcels=[MondialRelayParcelLabel.from_soap(result)])


class MondialRelayPickupSite(PickupSite):
actionType: str
hours: dict
url_pic: str
url_map: str

@classmethod
def from_soap(cls, result):
return cls.model_construct(
id=result["Num"],
name="\n".join(
[part for part in [result["LgAdr1"], result["LgAdr2"]] if part]
),
street="\n".join(
[part for part in [result["LgAdr3"], result["LgAdr4"]] if part]
),
zip=result["CP"],
city=result["Ville"],
country=result["Pays"],
lat=result["Latitude"],
lng=result["Longitude"],
actionType=result["TypeActivite"],
hours={
"monday": result["Horaires_Lundi"],
"tuesday": result["Horaires_Mardi"],
"wednesday": result["Horaires_Mercredi"],
"thursday": result["Horaires_Jeudi"],
"friday": result["Horaires_Vendredi"],
"saturday": result["Horaires_Samedi"],
"sunday": result["Horaires_Dimanche"],
},
url_pic=result["URL_Photo"],
url_map=result["URL_Plan"],
)


class MondialRelayPickupSiteOutput(PickupSiteOutput):
sites: list[MondialRelayPickupSite]

@classmethod
def from_soap(cls, result):
return cls.model_construct(
sites=[
MondialRelayPickupSite.from_soap(site)
for site in result["PointsRelais"]["PointRelais_Details"]
]
)
15 changes: 14 additions & 1 deletion roulier/carriersv2/mondialrelay/transporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@

from ..api import Transporter, action
from ...exception import CarrierError
from .schema import MondialRelayLabelInput, MondialRelayLabelOutput
from .schema import (
MondialRelayLabelInput,
MondialRelayLabelOutput,
MondialRelayPickupSiteInput,
MondialRelayPickupSiteOutput,
)
from .constants import STATUSES


Expand All @@ -31,3 +36,11 @@ def get_label(self, input: MondialRelayLabelInput) -> MondialRelayLabelOutput:
result = self.client.WSI2_CreationEtiquette(**input.soap())
self.raise_for_status(result)
return MondialRelayLabelOutput.from_soap(result)

@action
def find_pickup_site(
self, input: MondialRelayPickupSiteInput
) -> MondialRelayPickupSiteOutput:
result = self.client.WSI4_PointRelais_Recherche(**input.soap())
self.raise_for_status(result)
return MondialRelayPickupSiteOutput.from_soap(result)
28 changes: 28 additions & 0 deletions roulier/carriersv2/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,19 @@ class LabelInput(BaseModel):
to_address: ToAddress


class PickupSiteSearch(BaseModel):
country: str
city: str | None = None
zip: str
lat: float | None = None
lng: float | None = None


class PickupSiteInput(BaseModel):
auth: Auth
search: PickupSiteSearch


class Tracking(BaseModel):
number: str
url: str | None = None
Expand All @@ -79,3 +92,18 @@ class ParcelLabel(BaseModel):
class LabelOutput(BaseModel):
parcels: list[ParcelLabel]
annexes: list[Label] = []


class PickupSite(BaseModel):
id: int
name: str
street: str
zip: str
city: str
country: str
lat: str
lng: str


class PickupSiteOutput(BaseModel):
sites: list[PickupSite]

0 comments on commit 46b0897

Please sign in to comment.