fastpurge

A client for the Akamai Fast Purge API.

Quick Start

Install the fastpurge client from PyPI:

pip install fastpurge

Authorize a client in the Akamai Control Center.

Place the client credentials in an edgerc file located at $HOME/.edgerc; this is an INI-style configuration file with the following structure:

[default]
host = <Base hostname without the scheme>
client_token = <Client token>
client_secret = <Secret>
access_token = <Access Token>

In your Python code, construct a FastPurgeClient and call the purge methods. Make sure to block on the returned Future if you want to wait for the purge to complete.

from fastpurge import FastPurgeClient
client = FastPurgeClient()

# start a purge of two URLs
future = client.purge_by_url(["https://example.com/resource1",
                              "https://example.com/resource2"])

# wait for the purge to complete (or raise if purge fails)
future.result()

Usage of $HOME/.edgerc for credentials is a convention supported by other clients in the Akamai ecosystem. If you’d like to configure the credentials separately, you can pass them to the client constructor explicitly, as in example:

from fastpurge import FastPurgeClient
client = FastPurgeClient(auth={
    # The entries from ~/.edgerc can be supplied directly here
    "host": "akaa-xxxxxxxxxxxxxxxx-xxxxxxxxxxxxxxxx.purge.akamaiapis.net",
    "client_token": "akab-xxxxxxxxxxxxxxxx-xxxxxxxxxxxxxxxx",
    "client_secret": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
    "access_token": "akab-xxxxxxxxxxxxxxxx-xxxxxxxxxxxxxxxx",
})

API Reference

class fastpurge.FastPurgeClient(auth=None, scheme='https', port=443)

A client for the Akamai Fast Purge API.

This class provides methods for purging content asynchronously.

Purge operations are represented by Future objects which are resolved when the purge completes, or raise an exception when the purge fails.

Though not mandatory, instances of this class may be used as a context manager, as in example:

>>> with FastPurgeClient() as client:
>>>     client.purge_urls(urls).result()

Resources will be reclaimed when the context is exited, and any outstanding purge requests will be cancelled or abandoned.

Parameters:
  • auth (str, dict) –

    The following values may be provided:

    dict:

    Must contain all authentication components from .edgerc: host, client_token, client_secret, access_token.

    str:

    Must be a path to an .edgerc file to load.

    None:

    Credentials are loaded from ~/.edgerc.

  • scheme (str) – Alternative scheme for requests (e.g. ‘http’).

  • port (int) – Alternative port for requests.

purge_objects(object_type, objects, network=None, purge_type=None)

Purge a collection of objects.

The Fast Purge API limits the number of objects purged in a single request. If the number of objects given exceeds the limit, this method may split the collection of objects into smaller lists and issue multiple requests.

Requests may be retried a few times on failure.

Parameters:
  • object_type (str) – type of object used for purge: “url”, “tag”, “cpcode”.

  • objects (list) – list of objects for purge (e.g. list of URLs, tags or cpcodes).

  • network (str) – “staging” or “production”. The default is “production”.

  • purge_type (str) – “delete” or “invalidate”. See fast purge concepts for more information regarding these purge types. The default is “delete”.

Returns:

a Future resolved when the purge completes or fails, with:

result:

If a purge succeeds, the result is a list of responses from the Fast Purge API (dict). See the fast purge resources for the expected fields in the response.

Typically, the result would contain only a single response object. Multiple responses are provided if the client split a large cache invalidation request into multiple smaller requests.

Warning: future resolution is based only on the estimated time to completion provided by the Fast Purge API. There is no guarantee that the purge has completed.

exception:

If purge(s) fail, an exception will be set; typically, though not always, an instance of FastPurgeError.

Return type:

Future of list

purge_by_cpcode(cpcodes, **kwargs)

Convenience method for purge_objects() with object_type=’cpcode’

purge_by_tag(tags, **kwargs)

Convenience method for purge_objects() with object_type=’tag’

purge_by_url(urls, **kwargs)

Convenience method for purge_objects() with object_type=’url’

class fastpurge.FastPurgeError

Raised when the Fast Purge API reports an error.

The exception message contains a summary of the error encountered.