Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 46b0897

Browse files
committedSep 13, 2024·
Add find_pickup_site for mondial relay and common schema
1 parent 28c5dad commit 46b0897

File tree

6 files changed

+169
-7
lines changed

6 files changed

+169
-7
lines changed
 

‎roulier.py

-2
This file was deleted.

‎roulier/carriersv2/helpers.py

+15
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,18 @@ def clean_empty(data):
2121

2222
def none_as_empty(data):
2323
return {k: v if v is not None else "" for k, v in data.items()}
24+
25+
26+
def merge(*dicts):
27+
# Recursively merge dictionaries
28+
result = {}
29+
for d in dicts:
30+
for k, v in d.items():
31+
if isinstance(v, dict):
32+
result[k] = merge(result.get(k, {}), v)
33+
else:
34+
if not v and result.get(k):
35+
# Do not override value with empty value
36+
continue
37+
result[k] = v
38+
return result

‎roulier/carriersv2/mondialrelay/constants.py

+13
Original file line numberDiff line numberDiff line change
@@ -146,4 +146,17 @@
146146
"Montage",
147147
"Assurance",
148148
"Instructions",
149+
"Pays",
150+
"NumPointRelais",
151+
"Ville",
152+
"CP",
153+
"Latitude",
154+
"Longitude",
155+
"Taille",
156+
"Poids",
157+
"Action",
158+
"DelaiEnvoi",
159+
"RayonRecherche",
160+
"TypeActivite",
161+
"NombreResultats",
149162
]

‎roulier/carriersv2/mondialrelay/schema.py

+99-4
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,18 @@
33
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
44
from ..helpers import prefix, clean_empty, REMOVED
55
from ..schema import (
6-
LabelInput,
76
Address,
8-
LabelOutput,
97
Auth,
10-
Service,
8+
Label,
9+
LabelInput,
10+
LabelOutput,
1111
Parcel,
1212
ParcelLabel,
13-
Label,
13+
PickupSite,
14+
PickupSiteInput,
15+
PickupSiteOutput,
16+
PickupSiteSearch,
17+
Service,
1418
)
1519
from .constants import SORTED_KEYS
1620
from hashlib import md5
@@ -154,6 +158,48 @@ def soap(self):
154158
)
155159

156160

161+
class MondialRelayPickupSiteSearch(PickupSiteSearch):
162+
id: int | None = None
163+
weight: float | None = None
164+
action: str | None = None
165+
delay: int | None = None
166+
searchRadius: int | None = None
167+
actionType: str | None = None
168+
resultsCount: int | None = None
169+
170+
def soap(self):
171+
return clean_empty(
172+
{
173+
"Pays": self.country,
174+
"NumPointRelais": self.id,
175+
"CP": self.zip,
176+
"Latitude": self.lat,
177+
"Longitude": self.lng,
178+
"Poids": self.weight,
179+
"Action": self.action,
180+
"DelaiEnvoi": self.delay,
181+
"RayonRecherche": self.searchRadius,
182+
"TypeActivite": self.actionType,
183+
"NombreResultats": self.resultsCount,
184+
}
185+
)
186+
187+
188+
class MondialRelayPickupSiteInput(PickupSiteInput):
189+
auth: MondialRelayAuth
190+
search: MondialRelayPickupSiteSearch
191+
192+
def soap(self):
193+
return self.auth.sign(
194+
clean_empty(
195+
{
196+
**self.auth.soap(),
197+
**self.search.soap(),
198+
}
199+
)
200+
)
201+
202+
157203
class MondialRelayLabel(Label):
158204
@classmethod
159205
def from_soap(cls, result):
@@ -181,3 +227,52 @@ class MondialRelayLabelOutput(LabelOutput):
181227
@classmethod
182228
def from_soap(cls, result):
183229
return cls.model_construct(parcels=[MondialRelayParcelLabel.from_soap(result)])
230+
231+
232+
class MondialRelayPickupSite(PickupSite):
233+
actionType: str
234+
hours: dict
235+
url_pic: str
236+
url_map: str
237+
238+
@classmethod
239+
def from_soap(cls, result):
240+
return cls.model_construct(
241+
id=result["Num"],
242+
name="\n".join(
243+
[part for part in [result["LgAdr1"], result["LgAdr2"]] if part]
244+
),
245+
street="\n".join(
246+
[part for part in [result["LgAdr3"], result["LgAdr4"]] if part]
247+
),
248+
zip=result["CP"],
249+
city=result["Ville"],
250+
country=result["Pays"],
251+
lat=result["Latitude"],
252+
lng=result["Longitude"],
253+
actionType=result["TypeActivite"],
254+
hours={
255+
"monday": result["Horaires_Lundi"],
256+
"tuesday": result["Horaires_Mardi"],
257+
"wednesday": result["Horaires_Mercredi"],
258+
"thursday": result["Horaires_Jeudi"],
259+
"friday": result["Horaires_Vendredi"],
260+
"saturday": result["Horaires_Samedi"],
261+
"sunday": result["Horaires_Dimanche"],
262+
},
263+
url_pic=result["URL_Photo"],
264+
url_map=result["URL_Plan"],
265+
)
266+
267+
268+
class MondialRelayPickupSiteOutput(PickupSiteOutput):
269+
sites: list[MondialRelayPickupSite]
270+
271+
@classmethod
272+
def from_soap(cls, result):
273+
return cls.model_construct(
274+
sites=[
275+
MondialRelayPickupSite.from_soap(site)
276+
for site in result["PointsRelais"]["PointRelais_Details"]
277+
]
278+
)

‎roulier/carriersv2/mondialrelay/transporter.py

+14-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@
55

66
from ..api import Transporter, action
77
from ...exception import CarrierError
8-
from .schema import MondialRelayLabelInput, MondialRelayLabelOutput
8+
from .schema import (
9+
MondialRelayLabelInput,
10+
MondialRelayLabelOutput,
11+
MondialRelayPickupSiteInput,
12+
MondialRelayPickupSiteOutput,
13+
)
914
from .constants import STATUSES
1015

1116

@@ -31,3 +36,11 @@ def get_label(self, input: MondialRelayLabelInput) -> MondialRelayLabelOutput:
3136
result = self.client.WSI2_CreationEtiquette(**input.soap())
3237
self.raise_for_status(result)
3338
return MondialRelayLabelOutput.from_soap(result)
39+
40+
@action
41+
def find_pickup_site(
42+
self, input: MondialRelayPickupSiteInput
43+
) -> MondialRelayPickupSiteOutput:
44+
result = self.client.WSI4_PointRelais_Recherche(**input.soap())
45+
self.raise_for_status(result)
46+
return MondialRelayPickupSiteOutput.from_soap(result)

‎roulier/carriersv2/schema.py

+28
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,19 @@ class LabelInput(BaseModel):
5757
to_address: ToAddress
5858

5959

60+
class PickupSiteSearch(BaseModel):
61+
country: str
62+
city: str | None = None
63+
zip: str
64+
lat: float | None = None
65+
lng: float | None = None
66+
67+
68+
class PickupSiteInput(BaseModel):
69+
auth: Auth
70+
search: PickupSiteSearch
71+
72+
6073
class Tracking(BaseModel):
6174
number: str
6275
url: str | None = None
@@ -79,3 +92,18 @@ class ParcelLabel(BaseModel):
7992
class LabelOutput(BaseModel):
8093
parcels: list[ParcelLabel]
8194
annexes: list[Label] = []
95+
96+
97+
class PickupSite(BaseModel):
98+
id: int
99+
name: str
100+
street: str
101+
zip: str
102+
city: str
103+
country: str
104+
lat: str
105+
lng: str
106+
107+
108+
class PickupSiteOutput(BaseModel):
109+
sites: list[PickupSite]

0 commit comments

Comments
 (0)
Please sign in to comment.