internal details

Warning

Contents of this module considered to be internal and therefore may change without warning. If your code uses something in this module, please let the developer know about it so we could consider adding it to public API.

Overview

All requests are made by the instance of Requestor which is created by the Account class and is usually shared between all instances created from it.

It also uses NoRedirect to avoid redirections when API returns both 30x redirect code and actual data in response body. It is important, for example, when requesting data for a finished test (Test.fetch()) - in that case, we would like to store received data in the Test object, but by default Python will throw away received data and follow redirect.

There are four helper functions to check if received JSON represents a valid error, test, report, user.

Test and Report classes have a same parent class Object, which holds elements that are common to both of them (basically, constructor and parent class)

Reference

class python_gtmetrix2._internals.NoRedirect[source]

Bases: urllib.request.HTTPRedirectHandler

Helper class for avoiding redirection on 30x responses. From https://stackoverflow.com/a/52086806

redirect_request(req, fp, code, msg, headers, newurl)[source]

Returns None to avoid redirection.

class python_gtmetrix2._internals.Requestor(api_key, base_url='https://gtmetrix.com/api/2.0/', sleep_function=<built-in function sleep>)[source]

Bases: object

Class for making requests.

It also manages authentication, optionally follows redirects, and retries on “429” responses.

Note that usually objects of this class should not be instantiated directly - you can use methods of Account class instead.

Parameters are the same as for Account

request(url, follow_redirects=False, data=None, **kwargs)[source]

Make a request and return the response.

Parameters
  • url (str) – URL to request (base URL will be prepended)

  • follow_redirects (bool, optional) – Whether to follow 30x redirects, defaults to False.

  • data (dict (to be JSON-encoded), string or bytes, optional) – data to send as request body (usually with a POST request), defaults to None

  • method (str, optional) – method to use for request (“GET”, “POST”, etc.), defaults to None to let urllib to decide (POST if data is provided, GET otherwise)

  • headers (dict, optional) – headers to send with the request, in format understood by urllib, defaults to {}

  • retries (int, optional) – Number of times to retry on “429 Rate limit exceeded” responses, defaults to 10

  • return_data (bool, optional) – whether this function should read() the response, parse it as JSON, validate it (check that it’s a dict and has a “data” key), and return that JSON - or if it should let the caller deal with it. Latter is useful for API calls which return files instead of JSON.

Returns

Tuple of 2 elements: response object and response data. When API returns HTTP status code in [200..299] range, “response object” is an instance of http.client.HTTPResponse. However, when API returns code 30x, Python considers it an error, so “response object” is an instance of urllib.error.HTTPError instead. Second element of the returned tuple is parsed JSON (dict), unless return_data parameter was False. In latter case, it’s responsibility of the caller to call read method on the response object (conveniently, both types of returned objects support it).

Return type

tuple(http.client.HTTPResponse or urllib.error.HTTPError, dict or None)

python_gtmetrix2._internals.dict_is_error(data)[source]

helper function to check whether passed argument is a proper dict object describing an error.

Parameters

data (dict) – value to check

Return type

bool

python_gtmetrix2._internals.dict_is_test(data)[source]

helper function to check whether passed argument is a proper dict object describing a test.

Parameters

data (dict) – value to check

Return type

bool

python_gtmetrix2._internals.dict_is_report(data)[source]

helper function to check whether passed argument is a proper dict object describing a report.

Parameters

data (dict) – value to check

Return type

bool

python_gtmetrix2._internals.dict_is_user(data)[source]

helper function to check whether passed argument is a proper dict object describing a user.

Parameters

data (dict) – value to check

Return type

bool

class python_gtmetrix2._internals.Object(requestor, data, sleep_function=<built-in function sleep>)[source]

Bases: dict

Base class for Test and Report classes.

Note that usually objects of these classes should not be instantiated directly - you can use methods of Account class instead.

Also note that since they are descendants of the dict, you can simply json.dumps() them to inspect their internals.

Parameters
  • requestor (Requestor) – Requestor object to use for requests made by this object

  • data (dict) – initial data. Note that it is responsibility of the caller to ensure that it contains valid data (passes respective dict_is_* check).

  • sleep_function (method, optional) – the function to execute when waiting between retries (after receiving a “429” response) - useful for testing, or if someone wants to add some logging or notification of a delayed request, defaults to time.sleep()