download
napt.download
Download operations for NAPT.
Provides HTTP(S) file download with conditional requests, retries, atomic writes, and SHA-256 verification.
Modules:
| Name | Description |
|---|---|
download |
HTTP downloads with ETag support (download_file, make_session). |
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.
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.
DownloadResult
dataclass
Result of a file download operation.
Attributes:
| Name | Type | Description |
|---|---|---|
file_path |
Path
|
Path to the downloaded file. |
sha256 |
str
|
SHA-256 hex digest of the downloaded file. |
headers |
dict
|
HTTP response headers from the download. |
Source code in napt/download/download.py
sha256_file
Computes the SHA-256 hex digest of a file on disk.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path
|
File to hash. |
required |
Returns:
| Type | Description |
|---|---|
str
|
SHA-256 hex digest string. |
Source code in napt/download/download.py
make_session
Creates a requests.Session with retry/backoff defaults.
Retries on common transient status codes (429, 500, 502, 503, 504 — honoring Retry-After) and connection failures, applies exponential backoff, and sets a User-Agent header identifying NAPT. Retries only GET and HEAD; other methods are sent once. The shared transport layer for every NAPT HTTP call outside Microsoft Graph (which has its own retry tuned to Graph's error contract).
Note
Forces Accept-Encoding: identity to request raw (uncompressed) bytes. CDNs compute representation-specific ETags, so requesting gzip vs identity can yield different ETags for the same content. Binary installers (MSI/EXE/MSIX/ZIP) are already compressed, so identity encoding has no size cost.
Source code in napt/download/download.py
download_file
download_file(
url: str,
destination_folder: Path,
*,
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. A server that ignores them and answers 200 with the same validator is treated as not modified too, and the body is never read.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
str
|
Source URL. |
required |
destination_folder
|
Path
|
Folder to save into (created if missing). |
required |
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, or on a 200 whose ETag (or Last-Modified, when no ETag was sent) equals the one sent; either way the content has not changed since the last request. |
NetworkError
|
On transport failure, a non-2xx response (after retries), or an incomplete download (Content-Length mismatch). |
Source code in napt/download/download.py
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 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 | |