Jump to >

rbtools.api.cache

Functions

clear_cache([cache_path])

Delete the HTTP cache used for the API.

Classes

APICache([create_db_in_memory, db_location, ...])

An API cache backed by a SQLite database.

CacheEntry(url, vary_headers, max_age, etag, ...)

An entry in the API Cache.

CachedHTTPResponse(cache_entry)

A response returned from the APICache.

LiveHTTPResponse(response)

An uncached HTTP response that can be read() more than once.

class rbtools.api.cache.CacheEntry(url: str, vary_headers: Dict[str, str], max_age: int, etag: str, local_date: datetime, last_modified: str, mime_type: str, item_mime_type: str, response_body: bytes)[source]

Bases: object

An entry in the API Cache.

DATE_FORMAT = '%Y-%m-%dT%H:%M:%S'[source]
__init__(url: str, vary_headers: Dict[str, str], max_age: int, etag: str, local_date: datetime, last_modified: str, mime_type: str, item_mime_type: str, response_body: bytes) None[source]

Create a new cache entry.

Parameters:
  • url (str) – The URL of the entry.

  • vary_headers (dict) – The resource’s Vary header.

  • max_age (int) – The resource’s maximum cache time, in seconds.

  • etag (str) – The resource’s ETag.

  • local_date (datetime.datetime) – The local time when the cached resource was fetched.

  • last_modified (str) – The last modified date provided by the server.

  • mime_type (str) – The resource’s Content-Type.

  • item_mime_type (str) – The resource’s Item-Content-Type.

  • response_body (bytes) – The cached response body.

matches_request(request: Request) bool[source]

Determine if the cache entry matches the given request.

This is done by comparing the value of the headers field to the headers in the request.

Parameters:

request (urllib.request.Request) – The HTTP request to compare against.

Returns:

True if the cache entry matches the request. False otherwise.

Return type:

bool

up_to_date() bool[source]

Determine if the cache entry is up to date.

Returns:

True if the cache entry is still valid. False, otherwise.

Return type:

bool

class rbtools.api.cache.LiveHTTPResponse(response: HTTPResponse)[source]

Bases: object

An uncached HTTP response that can be read() more than once.

This is intended to be API-compatible with an http.client.HTTPResponse object. This allows a response to be read more than once.

__init__(response: HTTPResponse) None[source]

Initialize the response.

This will extract the data from the http.client response and store it.

Parameters:

response (http.client.HTTPResponse) – The response from the server.

property code: int[source]

The HTTP response code.

Type:

int

info() Message[source]

Return the headers associated with the response.

Deprecated since version 4.0: Deprecated in favor of the headers attribute.

Returns:

The response headers.

Return type:

email.message.Message

read() bytes[source]

Return the content associated with the response.

Returns:

The response content.

Return type:

bytes

getcode() int[source]

Return the associated HTTP response code.

Deprecated since version 4.0: Deprecated in favor of the code attribute.

Returns:

The HTTP response code.

Return type:

int

class rbtools.api.cache.CachedHTTPResponse(cache_entry: CacheEntry)[source]

Bases: object

A response returned from the APICache.

This is intended to be API-compatible with a urllib response object.

__init__(cache_entry: CacheEntry) None[source]

Initialize the response.

Parameters:

cache_entry (CacheEntry) – The cached data.

property code: int[source]

The HTTP response code.

Type:

int

info() dict[source]

Return the headers associated with the response.

Deprecated since version 4.0: Deprecated in favor of the headers attribute.

Returns:

The cached response headers.

Return type:

dict

read() bytes[source]

Return the content associated with the response.

Returns:

The cached response content.

Return type:

bytes

getcode() int[source]

Return the associated HTTP response code, which is always 200.

Deprecated since version 4.0: Deprecated in favor of the code attribute.

Returns:

200, always. This pretends that the response is the successful result of an HTTP request.

Return type:

int

class rbtools.api.cache.APICache(create_db_in_memory: bool = False, db_location: Optional[str] = None, urlopen: Optional[Callable] = None)[source]

Bases: object

An API cache backed by a SQLite database.

EXPIRES_FORMAT = '%a, %d %b %Y %H:%M:%S %Z'[source]
DEFAULT_CACHE_DIR = '/Users/chipx86/Library/Caches/rbtools'[source]
DEFAULT_CACHE_PATH = '/Users/chipx86/Library/Caches/rbtools/apicache.db'[source]
SCHEMA_VERSION = 2[source]
__init__(create_db_in_memory: bool = False, db_location: Optional[str] = None, urlopen: Optional[Callable] = None) None[source]

Create a new instance of the APICache

If the db_path is provided, it will be used as the path to the SQLite database; otherwise, the default cache (in the CACHE_DIR) will be used. The urlopen parameter determines the method that is used to open URLs.

Changed in version 4.0: Deprecated the urlopen parameter.

Parameters:
  • create_db_in_memory (bool, optional) – Whether to store the API cache in memory, or persist to disk.

  • db_location (str) – The filename of the cache database, if using.

  • urlopen (callable) – The method to call for urlopen. This parameter has been deprecated.

Raises:

CacheError – The database exists but the schema could not be read.

make_request(request: Request) Union[LiveHTTPResponse, CachedHTTPResponse][source]

Perform the specified request.

If there is an up-to-date cached entry in our store, a CachedResponse will be returned. Otherwise, The urlopen method will be used to execute the request and a CachedResponse (if our entry is still up to date) or a Response (if it is not) will be returned.

Parameters:

request (urllib.request.Request) – The HTTP request to perform.

Returns:

The response object.

Return type:

LiveHTTPResponse or CachedHTTPResponse

rbtools.api.cache.clear_cache(cache_path: str = '/Users/chipx86/Library/Caches/rbtools/apicache.db') bool[source]

Delete the HTTP cache used for the API.

Parameters:

cache_path (str) – The path of the cache database.

Returns:

True if the operation succeeded. False, otherwise.

Return type:

bool