Skip to main content

A client for the bunny.net APIs

Project description

bunnynet

A Python client for the bunny.net APIs.

Installation

pip install bunnynet

Usage

Create a client with your bunny.net account API token:

import bunnynet

client = bunnynet.BunnyClient("your-api-token")

Every request uses a default timeout of 10 seconds. Override it for all requests by passing timeout (in seconds):

client = bunnynet.BunnyClient("your-api-token", timeout=30)

Storage zones

# Iterate over every storage zone
for zone in client.storage_zones.get_all():
    print(zone.name)

# Look one up by id or name (returns None if it doesn't exist)
zone = client.storage_zones.get_by_name("my-zone")

# Upload, read and delete files
client.storage_zones.upload_text_file(
    storage_zone=zone,
    contents="Hello World",
    path="some/path",
    file_name="hello.txt",
)
text = client.storage_zones.get_text_file(storage_zone=zone, path="some/path", file_name="hello.txt")
client.storage_zones.delete_file(storage_zone=zone, path="some/path", file_name="hello.txt")

Pull zones

for zone in client.pull_zones.get_all():
    print(zone.name)

Provisioning a pull zone

The pull zone client can also create and configure zones:

pull_zones = client.get_pull_zone_client()

zone = pull_zones.get_by_name("my-zone") or pull_zones.create(
    name="my-zone",
    origin_url="https://origin.example.com",
)

# Apply settings using bunny.net's PascalCase field names. -1 on
# CacheControlMaxAgeOverride tells bunny.net to respect the origin's
# Cache-Control headers.
pull_zones.update(zone.identifier, {
    "IgnoreQueryStrings": False,
    "EnableQueryStringOrdering": True,
    "CacheControlMaxAgeOverride": -1,
})

# Attach a custom hostname, issue a free Let's Encrypt certificate and force
# HTTPS. The hostname's CNAME must resolve to <zone>.b-cdn.net before the
# certificate can be issued.
pull_zones.add_hostname(zone.identifier, "cdn.example.com")
pull_zones.load_free_certificate("cdn.example.com")
pull_zones.set_force_ssl(zone.identifier, "cdn.example.com", force=True)

Signed URLs

import datetime

signed = client.sign_url(
    "https://example.b-cdn.net/foo/bar/file.png",
    key="your-token-authentication-key",
    expiration=datetime.datetime.now(datetime.timezone.utc) + datetime.timedelta(hours=1),
)

Purging the cache

client.purge_url("https://example.b-cdn.net/foo/bar/file.png")

Development

Install dependencies and run the tests with Poetry:

poetry install
poetry run pytest

The integration tests in tests/test_bunnynet.py require a BUNNY_TOKEN environment variable and are skipped when it is not set. The offline unit tests in tests/test_unit.py always run.

License

MIT

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

bunnynet-0.5.0.tar.gz (15.7 kB view details)

Uploaded Source

Built Distribution

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

bunnynet-0.5.0-py3-none-any.whl (22.8 kB view details)

Uploaded Python 3

File details

Details for the file bunnynet-0.5.0.tar.gz.

File metadata

  • Download URL: bunnynet-0.5.0.tar.gz
  • Upload date:
  • Size: 15.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.3.2 CPython/3.13.14 Windows/11

File hashes

Hashes for bunnynet-0.5.0.tar.gz
Algorithm Hash digest
SHA256 822f275dad2e6eb353a3de034add50dce1bbf920f25a178533800a8b041c655e
MD5 ee7c1d168d6bde2bfa2cdebc0d9e379b
BLAKE2b-256 5c8c17ae7d00353d82e04d7af5d30b43ffa389057f70f28934a872b7bf01af23

See more details on using hashes here.

File details

Details for the file bunnynet-0.5.0-py3-none-any.whl.

File metadata

  • Download URL: bunnynet-0.5.0-py3-none-any.whl
  • Upload date:
  • Size: 22.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.3.2 CPython/3.13.14 Windows/11

File hashes

Hashes for bunnynet-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 55c92c86466b692aeca78413059590cfc56b8bb3376727457abdfa0427201058
MD5 247c7ebaab673574d32efcede52061ab
BLAKE2b-256 11290b6adf9c41107ddb2fd5baad15d8529169a6aa05747b4e27cb14056f687b

See more details on using hashes here.

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