download
napt.download.download
HTTP(S) file download for NAPT.
Downloads files to a destination folder. Retries on transient failures, supports ETag-based conditional requests, and writes to a .part file before renaming on success.
Behavior
- Retries on status codes 429, 500, 502, 503, 504 with exponential backoff
- Sends If-None-Match when etag is provided; If-Modified-Since when last_modified is provided
- Writes to a .part file and renames on success
- Hashes content during download; validates against expected_sha256 if set
- Reads filename from Content-Disposition (including RFC 5987 filenames), falling back to the URL path
- Forces Accept-Encoding: identity to keep ETags stable across requests
DEFAULT_CHUNK (1 MiB) is the stream chunk size.
Example
Basic download:
from pathlib import Path
from napt.download import download_file
result = download_file(
url="https://example.com/installer.msi",
destination_folder=Path("./downloads/my-app"),
)
print(f"Downloaded to {result.file_path}")
print(f"SHA-256: {result.sha256}")
Conditional download (avoid re-downloading):
from napt.exceptions import NotModifiedError
try:
result = download_file(
url="https://example.com/installer.msi",
destination_folder=Path("./downloads/my-app"),
etag=previous_etag,
)
except NotModifiedError:
print("File unchanged, using cached version")
Checksum validation:
Note
CDNs compute representation-specific ETags, so requesting gzip vs identity can yield different ETags for the same content. Forcing Accept-Encoding: identity keeps ETags stable. Timeouts are per-request, not total download time.
download_file
download_file(url: str, destination_folder: Path, *, expected_sha256: str | None = None, validate_content_type: bool = False, timeout: int = 60, etag: str | None = None, last_modified: str | None = None) -> DownloadResult
Downloads a URL to destination_folder.
Follows redirects and retries transient failures. Writes to a .part file then renames to the final filename on success. Sends conditional headers if etag or last_modified is provided. Validates checksum if expected_sha256 is set.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
str
|
Source URL. |
required |
destination_folder
|
Path
|
Folder to save into (created if missing). |
required |
expected_sha256
|
str | None
|
Optional known SHA-256 (hex). If provided and the computed hash does not match, the .part file is deleted and NetworkError is raised. |
None
|
validate_content_type
|
bool
|
If True, raises ConfigError when the server responds with Content-Type: text/html. |
False
|
timeout
|
int
|
Per-request timeout in seconds. |
60
|
etag
|
str | None
|
Previous ETag for If-None-Match conditional GET. |
None
|
last_modified
|
str | None
|
Previous Last-Modified for If-Modified-Since conditional GET. |
None
|
Returns:
| Type | Description |
|---|---|
DownloadResult
|
Download result containing file path, SHA-256 hash, and HTTP response headers. |
Raises:
| Type | Description |
|---|---|
NotModifiedError
|
On HTTP 304, when the server confirms the content has not changed since the last request. |
NetworkError
|
For non-2xx responses (after retries), checksum mismatch, or incomplete download (Content-Length mismatch). |
ConfigError
|
If validate_content_type is True and the server responds with text/html. |
Source code in napt/download/download.py
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 | |