A Python wrapper for the Permutive API.
Project description
PermutiveAPI
PermutiveAPI is a Python module to interact with the Permutive API. It provides a set of classes and methods to manage users, imports, cohorts, and workspaces within the Permutive ecosystem.
Table of Contents
Installation
You can install the PermutiveAPI module using pip:
pip install PermutiveAPI --upgrade
Configuration
Before using the library, you need to configure your credentials.
- Copy the environment file:
cp _env .env
- Set your credentials path:
Edit the
.envfile and set thePERMUTIVE_APPLICATION_CREDENTIALSenvironment variable to the absolute path of your workspace JSON file.PERMUTIVE_APPLICATION_CREDENTIALS="/absolute/path/to/your/workspace.json"
The workspace credentials JSON can be downloaded from the Permutive dashboard under Settings → API keys. Save the file somewhere secure. The apiKey inside this JSON is used to authenticate API calls.
Usage
Importing the Module
To use the PermutiveAPI module, import the necessary classes. The main classes are exposed at the top level of the PermutiveAPI package:
from PermutiveAPI import (
Alias,
Cohort,
Identity,
Import,
Segment,
Source,
Workspace,
)
Managing Workspaces
The Workspace class is the main entry point for interacting with your Permutive workspace.
# Create a workspace instance
workspace = Workspace(
name="Main",
organisation_id="your-org-id",
workspace_id="your-workspace-id",
api_key="your-api-key",
)
# List all cohorts in a workspace (includes child workspaces)
all_cohorts = workspace.cohorts()
for cohort in all_cohorts:
print(f"Cohort ID: {cohort.id}, Name: {cohort.name}")
# List all imports in a workspace
all_imports = workspace.imports()
for imp in all_imports:
print(f"Import ID: {imp.id}, Name: {imp.name}")
# List segments for a specific import
segments_in_import = workspace.segments(import_id="your-import-id")
for segment in segments_in_import:
print(f"Segment ID: {segment.id}, Name: {segment.name}")
Managing Cohorts
You can create, retrieve, and list cohorts using the Cohort class.
# List all cohorts
all_cohorts = Cohort.list(api_key="your_api_key")
print(f"Found {len(all_cohorts)} cohorts.")
# Get a specific cohort by ID
cohort_id = "your-cohort-id"
cohort = Cohort.get_by_id(id=cohort_id, api_key="your_api_key")
print(f"Retrieved cohort: {cohort.name}")
# Create a new cohort
new_cohort = Cohort(
name="High-Value Customers",
query={"type": "segment", "id": "segment-id-for-high-value-customers"}
)
new_cohort.create(api_key="your_api_key")
print(f"Created cohort with ID: {new_cohort.id}")
Managing Segments
The Segment class allows you to interact with audience segments.
# List all segments for a given import
import_id = "your-import-id"
segments = Segment.list(api_key="your_api_key", import_id=import_id)
print(f"Found {len(segments)} segments in import {import_id}.")
# Get a specific segment by ID
segment_id = "your-segment-id"
segment = Segment.get_by_id(import_id=import_id, segment_id=segment_id, api_key="your_api_key")
print(f"Retrieved segment: {segment.name}")
Managing Imports
You can list and retrieve imports using the Import class.
# List all imports
all_imports = Import.list(api_key="your_api_key")
for imp in all_imports:
print(f"Import ID: {imp.id}, Code: {imp.code}, Source Type: {imp.source.type}")
# Get a specific import by ID
import_id = "your-import-id"
import_instance = Import.get_by_id(id=import_id, api_key="your_api_key")
print(f"Retrieved import: {import_instance.id}, Source Type: {import_instance.source.type}")
Managing Users
The Identity and Alias classes are used to manage user profiles.
# Create an alias for a user
alias = Alias(id="user@example.com", tag="email", priority=1)
# Create an identity for the user
identity = Identity(user_id="internal-user-id-123", aliases=[alias])
# Send the identity information to Permutive
try:
identity.identify(api_key="your-api-key")
print("Successfully identified user.")
except Exception as e:
print(f"Error identifying user: {e}")
### Error Handling
The package raises purpose-specific exceptions that are also available at the
top level of the package for convenience:
```python
from PermutiveAPI import (
PermutiveAPIError,
PermutiveAuthenticationError,
PermutiveBadRequestError,
PermutiveRateLimitError,
PermutiveResourceNotFoundError,
PermutiveServerError,
)
try:
# make an API call via the high-level classes
Cohort.list(api_key="your_api_key")
except PermutiveBadRequestError as e:
# e.status, e.url, and e.response are available for debugging
print(e.status, e.url, e)
except PermutiveAPIError as e:
print("Unhandled API error:", e)
## Development
To set up a development environment, install the required dependencies:
```sh
pip install -r requirements-dev.txt
Running Tests
Before committing any changes, please run the following checks to ensure code quality and correctness.
Style Checks:
pydocstyle PermutiveAPI
black --check .
Static Type Analysis:
pyright PermutiveAPI
Unit Tests and Coverage:
pytest -q --cov=PermutiveAPI --cov-report=term-missing --cov-fail-under=70
All checks must pass before a pull request can be merged.
Contributing
Contributions are welcome! Please see CONTRIBUTING.md for development setup and pull request guidelines.
License
This project is licensed under the MIT License. See the LICENSE file for details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file permutiveapi-5.3.0.tar.gz.
File metadata
- Download URL: permutiveapi-5.3.0.tar.gz
- Upload date:
- Size: 30.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.9.23
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
608bbddfe67de4a5974be755215aa6da0e4c009e328e5fe15b9e3da2c8aea6fe
|
|
| MD5 |
d9a5382031ff4b5faddfe2e2f8d425a6
|
|
| BLAKE2b-256 |
597866a1618145fd8aa061de1fb38dba449e782c2f0d7b603699dbf4dc808d8f
|
File details
Details for the file permutiveapi-5.3.0-py3-none-any.whl.
File metadata
- Download URL: permutiveapi-5.3.0-py3-none-any.whl
- Upload date:
- Size: 28.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.9.23
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
88b49cba5cb0cae92daa1d3cf5876af2279200b5d779ad603c0b61b81df41502
|
|
| MD5 |
15c72fdcb3eb9d26b28dcdc567334ad3
|
|
| BLAKE2b-256 |
4b39d60ae91658e9813326305ada6445a770dc18f4a3fb8ec151b63f53398085
|