Skip to main content

API client for Hyp

Project description

hyp_python_client

Python API client for Hyp.

Installation

pip install hyp-python-client

Usage

The most basic usage of the client is to call the Hyp API endpoints for assignment of a participant into an experiment variant and for conversion of a participant in a particular experiment:

from hyp_client.v1 import HypClient

# This is a real access token for a dummy data account on Hyp. Feel free to use
# it to test out the API.
client = HypClient(access_token="PRODUCTION/HYP/5ab8d3d8-6eca-4e11-9203-1b64faea1f33")
client.assignment(participant_id="fuzzybear", experiment_id=8)
# {'payload': {'variant_id': 18, 'variant_name': 'v2'}, 'message': 'success', 'status_code': 200}

client.conversion(participant_id="fuzzybear", experiment_id=8)
# {'payload': {'converted': True}, 'message': 'success', 'status_code': 200}

# No experiment found
client.assignment(participant_id="fuzzybear", experiment_id=13)
# {'payload': '', 'message': 'No experiment with ID 13 was found.', 'status_code': 404}

client.conversion(participant_id="fuzzybear", experiment_id=13)
# {'payload': '', 'message': 'No variant assignment for participant fuzzybear in experiment 8 was found. Participants must be assigned to a variant before conversion can be recorded.', 'status_code': 404}

The client also provides convenience methods for the common task of calling the API, parsing the response, and handling errors. For example in your application you could write code like the following:

from hyp_client.v1 import HypClient

client = HypClient("PRODUCTION/HYP/5ab8d3d8-6eca-4e11-9203-1b64faea1f33")
response = client.assignment(participant_id="fuzzybear", experiment_id=8)
variant = "Red button color"

if response["message"] == "success":
  variant = response["payload"]["variant_name"]

To avoid this boilerplate you can use the try_assignment and try_conversion methods:

variant = self.client.try_assignment(participant_id="fuzzybear", experiment_id=8, fallback="some variant name")
# API call was successful, `variant` is "v2"

variant = self.client.try_assignment(participant_id="fuzzybear", experiment_id=13, fallback="some variant name")
# API call was unsuccessful because there is no experiment with ID 13.
# `variant` is "some variant name"

converted = self.client.try_conversion(participant_id="fuzzybear", experiment_id=8)
# API call was successful, `converted` is True

converted = self.client.try_conversion(participant_id="fuzzybear", experiment_id=13)
# API call was unsuccessful both because there is no experiment with ID 13 and
# there is no assignment for "fuzzybear" in that experiment.
# `converted` is False.

These methods will call the API and return the relevant result if the API call was successful. In the case of try_assignment the result is the name of the variant. In the case of try_conversion the result is the boolean True, confirming that the participant was marked as converted for that experiment.

If the API call returns a non-200 response code for some reason try_assignment will return the provided fallback and try_conversion will return False.

Regardless of whether or not the API call is successful, these methods also log out info about what has happened. They use a logger named "hyp_python_client" and log out at the info level if the API call is successful and at the warning level if the call was unsuccessful. The log messages will pass include any error message from the server and look like this:

variant = self.client.try_assignment(participant_id="fuzzybear", experiment_id=8, fallback="some variant name")
# => assignment successful for participant fuzzybear in experiment 8.

variant = self.client.try_assignment(participant_id="fuzzybear", experiment_id=13, fallback="some variant name")
# => assignment failed for participant sillybear in experiment 888. Error: No experiment with ID 13 was found. Returning fallback some variant name.

# Missing data:
variant = self.client.try_assignment(participant_id=None, experiment_id=None, fallback="some variant name")
# => assignment failed due to missing participant ID and experiment ID. Returning fallback some variant name.

converted = self.client.try_conversion(participant_id="fuzzybear", experiment_id=8)
# => conversion successful for participant fuzzybear in experiment 8.

converted = self.client.try_conversion(participant_id="fuzzybear", experiment_id=13)
# => conversion failed for participant sillybear in experiment 13. Error: No variant assignment for participant sillybear in experiment 888 was found. Participants must be assigned to a variant before conversion can be recorded. Returning fallback False.

converted = self.client.try_conversion(participant_id="fuzzybear", experiment_id=None)
# => conversion failed due to missing experiment ID. Returning fallback False.

Developing and running the tests

We recommend using a virtual environment to isolate your installed packages.

python -m pip install -r requirements.txt
python -m unittest discover tests

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

hyp_python_client-0.0.5.tar.gz (4.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

hyp_python_client-0.0.5-py3-none-any.whl (4.6 kB view details)

Uploaded Python 3

File details

Details for the file hyp_python_client-0.0.5.tar.gz.

File metadata

  • Download URL: hyp_python_client-0.0.5.tar.gz
  • Upload date:
  • Size: 4.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.12

File hashes

Hashes for hyp_python_client-0.0.5.tar.gz
Algorithm Hash digest
SHA256 057b2565c13479c7f5881737223a33ad40d6bc9d0ad0e858debd0b9310e6314f
MD5 fa3dcf194941e52480ff05625f02825b
BLAKE2b-256 7ac9ca2a976d5bd1345bb8dfb47eb5d0e89e5d60df705b6cd636e97e4d686941

See more details on using hashes here.

File details

Details for the file hyp_python_client-0.0.5-py3-none-any.whl.

File metadata

  • Download URL: hyp_python_client-0.0.5-py3-none-any.whl
  • Upload date:
  • Size: 4.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.12

File hashes

Hashes for hyp_python_client-0.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 e3ebbdc3ca488a8a9d922010a8258926267606df084fdbb1d3caea5345ddd81b
MD5 ce2a9c35b426659ab0e5205504e65f58
BLAKE2b-256 7a31244b47a37204863994d90af3a9b15682982c584d6ff4e6f3c37367fd2f34

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