-
Notifications
You must be signed in to change notification settings - Fork 0
/
files.py
68 lines (54 loc) · 1.67 KB
/
files.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
import logging
import pathlib
import importlib_resources
import requests
from common import HQ_VOLUME_PATH
from helpers import *
logger = logging.getLogger(__name__)
def putfile(host: str, user: str, password: str, file: str, destfolder: str, *args, **kwargs):
"""
PUT request to Admin REST API at /files.
args and kwargs for requests.put.
"""
basedir = importlib_resources.files("main")
filepath = basedir.joinpath(file)
filename = pathlib.Path(filepath).name
destination = f"{HQ_VOLUME_PATH}/{destfolder}/{filename}"
REST_URL = f"https://{host}:8443/files{destination}"
try:
with open(filepath, "rb") as f:
response = requests.put(
url=REST_URL,
auth=(user, password),
verify=False,
data=f,
timeout=5,
*args,
**kwargs
)
response.raise_for_status()
return response
except Exception as error:
logger.warning("PUTFILE ERROR %s", error)
return None
# Get file from destination
def getfile(host: str, user: str, password: str, filepath: str, *args, **kwargs):
"""
GET request to Admin REST API at /files.
args and kwargs for requests.get.
"""
REST_URL = f"https://{host}:8443/files{filepath}"
try:
response = requests.get(
url=REST_URL,
auth=(user, password),
verify=False,
timeout=5,
*args,
**kwargs
)
response.raise_for_status()
return response
except Exception as error:
logger.warning("No such file: %s", error)
return None