-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathclient.py
More file actions
67 lines (50 loc) · 2.1 KB
/
client.py
File metadata and controls
67 lines (50 loc) · 2.1 KB
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
from pathlib import Path
class FilesClient:
def __init__(self, parent):
self._parent = parent
self._v1 = None
@property
def v1(self):
if self._v1 is None:
self._v1 = FilesV1(self._parent)
return self._v1
class FilesV1:
"""Client for `/v1/files` endpoints.
Usage example:
>>> import incydr
>>>
>>> client = incydr.Client(**kwargs)
>>> client.files.v1.download_file_by_sha256("example_hash", "./testfile.test")
"""
def __init__(self, parent):
self._parent = parent
def download_file_by_sha256(self, sha256: str, target_path: Path) -> Path:
"""Download a file that matches the given SHA256 hash.
**Parameters:**
* **sh256**: `str` (required) The SHA256 hash matching the file you wish to download.
* **target_path**: `Path | str` a string or `pathlib.Path` object that represents the target file path and
name to which the file will be saved to.
**Returns**: A `pathlib.Path` object representing the location of the downloaded file.
"""
target = Path(
target_path
) # ensure that target is a path even if we're given a string
response = self._parent.session.get(f"/v1/files/get-file-by-sha256/{sha256}")
target.write_bytes(response.content)
return target
def stream_file_by_sha256(self, sha256: str):
"""Stream a file that matches the given SHA256 hash.
**Example usage:**
```
>>> with sdk.files.v1.stream_file_by_sha256("example_hash") as response:
>>> with open("./testfile.zip", "wb") as file:
>>> for chunk in response.iter_content(chunk_size=128):
>>> file.write(chunk)
```
**Parameters:**
* **sh256**: `str` (required) The SHA256 hash matching the file you wish to download.
**Returns**: A `requests.Response` object with a stream of the requested file.
"""
return self._parent.session.get(
f"/v1/files/get-file-by-sha256/{sha256}", stream=True
)