Skip to main content

A Python client library for GTmetrix REST API v2.0

Project description

python-gtmetrix2 is a Python client library for GTmetrix REST API v2.0 (hence 2 in the name).

Build Status codecov Code style: black Documentation Status

PyPI - Latest Version PyPI - Python Version PyPI - Downloads License: MIT

Inspired by the library with a similar name.

Installation:

via pip

pip install python-gtmetrix2

file copy

This library has zero dependencies and is contained within a single file, so you can just save this file as python_gtmetrix2.py in your project directory and import it

Usage:

Simplest example:

import json
import python_gtmetrix2
api_key = "e8ddc55d93eb0e8281b255ea236dcc4f"    # your API key
interface = python_gtmetrix2.Interface(api_key) # init
test = interface.start_test(url)                # start test
test.fetch(wait_for_completion=True)            # wait for it to finish
report = test.getreport()                       # get test result
print(json.dumps(report, indent=2))             # do something useful with it

For an explanation of the above lines, dive into the docs

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.1.1.tar.gz (14.1 kB view hashes)

Uploaded Source

Built Distribution

python_gtmetrix2-0.1.1-py3-none-any.whl (12.4 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