Skip to main content

trustpilot api client including cli tool

Project description

Build Status Latest Version Python Support

Python HTTP client for Trustpilot.

Features

  • Extends the `requests.Session <http://docs.python-requests.org/en/master/api/#requests.Session>`__ class with automatic authentication for public and private endpoints

  • GET, POST, PUT, DELETE, HEAD, OPTIONS and PATCH methods are exposed on module level

  • Implements session factory and default singleton session

  • Provides a simple hook system

  • CLI tool with basic HTTP commands

Installation

Install the package from PyPI using pip:

pip install trustpilot

Getting Started

This client is using the Requests library. Responses are standard `requests.Response <http://docs.python-requests.org/en/master/api/#requests.Response>`__ objects. You can use it as a factory or as a singleton.

Use the singleton session

Use the built-in default session to instantiate a globally accessible session.

from trustpilot import client
client.default_session.setup(
    api_host="https://api.trustpilot.com",
    api_version="v1",
    api_key="YOUR_API_KEY",
    api_secret="YOUR_API_SECRET",
    username="YOUR_TRUSTPILOT_BUSINESS_USERNAME",
    password="YOUR_TRUSTPILOT_BUSINESS_PASSWORD"
)
response = client.get("/foo/bar")

You can rely on environment variables for the setup of sessions so

$ env
TRUSTPILOT_API_HOST=foobar.com
TRUSTPILOT_API_VERSION=v1
TRUSTPILOT_API_KEY=foo
TRUSTPILOT_API_SECRET=bar
TRUSTPILOT_USERNAME=username
TRUSTPILOT_PASSWORD=password

Will work with the implicit default_session and the TrustpilotSession.setup method.

from trustpilot import client
client.get("/foo/bar")

Instantiate your own session

You can create as many sessions as you like, as long as you pass them around yourself.

from trustpilot import client
session = client.TrustpilotSession(
    api_host="https://api.trustpilot.com",
    api_version="v1",
    api_key="YOUR_API_KEY",
    api_secret="YOUR_API_SECRET",
    username="YOUR_TRUSTPILOT_BUSINESS_USERNAME",
    password="YOUR_TRUSTPILOT_BUSINESS_PASSWORD"
)
response = session.get("/foo/bar")

Async client

Since version 3.0.0 you are able to use the async_client for asyncio usecases.

To use the default async_client session, using env-vars for settings, import is as following:

import asyncio
from trustpilot import async_client
loop = asyncio.get_event_loop()

async def get_response():
    response = await async_client.get('/foo/bar')
    response_json = await response.json()

loop.run_until_complete(get_response())

Or instantiate the session yourself with:

import asyncio
from trustpilot import async_client
loop = asyncio.get_event_loop()

session = async_client.TrustpilotAsyncSession(
    api_host="https://api.trustpilot.com",
    api_version="v1",
    api_key="YOUR_API_KEY",
    api_secret="YOUR_API_SECRET",
    username="YOUR_TRUSTPILOT_BUSINESS_USERNAME",
    password="YOUR_TRUSTPILOT_BUSINESS_PASSWORD"
)

async def get_response():
    response = await session.get('/foo/bar')
    response_json = await response.json()

loop.run_until_complete(get_response())

CLI

A command line tool trustpilot_api_client is bundled with the module. To invoke it, use:

Usage: trustpilot_api_client [OPTIONS] COMMAND [ARGS]...

Options:
  --host TEXT               host name
  --version TEST            api version
  --key TEXT                api key
  --secret TEXT             api secret
  --token_issuer_host TEXT  token issuer host name
  --username TEXT           Trustpilot username
  --password TEXT           Trustpilot password
  -c TEXT                   json config file name
  -v, --verbose             verbosity level
  --help                    Show this message and exit.

Commands:
  create_access_token  Get an access token
  delete               Send a DELETE request
  get                  Send a GET request
  post                 Send a POST request with specified data
  put                  Send a PUT request with specified data

In order to use the -c option please supply the filename of a JSON in the following format:

{
  "TRUSTPILOT_API_HOST": "foo",
  "TRUSTPILOT_API_VERSION": "v1",
  "TRUSTPILOT_API_KEY": "bar",
  "TRUSTPILOT_API_SECRET": "baz",
  "TRUSTPILOT_USERNAME": "username",
  "TRUSTPILOT_PASSWORD": "password"
}

Tests

You can use pytest to run tests against your current Python version.

See `setup.py <setup.py>`__ for test dependencies.

History

0.1.0 (2016-11-09)

  • First release on gemfury

0.1.1 (2016-11-09)

  • change names

0.1.2 (2016-11-09)

  • fix issue with 401-retry

0.1.3 (2016-11-10)

  • add dependencies to setup.py

0.1.4 (2016-11-11)

  • cli tool

0.1.5 (2016-11-11)

  • fix apikey url query param error

0.1.6 (2016-11-11)

  • introduce different token_issuer_host thatn api_host

0.1.7 (2016-12-06)

  • Introduce context_getter on session object, defaulted to holding CorrelationId=random_uuid

1.0.0 (2017-02-01)

  • first release as oss, major refactoring of inner machinery (session objects, retry policies, cli, tests etc)

1.1.0 (2017-06-12)

2.0.0 (2017-09-29)

  • DEPRECATED: create_session is getting deprecated, use trustpilot.client.default_session.setup instead

  • now able to query public endpoints without being authenticated

2.1.0 (2017-10-05)

  • fixed issue in cli.post & cli.put where ‘content_type’ should be ‘content-type’

3.0.0 (2018-01-18)

DELETED DO NOT USE!!

  • add async-client

3.0.1 (2018-01-18)

  • removed prints

  • made async_client retry on unauthorized

4.0.0 (2018-06-06)

  • drop support for Python 3.3

4.0.1 (2018-06-06)

  • Switch to non-deprecated session object for utility method calls

4.0.2 (2018-10-30)

  • Upgrade requests to 2.20.0

5.0.0 (2019-01-04)

  • Update to authentication methods

5.0.1 (2019-02-04)

  • Fix documentation formatting

6.0.0 (2019-02-06)

  • reorganize code

  • add user-agent header

  • get access_token with async call in async_client

6.0.4 (2019-08-15)

  • Added support for ‘API Version’ parameter for Client initialisation.

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

trustpilot-6.0.4.tar.gz (14.6 kB view details)

Uploaded Source

File details

Details for the file trustpilot-6.0.4.tar.gz.

File metadata

  • Download URL: trustpilot-6.0.4.tar.gz
  • Upload date:
  • Size: 14.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.33.0 CPython/3.7.3

File hashes

Hashes for trustpilot-6.0.4.tar.gz
Algorithm Hash digest
SHA256 7057ce6785d858313b45451ace5907f158d82915820bfc3c178597ca99c4ae74
MD5 64670b96aff490ef7d7399e1fe621de0
BLAKE2b-256 740c3d1249d96555724409512b70dbed7ca5a8cd623def72275942ea0bce8ea6

See more details on using hashes here.

Supported by

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