forked from OceanNetworksCanada/api-python-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_util.py
74 lines (62 loc) · 2.21 KB
/
_util.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
from datetime import timedelta
from pathlib import Path
import humanize
import requests
def saveAsFile(
response: requests.Response, outPath: Path, fileName: str, overwrite: bool
) -> None:
"""
Saves the file downloaded in the response object, in the outPath, with filename
If overwrite, will overwrite files with the same name
"""
filePath = outPath / fileName
outPath.mkdir(parents=True, exist_ok=True)
# Save file in outPath if it doesn't exist yet
if Path.exists(filePath) and not overwrite:
raise FileExistsError(filePath)
with open(filePath, "wb+") as file:
file.write(response.content)
def _formatSize(size: float) -> str:
"""
Returns a formatted file size string representation
@param size: {float} Size in bytes
"""
return humanize.naturalsize(size)
def _formatDuration(secs: float) -> str:
"""
Returns a formatted time duration string representation of a duration in seconds
@param seconds: float
"""
if secs < 1.0:
txtDownTime = f"{secs:.3f} seconds"
else:
d = timedelta(seconds=secs)
txtDownTime = humanize.naturaldelta(d)
return txtDownTime
def _createErrorMessage(response: requests.Response) -> str:
"""
Method to print infromation of an error returned by the API to the console
Builds the error description from the response object
"""
status = response.status_code
if status == 400:
prefix = f"\nStatus 400 - Bad Request: {response.url}"
payload = response.json()
# see https://wiki.oceannetworks.ca/display/O2A for error codes
msg = f"{prefix}\n" + "\n".join(
[
f"API Error {e['errorCode']}: {e['errorMessage']} "
f"(parameter: {e['parameter']})"
for e in payload["errors"]
]
)
return msg
elif status == 401:
return (
f"Status 401 - Unauthorized: {response.url}\n"
"Please check that your Web Services API token is valid. "
"Find your token in your registered profile at "
"https://data.oceannetworks.ca."
)
else:
return f"The server request failed with HTTP status {status}."