Introduction¶
Warning
As long as major version of this library is 0 (i.e. library has version 0.x.y), API is not guaranteed to be compatible between versions. When you start using this library, please let the developer know about - I will bump the major version to 1, and usuall semver guatantees regarding version compatibility will be applied.
Account¶
Main entry point for this library is Account
class which is
initialized like this:
account = python_gtmetrix2.Account(api_key)
where api_key
is your GTmetrix API key.
Object of this class lets you start tests, like this:
test = account.start_test(url)
where url
is the url you want to test. Optionally, you can pass extra
arguments, like this:
test = account.start_test(url, report='none', adblock=1)
Full list of available parameters is available in GTmetrix API documentation, section “Test
Parameters”. This call returns an object of type Test
. Note that
this call does not wait for the test to finish. To know how to wait for
the test to finish, read on.
Test¶
Object of type Test
has two methods which you will be using:
fetch()
and getreport()
. Method
fetch()
updates test information from GTmetrix API server
and has an optional argument wait_for_completion
, which, when set to
True
, instructs this method to wait until the test finishes.
If the test completes successfully (which happens most of the time), you can
use getreport()
method to retrieve test results in the
form of Report
object, like this:
test.fetch(wait_for_completion=True)
report = test.getreport()
Note that report
might be None
if test did not finish successfully (for
example, due to connection or certificate error).
Report¶
Report
is a descendant of dict
, so you can treat it like one:
print(json.dumps(report, indent=2))
Report also has getresource
method which lets you
save a report resource (like a PDF representation of the report, screenshot, or
a video of loading website) to file or a variable in your program:
report.getresource('report.pdf', 'report.pdf')
That’s all for now. More examples can be found in examples directory in the repo.