Skip to main content

Python download library

Project description

Download Boss

Python CI Build PyPI Downloads PyPI Version

Python download library

1. Installation

pip install download_boss

 

2. Usage

2.1 HttpClient

Simple request

from requests import Request
from download_boss.client.request.RequestEnvelope import RequestEnvelope
from download_boss.client.HttpClient import HttpClient

request = RequestEnvelope(
    Request(method='POST', url='https://httpbin.org/anything/hello', json={"hello": "world"},
    {'verify': False, 'timeout': 10})
)
response = HttpClient().download(request)
print(response.text)

Retry based on HTTP status codes

from requests import Request
from download_boss.client.request.RequestEnvelope import RequestEnvelope
from download_boss.client.HttpClient import HttpClient
from download_boss.error.ClientRetriable import ClientRetriable

request = RequestEnvelope(
    Request(method='GET', url='https://httpbin.org/anything/hello')
)
client = HttpClient(throwRetriableStatusCodeRanges=[401, range(500,599)])

while True:
    try:
        response = client.download(request)
        print(response.text)
        break
    except ClientRetriable:
        continue

Kerberos authentication:

from requests import Request
from requests_kerberos import HTTPKerberosAuth, OPTIONAL
from download_boss.client.request.RequestEnvelope import RequestEnvelope
from download_boss.client.HttpClient import HttpClient

request = RequestEnvelope(
    Request(method='POST', url='https://httpbin.org/anything/kerb', auth=HTTPKerberosAuth(mutual_authentication=OPTIONAL))
)
response = HttpClient().download(request)

2.2. RetryWrapper

Retry automatically some HTTP status codes

from requests import Request
from download_boss.client.request.RequestEnvelope import RequestEnvelope
from download_boss.client.HttpClient import HttpClient
from download_boss.wrapper.RetryWrapper import RetryWrapper
from download_boss.error.RetriesExhausted import RetriesExhausted

request = RequestEnvelope(
    Request(method='GET', url='https://httpbin.org/status/500')
)
client = HttpClient(throwRetriableStatusCodeRanges=[401, range(500,599)])
client = RetryWrapper(client, count=1, catchRetriableStatusCodeRanges=[range(500,599)])

try:
    response = client.download(request)
except RetriesExhausted:
    print("Retries exhausted!")

"""
2024-12-03 11:51:10,085 [ INFO] HttpClient.py :: download() - Requesting: GET https://httpbin.org/status/500
2024-12-03 11:51:10,485 [ INFO] RetryWrapper.py :: download() - Retrying... GET https://httpbin.org/status/500
2024-12-03 11:52:10,485 [ INFO] HttpClient.py :: download() - Requesting: GET https://httpbin.org/status/500
Retries exhausted!
"""

2.3. DelayWrapper

Delay download calls by 2-5 seconds

from requests import Request
from download_boss.client.request.RequestEnvelope import RequestEnvelope
from download_boss.client.HttpClient import HttpClient
from download_boss.wrapper.RetryWrapper import RetryWrapper
from download_boss.wrapper.DelayWrapper import DelayWrapper
from download_boss.error.RetriesExhausted import RetriesExhausted

client = HttpClient(throwRetriableStatusCodeRanges=[401, range(500,599)])
client = RetryWrapper(client, count=1, catchRetriableStatusCodeRanges=[range(500,599)]) 
client = DelayWrapper(client, length=2, maxLength=5) 

requests = [
    RequestEnvelope( Request(method='GET', url='https://httpbin.org/anything/one') ),
    RequestEnvelope( Request(method='GET', url='https://httpbin.org/anything/two') ),
    RequestEnvelope( Request(method='GET', url='https://httpbin.org/anything/three') )
]

for r in requests:
    response = client.download(r)

"""
2024-12-03 12:00:28,804 [ INFO] DelayWrapper.py :: download() - Delaying by 3s ... GET https://httpbin.org/anything/one
2024-12-03 12:00:31,805 [ INFO] HttpClient.py :: download() - Requesting: GET https://httpbin.org/anything/one
2024-12-03 12:00:32,206 [ INFO] DelayWrapper.py :: download() - Delaying by 2s ... GET https://httpbin.org/anything/two
2024-12-03 12:00:34,208 [ INFO] HttpClient.py :: download() - Requesting: GET https://httpbin.org/anything/two
2024-12-03 12:00:34,827 [ INFO] DelayWrapper.py :: download() - Delaying by 5s ... GET https://httpbin.org/anything/three
2024-12-03 12:00:39,830 [ INFO] HttpClient.py :: download() - Requesting: GET https://httpbin.org/anything/three
"""

2.4. FileCacheWrapper

from os.path import join, dirname
from requests import Request
from download_boss.client.request.RequestEnvelope import RequestEnvelope
from download_boss.client.HttpClient import HttpClient
from download_boss.wrapper.RetryWrapper import RetryWrapper
from download_boss.wrapper.DelayWrapper import DelayWrapper
from download_boss.wrapper.FileCacheWrapper import FileCacheWrapper

cacheFolderPath = join(dirname(__file__), "cache")
cacheLength = 60*60*24 # 1 day

client = HttpClient(throwRetriableStatusCodeRanges=[401, range(500,599)]) 
client = RetryWrapper(client, count=1, catchRetriableStatusCodeRanges=[range(500,599)]) 
client = DelayWrapper(client, length=2, maxLength=5)
client = FileCacheWrapper(client, cacheFolderPath, cacheLength)

requests = [
    RequestEnvelope( Request(method='GET', url='https://httpbin.org/anything/one') ),
    RequestEnvelope( Request(method='GET', url='https://httpbin.org/anything/one') ),
    RequestEnvelope( Request(method='GET', url='https://httpbin.org/anything/one') )
]

for r in requests:
    response = client.download(r)

"""
2024-12-03 13:26:24,921 [ INFO] FileCacheWrapper.py :: _getCache() - Cache miss: GET https://httpbin.org/anything/one
2024-12-03 13:26:24,921 [ INFO] DelayWrapper.py :: download() - Delaying by 3s ... GET https://httpbin.org/anything/one
2024-12-03 13:26:27,923 [ INFO] HttpClient.py :: download() - Requesting: GET https://httpbin.org/anything/one
2024-12-03 13:26:27,956 [DEBUG] connectionpool.py :: _new_conn() - Starting new HTTPS connection (1): httpbin.org:443
2024-12-03 13:26:29,256 [DEBUG] connectionpool.py :: _make_request() - https://httpbin.org:443 "GET /anything/one HTTP/11" 200 370
2024-12-03 13:26:29,257 [DEBUG] FileCacheWrapper.py :: _getCache() - Cache found: GET https://httpbin.org/anything/one
2024-12-03 13:26:29,263 [DEBUG] FileCacheWrapper.py :: _getCache() - Cache found: GET https://httpbin.org/anything/one
"""

 

3. Contribute

3.1. Install locally

Install pip/python.

Clone the project.

Create virtual env:

# Install virtualenv module
pip install --upgrade virtualenv
cd <PROJECT_ROOT>

# Create venv in your project
python -m venv venv

# Activate your virtual environment (Windows)
.\venv\Scripts\activate
+
# Activate your virtual environment (Linux)
chmod +x venv/bin/activate
source venv/bin/activate

Install project dependencies:

pip install -r requirements.txt

Install module locally as editable

pip install -e .

 

3.2. Testing

# Run test suite (Windows)
.\wtests.bat

# Run test suite (Linux)
./tests.sh

 

3.3. Release (automated)

Git add/commit/push to GitHub. The GitHub action will automatically publish the new version to PyPi.

 

3.4. Release (manual)

Install dependencies

pip install --upgrade setuptools wheel build twine

Build the package (wheel and sdist)

python -m build 

Ensure .pypirc in user folder is correct, then upload

python -m twine upload dist/*

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

download_boss-0.0.9.tar.gz (11.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

download_boss-0.0.9-py3-none-any.whl (16.8 kB view details)

Uploaded Python 3

File details

Details for the file download_boss-0.0.9.tar.gz.

File metadata

  • Download URL: download_boss-0.0.9.tar.gz
  • Upload date:
  • Size: 11.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for download_boss-0.0.9.tar.gz
Algorithm Hash digest
SHA256 32136b9fc60eaf2d99c455e422edc2fdff42c8b5d8780e9629719fe586653717
MD5 d483ed28203d3d5e34dcef7f02df4e84
BLAKE2b-256 e9dc73b42ec0779ecb8192f50300d4de949cbec0617fe63e414b14edf929aebb

See more details on using hashes here.

Provenance

The following attestation bundles were made for download_boss-0.0.9.tar.gz:

Publisher: python-publish.yml on kristof9851/download_boss

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file download_boss-0.0.9-py3-none-any.whl.

File metadata

  • Download URL: download_boss-0.0.9-py3-none-any.whl
  • Upload date:
  • Size: 16.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for download_boss-0.0.9-py3-none-any.whl
Algorithm Hash digest
SHA256 b9e76f39599fe91e83751a6a2fe5a3372d05e62abbd7d01bcfd2e0bf06611c04
MD5 c299ed27c752dd89ecb8a7d1ccc18730
BLAKE2b-256 182fecb0c51a058788ec2521f919526fa0d562da88876bccc86870f53a3dc80d

See more details on using hashes here.

Provenance

The following attestation bundles were made for download_boss-0.0.9-py3-none-any.whl:

Publisher: python-publish.yml on kristof9851/download_boss

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page