rbtools.api.cache¶
Caching implementation for the RBTools API.
Module Attributes
The minimum version of Review Board to allow HTTP caching. |
Functions
|
Delete the HTTP cache used for the API. |
Classes
|
An API cache backed by a SQLite database. |
|
An entry in the API Cache. |
|
A response returned from the APICache. |
|
An uncached HTTP response that can be read() more than once. |
- rbtools.api.cache.MINIMUM_VERSION = '2.0.14'[source]¶
The minimum version of Review Board to allow HTTP caching.
Versions prior to this had broken caching. If this version or older is encountered, caching will be disabled.
- 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.
- __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:
- 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.
- 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.
- class rbtools.api.cache.APICache(create_db_in_memory: bool = False, db_location: Optional[str] = None)[source]¶
Bases:
object
An API cache backed by a SQLite database.
- __init__(create_db_in_memory: bool = False, db_location: Optional[str] = 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:
- 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: