cdm_data_loaders.utils.file_transfer.async_client ¶
Asynchronous file download client.
Usage:
initialise a download client that downloads files in chunks of 1024 bytes and retries the downlooad¶
three times in case of connection/timeout errors¶
async_client = AsyncFileDownloader(max_attempts=3, chunk_size=1024)
output_path will be the path to the saved file¶
output_path = await async_client.download( "https://example.com/file.txt", Path("/path") / "to" / "save" / "file.txt"), expected_checksum="some_checksum", checksum_fn="sha512", extra_headers={ "If-None-Match": "abc", "If-Modified-Since": "Wed, 21 Oct 2015 07:28:00 GMT" })
AsyncFileDownloader ¶
Bases: BaseFileDownloader
Asynchronous downloader interface.
Source code in src/cdm_data_loaders/utils/file_transfer/async_client.py
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 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 | |
__init__ ¶
__init__(
client: AsyncClient | None = None,
max_attempts: int = RETRY_DEFAULTS["max_attempts"],
min_backoff: int = RETRY_DEFAULTS["min_backoff"],
max_backoff: int = RETRY_DEFAULTS["max_backoff"],
chunk_size: int = 8192,
max_concurrency: int | None = None,
) -> None
Initialise an async download client.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
client
|
AsyncClient | None
|
an httpx.Client object, defaults to None |
None
|
max_attempts
|
int
|
how many times to retry the request defaults to 5 |
RETRY_DEFAULTS['max_attempts']
|
min_backoff
|
int
|
minimum backoff for retries, defaults to 1 |
RETRY_DEFAULTS['min_backoff']
|
max_backoff
|
int
|
maximum backoff for retries, defaults to 30 |
RETRY_DEFAULTS['max_backoff']
|
chunk_size
|
int
|
chunk size for the downloader, defaults to 8192 |
8192
|
max_concurrency
|
int | None
|
maximum concurrency for the downloader, defaults to None |
None
|
Source code in src/cdm_data_loaders/utils/file_transfer/async_client.py
download
async
¶
download(
url: str,
destination: str | Path,
expected_checksum: str | None = None,
checksum_fn: str | None = "sha256",
extra_headers: dict[str, Any] | None = None,
) -> Path | None
Download a file from url to destination on disk.
Returns the file path if successful; None otherwise. 304 / Not Modified responses return None.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
str
|
URL to download from |
required |
destination
|
str | Path
|
where to save the file to |
required |
expected_checksum
|
str | None
|
expected checksum (if known), defaults to None |
None
|
checksum_fn
|
str | None
|
function used for calculating the checksum, defaults to "sha256" |
'sha256'
|
extra_headers
|
dict[str, Any] | None
|
allow extra headers to be passed, defaults to None |
None
|
Returns:
| Type | Description |
|---|---|
Path | None
|
either the path to the downloaded file or None if no file was downloaded |