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.4.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.4-py3-none-any.whl (4.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: hyp_python_client-0.0.4.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.4.tar.gz
Algorithm Hash digest
SHA256 35f46c652a0ecc35b448cd6617d29fb460e46985e303d2fad850d6100c6075a7
MD5 4fee0f770efd095a7987de1c8e94b7e4
BLAKE2b-256 9f805f761ac02d63cd7f28e855e4d67dd390135d2a32a3456235f7f458b8a5f2

See more details on using hashes here.

File details

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

File metadata

  • Download URL: hyp_python_client-0.0.4-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.4-py3-none-any.whl
Algorithm Hash digest
SHA256 6c03f781cda9c277b56cac7f5160a7b6303291302d43f30d92ffb41d37a7895a
MD5 e7be73efba0e54ea2a407b0aeeb24bdc
BLAKE2b-256 e277539d388d93dc586329a703c119464ff34db8a13302123925337e00d0ecd3

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