A Python client library for GTmetrix REST API v2.0
Project description
python-gtmetrix2
python-gtmetrix2 is a Python client library for GTmetrix REST API v2.0 (hence 2 in the name).
Inspired by the other library.
Goals:
- CI
- 100% code coverage
- pip package
- drop-in compatibility with previous library
- 100% coverage of API
- fancy docs
Installation:
via pip
pip install python-gtmetrix2
file copy
Just save this file as python_gtmetrix2.py
in your project directory and
import python_gtmetrix2
Your favorite package manager
I wish...
Usage:
Simplest example:
def test_url(url, api_key):
""" returns GTmetrix grade (one letter from A to F) for a given URL """
interface = python_gtmetrix2.Interface(api_key) # init
test = interface.start_test(url) # start test
test.fetch(True) # wait for it to finish
report = test.getreport() # get test results
return report['attributes']['gtmetrix_grade'] # return one-letter result
These five lines are explained below.
Interface
Main entry point for this library is Interface
class which is initialized like this:
interface = python_gtmetrix2.Interface(api_key)
where api_key
is your GTmetrix API key.
Interface lets you start tests, like this:
test = interface.start_test(url)
where url
is the url you want to test. Optionally, you can pass extra arguments, like this:
test = interface.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.
You can also query for tests started within last 24 hours:
tests = interface.list_tests()
This call returns a list
of objects of type Test
.
Test
Object of type Test
has two useful methods: fetch
and getreport
.
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.
Like this:
test.fetch(True)
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:
report = test.getreport()
Note that report
might be None
if test did not finish successfully
(for example, due to connection or certificate error).
Report
For now, report doesn't provide any special functionality,
but you can access all its data.
It's basically a dict
containing all data returned by GTmetrix API.
You can consult all possible values in the docs.
More examples
are in examples directory.
Testing:
Autotests are automated by Travis in clouds, so to run them you can just create a PR.
To run tests locally, you need to install pytest with httpserver
and execute pytest
in the root of this repository like this:
~/git/python-gtmetrix2$ pytest tests
======================== test session starts =========================
platform linux -- Python 3.9.6, pytest-6.2.4, py-1.10.0, pluggy-0.13.1
rootdir: /home/lex/git/python-gtmetrix2
plugins: requests-mock-1.9.3, cov-2.12.1, httpserver-1.0.0
collected 15 items
tests/auto_test.py ............... [100%]
========================= 15 passed in 0.80s =========================
To measure coverage, install coverage and run it like this:
~/git/python-gtmetrix2$ coverage run -m pytest tests
Its output is same as above. To show actual coverage values, run:
~/git/python-gtmetrix2$ coverage report --skip-empty
Name Stmts Miss Cover
------------------------------------------------------
src/python_gtmetrix2/__init__.py 155 0 100%
tests/auto_test.py 227 0 100%
------------------------------------------------------
TOTAL 382 0 100%
To generate a coverage report in html format, run:
~/git/python-gtmetrix2$ coverage html --skip-empty
It will output nothing, but create nice HTML report in the htmlcov directory.
Tis project aims for 100% code coverage by tests, so just mark untested lines
with pragma: no cover
and be done with it, lol.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for python_gtmetrix2-0.0.22-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 322015dfc19be8b27a8a8d3f0b47bc6807df6fc68f4cd55498b52f169b4cae12 |
|
MD5 | 76bb8a226af056694348d5f1867bcc92 |
|
BLAKE2b-256 | 645f1b0312a981559065c59cd5f839b15988f90ca94edaa8bdfd6ba17493040a |