Skip to main content

A package to interact with StackPath

Project description

PyStackpath - StackPath Python Library

Actions Status Python Version Pypi license GitHub stars

Python library interact with StackPath API (https://stackpath.dev/reference)

It uses a custom OAuth2 requests.session

Install

pip install pystackpath

Examples

Set up a new Stackpath instance

from pystackpath import Stackpath

sp = Stackpath(
    os.getenv("STACKPATH_CLIENTID"),
    os.getenv("STACKPATH_APISECRET")
)

Search for stacks

stacks = sp.stacks().index(filter="name='my-awesome-stack' and status='ACTIVE'")
print(stacks)

Get one stack

stackid = "afcdaf14-47cb-40dd-9c13-3b20e6caf74a
onestack = sp.stacks().get(stackid)

Create a new stack

accountid = "081af5ee-38f8-44e9-b08a-881ea5de66f9"
newstack = sp.stacks().create(accountid, "my-second-stack")

Create a cdn site

stackid = "afcdaf14-47cb-40dd-9c13-3b20e6caf74a
cdnsite = sp.stacks().get(stackid).cdnsites().create(
    domain="cdn.johndoe.com",
    origin={"hostname": "www.johndoe.com"},
    type="CDN"
)

Search for cdn sites

cdnsite = sp.stacks().get(stackid).cdnsites().index(
    filter="label='www.johndoe.com' and status='ACTIVE'"
)

Delete a cdn site

cdnsiteid = "19e1a7b2-068f-491c-a95f-b64eae66dd34"
cdnsite = sp.stacks().get(stackid).cdnsites().get(cdnsiteid).delete()

Disable and enable a cdn site

cdnsiteid = "19e1a7b2-068f-491c-a95f-b64eae66dd34"
cdnsite = sp.stacks().get(stackid).cdnsites().get(cdnsiteid).enable()
cdnsite = cdnsite.disable()

Purge a cdn resource and check the purge status.

purge_request_id = sp.stacks().get(stackid).purge(
    [
        {
            "url": "https://example.com/resource/", # required
            "recursive": True,
            "invalidateOnly": False,
            "purgeAllDynamic": False,
            "headers": [],
            "purgeSelector": [],
        }
    ]
)

## purge_status can be used to check the status of the requested purge.
## Progress is represented as a decimal between 0 and 1, correlating to a
## percentage.

progress = sp.stacks().get(stackid).purge_status(purge_request_id)
print(progress)
##>> 1

Get metrics for a stack:

metrics_response1 = sp.stacks().get(stackid).metrics().get()

## Python datetime objects can be used to specify a date range, and the call
## allows a granularity to be specified. If no values are provided, the search range
## defaults to the last 24 hours, with 1 day granularity. See API doc for options.
## https://developer.stackpath.com/en/api/cdn/#operation/GetMetrics
## There is also a "platforms" argument which takes an array of "platform" codes.
## However the dev guide linked below only lists "CDE" which tracks egress traffic, which is
## how Stackpath bills for CDN usage, and "CDO" which is ingest traffic from the
## origin host, but is not billed. Since "CDE" tracks billable usage, it is the
## default.
## https://developer.stackpath.com/docs/en/cdn/getting-stack-metrics/
from datetime import datetime, timedelta
end = datetime.today()
start = end - timedelta(days=7)
metrics_response2 = sp.stacks().get(stackid).metrics().get(granularity="PT1H",\
  platforms = ["CDO", "CDE"], start_datetime_object = start, end_datetime_object = end)

Retrieve all certificates from a stack:

certificate_response = sp.stacks().get(stackid).certificates()

 ##The object returned will have a 'results' attribute containing
 ##an array of the available certificates:
 first_certificate = certificate_response['results'][0]
 second_certificate = certificate_response['results'][1]

Add, update and delete a certificate for a stack:

##The cert and key are required, CA bundle is optional.
new_cert_response = sp.stacks().get(stackid).certificates().add(\
SERVER_CERTIFICATE_STRING, PRIVATE_KEY_STRING, CA_BUNDLE_STRING)
##The new cert ID can be retrieved from the returned object:
cert_id = new_cert_response.id

##To update a cert:
sp.stacks().get(stackid).certificates().update(cert_id, UPDATED_CERT_STRING, UPDATED_KEY_STRING)

##To delete a cert:
delete_cert_response = sp.stacks().get(stackid).certificates().delete(cert_id)

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

pystackpath-0.5.0.tar.gz (15.0 kB view details)

Uploaded Source

Built Distribution

pystackpath-0.5.0-py3-none-any.whl (36.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pystackpath-0.5.0.tar.gz
  • Upload date:
  • Size: 15.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/3.9.0

File hashes

Hashes for pystackpath-0.5.0.tar.gz
Algorithm Hash digest
SHA256 bc44f4fc42976efbc66edde121a78adb15d9428b7bb672e8d5034accc850ab45
MD5 cfc9620ffac8af7ec09496ad0be5c653
BLAKE2b-256 3a07eabb46f3e31a00a97fd6680bb6f9b4b2cf0326ced6d04beef4f0ee4b3578

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pystackpath-0.5.0-py3-none-any.whl
  • Upload date:
  • Size: 36.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/3.9.0

File hashes

Hashes for pystackpath-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e991cd81aac4d4b3e9b8924c1dd118da123fbfb8261f344008bab6f224f09575
MD5 1b7a862b98814b41bdad78f65682ec07
BLAKE2b-256 11e032d8b2bd263e3ac6cacdd27f8b4b735c806b41c889a13ba589528e906d34

See more details on using hashes here.

Supported by

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