Skip to content

Commit 140b19d

Browse files
author
Guillaume Latour
committed
fix: download non zipped report if zip does not exist
1 parent b710366 commit 140b19d

File tree

2 files changed

+51
-26
lines changed

2 files changed

+51
-26
lines changed

pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ maintainers = [
2222
name = "tomtom_api"
2323
readme = "readme.md"
2424
requires-python = ">=3.8"
25-
version = "2024.7.4"
25+
version = "2024.7.9"
2626

2727
[project.urls]
2828
repository = "https://stash.macq.eu/projects/RDDS/repos/tomtom-api"

src/tomtom_api/traffic_stats/models/responses.py

+50-25
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
33
In this module are grouped all the dataclasses that are hydrated when the API is responding something.
44
"""
5+
56
from __future__ import annotations
67

78
import datetime as dt
@@ -47,7 +48,13 @@ class TomtomResponseStatus(JSONWizard, LoadMixin):
4748
response_status: str
4849
urls: Optional[List[str]]
4950

50-
def __init__(self, job_id: int, job_state: TomtomJobState, response_status: str, urls: Optional[List[str]] = None):
51+
def __init__(
52+
self,
53+
job_id: int,
54+
job_state: TomtomJobState,
55+
response_status: str,
56+
urls: Optional[List[str]] = None,
57+
):
5158
self.job_id = job_id
5259
self.job_state = job_state
5360
self.response_status = response_status
@@ -57,7 +64,7 @@ def load_to_enum(o, base_type: Type[Enum]) -> Enum:
5764
return base_type.from_str(o)
5865

5966
def display_info(self) -> str:
60-
return f'{self.job_id} ({self.job_state.name})'
67+
return f"{self.job_id} ({self.job_state.name})"
6168

6269
def download(self, file_type: TomtomDownloadFileType) -> bytes:
6370
"""Download the job report, in the given format.
@@ -82,24 +89,38 @@ def download(self, file_type: TomtomDownloadFileType) -> bytes:
8289
This exception can also be triggered if there is an error while downloading the file.
8390
"""
8491
if self.job_state != TomtomJobState.DONE:
85-
raise DownloadException(f'The "DONE" state is required for downloading the report ({self.job_state}).')
92+
raise DownloadException(
93+
f'The "DONE" state is required for downloading the report ({self.job_state}).'
94+
)
8695

8796
if self.urls is None:
88-
raise DownloadException('There is no download url for this job.')
97+
raise DownloadException("There is no download url for this job.")
98+
99+
filtered_links = [link for link in self.urls if f"{file_type.value}?" in link]
100+
101+
# if nothing is found, remove the compression extension
102+
if len(filtered_links) == 0:
103+
filtered_links = [
104+
link
105+
for link in self.urls
106+
if f"{file_type.value.replace('.zip', '')}?" in link
107+
]
89108

90-
filtered_links = [link for link in self.urls if f'{file_type.value}?' in link]
109+
# then perform a sanity check
91110
if len(filtered_links) != 1:
92-
log.debug(f'Available links: {self.urls}.')
93-
log.debug(f'Provided file type: {file_type}.')
94-
raise DownloadException(f'Impossible to find the desired file in the following links: {filtered_links}.')
111+
log.debug(f"Available links: {self.urls}.")
112+
log.debug(f"Provided file type: {file_type}.")
113+
raise DownloadException(
114+
f"Impossible to find the desired file in the following links: {filtered_links}."
115+
)
95116

96117
link = filtered_links[0]
97-
log.debug(f'Performing request to {link}')
118+
log.debug(f"Performing request to {link}")
98119
response = requests.get(link)
99120

100121
if response.status_code != 200:
101-
log.debug(f'Response: {response}')
102-
raise DownloadException(f'Error while downloading {link}')
122+
log.debug(f"Response: {response}")
123+
raise DownloadException(f"Error while downloading {link}")
103124

104125
return response.content
105126

@@ -124,10 +145,10 @@ def write(self, file: Path, file_type: TomtomDownloadFileType) -> None:
124145
"""
125146
content = self.download(file_type)
126147
if file.exists():
127-
log.warning(f'The following file will be overwritten: {file}')
148+
log.warning(f"The following file will be overwritten: {file}")
128149

129-
log.debug(f'Writing file {file}')
130-
with open(file, 'wb') as f:
150+
log.debug(f"Writing file {file}")
151+
with open(file, "wb") as f:
131152
f.write(content)
132153

133154

@@ -145,25 +166,29 @@ class TomtomJobInfo(JSONWizard, LoadMixin):
145166
name: str
146167
created_at: dt.datetime
147168
state: TomtomJobState
148-
job_id: int = json_field('id', all=True)
149-
job_type: str = json_field('type', all=True)
150-
completed_at: Optional[dt.datetime] = json_field('completed_at', default=None)
169+
job_id: int = json_field("id", all=True)
170+
job_type: str = json_field("type", all=True)
171+
completed_at: Optional[dt.datetime] = json_field("completed_at", default=None)
151172

152173
def load_to_enum(o, base_type: Type[Enum]) -> Enum:
153174
return base_type.from_str(o)
154175

155176
def display_info(self) -> str:
156-
datetime_fmt = '%Y-%m-%d %H:%M:%S'
177+
datetime_fmt = "%Y-%m-%d %H:%M:%S"
157178
start = self.created_at.strftime(datetime_fmt)
158-
end = 'None' if self.completed_at is None else self.completed_at.strftime(datetime_fmt)
159-
return f'┌─ [{self.job_id}] {self.name} // {self.state.name}\n└─ {self.job_type} <{start}{end}>'
179+
end = (
180+
"None"
181+
if self.completed_at is None
182+
else self.completed_at.strftime(datetime_fmt)
183+
)
184+
return f"┌─ [{self.job_id}] {self.name} // {self.state.name}\n└─ {self.job_type} <{start}{end}>"
160185

161186

162187
@dataclass
163188
class Sort(JSONWizard):
164-
is_sorted: bool = json_field('sorted', all=True)
165-
is_unsorted: bool = json_field('unsorted', all=True)
166-
is_empty: bool = json_field('empty', all=True)
189+
is_sorted: bool = json_field("sorted", all=True)
190+
is_unsorted: bool = json_field("unsorted", all=True)
191+
is_empty: bool = json_field("empty", all=True)
167192

168193

169194
@dataclass
@@ -194,5 +219,5 @@ class TomtomResponseSearchJobs(JSONWizard):
194219
@dataclass
195220
class JsonIpResponse(JSONWizard):
196221
ip: str
197-
geo_ip: str = json_field('geo-ip', all=True)
198-
api_help: str = json_field('API Help', all=True)
222+
geo_ip: str = json_field("geo-ip", all=True)
223+
api_help: str = json_field("API Help", all=True)

0 commit comments

Comments
 (0)