Skip to main content

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).

Build Status codecov Code style: black PyPI - Python Version PyPI - License PyPI - Status PyPI - Downloads

Inspired by the other library.

Goals:

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

python-gtmetrix2-0.0.22.tar.gz (9.7 kB view hashes)

Uploaded Source

Built Distribution

python_gtmetrix2-0.0.22-py3-none-any.whl (7.5 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page