OnyxClient
Bases: OnyxClientBase
Class for querying and manipulating data within Onyx.
__init__(config)
¶
Initialise a client.
PARAMETER | DESCRIPTION |
---|---|
config |
TYPE:
|
Examples:
The recommended way to initialise a client (as a context manager):
import os
from onyx import OnyxConfig, OnyxEnv, OnyxClient
config = OnyxConfig(
domain=os.environ[OnyxEnv.DOMAIN],
token=os.environ[OnyxEnv.TOKEN],
)
with OnyxClient(config) as client:
pass # Do something with the client here
Alternatively, the client can be initialised as follows:
import os
from onyx import OnyxConfig, OnyxEnv, OnyxClient
config = OnyxConfig(
domain=os.environ[OnyxEnv.DOMAIN],
token=os.environ[OnyxEnv.TOKEN],
)
client = OnyxClient(config)
# Do something with the client here
Tips
- When making multiple requests, using the client as a context manager can improve performance.
- This is due to the fact that the client will re-use the same session for all requests, rather than creating a new session for each request.
- For more information, see: https://requests.readthedocs.io/en/master/user/advanced/#session-objects
projects()
¶
View available projects.
RETURNS | DESCRIPTION |
---|---|
List[Dict[str, str]]
|
List of projects. |
Examples:
types()
¶
View available field types.
RETURNS | DESCRIPTION |
---|---|
List[Dict[str, Any]]
|
List of field types. |
Examples:
import os
from onyx import OnyxConfig, OnyxEnv, OnyxClient
config = OnyxConfig(
domain=os.environ[OnyxEnv.DOMAIN],
token=os.environ[OnyxEnv.TOKEN],
)
with OnyxClient(config) as client:
field_types = client.types()
>>> field_types
[
{
"type": "text",
"description": "A string of characters.",
"lookups": [
"exact",
"ne",
"in",
"notin",
"contains",
"startswith",
"endswith",
"iexact",
"icontains",
"istartswith",
"iendswith",
"length",
"length__in",
"length__range",
"isnull",
],
},
{
"type": "choice",
"description": "A restricted set of options.",
"lookups": [
"exact",
"ne",
"in",
"notin",
"isnull",
],
},
]
lookups()
¶
View available lookups.
RETURNS | DESCRIPTION |
---|---|
List[Dict[str, Any]]
|
List of lookups. |
Examples:
import os
from onyx import OnyxConfig, OnyxEnv, OnyxClient
config = OnyxConfig(
domain=os.environ[OnyxEnv.DOMAIN],
token=os.environ[OnyxEnv.TOKEN],
)
with OnyxClient(config) as client:
lookups = client.lookups()
>>> lookups
[
{
"lookup": "exact",
"description": "The field's value must be equal to the query value.",
"types": [
"text",
"choice",
"integer",
"decimal",
"date",
"datetime",
"bool",
],
},
{
"lookup": "ne",
"description": "The field's value must not be equal to the query value.",
"types": [
"text",
"choice",
"integer",
"decimal",
"date",
"datetime",
"bool",
],
},
]
fields(project)
¶
View fields for a project.
PARAMETER | DESCRIPTION |
---|---|
project |
Name of the project.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Dict[str, Any]
|
Dict of fields. |
Examples:
import os
from onyx import OnyxConfig, OnyxEnv, OnyxClient
config = OnyxConfig(
domain=os.environ[OnyxEnv.DOMAIN],
token=os.environ[OnyxEnv.TOKEN],
)
with OnyxClient(config) as client:
fields = client.fields("project")
>>> fields
{
"version": "0.1.0",
"fields": {
"climb_id": {
"description": "Unique identifier for a project record in Onyx.",
"type": "text",
"required": True,
"actions": [
"get",
"list",
"filter",
],
"restrictions": [
"Max length: 12",
],
},
"is_published": {
"description": "Indicator for whether a project record has been published.",
"type": "bool",
"required": False,
"actions": [
"get",
"list",
"filter",
"add",
"change",
],
"default": True,
},
"published_date": {
"description": "The date the project record was published in Onyx.",
"type": "date (YYYY-MM-DD)",
"required": False,
"actions": [
"get",
"list",
"filter",
],
},
"country": {
"description": "Country of origin.",
"type": "choice",
"required": False,
"actions": [
"get",
"list",
"filter",
"add",
"change",
],
"values": [
"ENG",
"WALES",
"SCOT",
"NI",
],
},
},
}
choices(project, field)
¶
View choices for a field.
PARAMETER | DESCRIPTION |
---|---|
project |
Name of the project.
TYPE:
|
field |
Choice field on the project.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Dict[str, Dict[str, Any]]
|
Dictionary mapping choices to information about the choice. |
Examples:
get(project, climb_id=None, fields=None, include=None, exclude=None)
¶
Get a record from a project.
PARAMETER | DESCRIPTION |
---|---|
project |
Name of the project.
TYPE:
|
climb_id |
Unique identifier for the record in the project.
TYPE:
|
fields |
Dictionary of field filters used to uniquely identify the record.
TYPE:
|
include |
Fields to include in the output.
TYPE:
|
exclude |
Fields to exclude from the output.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Dict[str, Any]
|
Dict containing the record. |
Examples:
Get a record by CLIMB ID:
import os
from onyx import OnyxConfig, OnyxEnv, OnyxClient
config = OnyxConfig(
domain=os.environ[OnyxEnv.DOMAIN],
token=os.environ[OnyxEnv.TOKEN],
)
with OnyxClient(config) as client:
record = client.get("project", "C-1234567890")
>>> record
{
"climb_id": "C-1234567890",
"published_date": "2023-01-01",
"field1": "value1",
"field2": "value2",
}
Get a record by fields that uniquely identify it:
import os
from onyx import OnyxConfig, OnyxEnv, OnyxClient
config = OnyxConfig(
domain=os.environ[OnyxEnv.DOMAIN],
token=os.environ[OnyxEnv.TOKEN],
)
with OnyxClient(config) as client:
record = client.get(
"project",
fields={
"field1": "value1",
"field2": "value2",
},
)
>>> record
{
"climb_id": "C-1234567890",
"published_date": "2023-01-01",
"field1": "value1",
"field2": "value2",
}
The include
and exclude
arguments can be used to control the fields returned:
import os
from onyx import OnyxConfig, OnyxEnv, OnyxClient
config = OnyxConfig(
domain=os.environ[OnyxEnv.DOMAIN],
token=os.environ[OnyxEnv.TOKEN],
)
with OnyxClient(config) as client:
record_v1 = client.get(
"project",
climb_id="C-1234567890",
include=["climb_id", "published_date"],
)
record_v2 = client.get(
"project",
climb_id="C-1234567890",
exclude=["field2"],
)
>>> record_v1
{
"climb_id": "C-1234567890",
"published_date": "2023-01-01",
}
>>> record_v2
{
"climb_id": "C-1234567890",
"published_date": "2023-01-01",
"field1": "value1",
}
Tips
- Including/excluding fields to reduce the size of the returned data can improve performance.
filter(project, fields=None, include=None, exclude=None, summarise=None, **kwargs)
¶
Filter records from a project.
PARAMETER | DESCRIPTION |
---|---|
project |
Name of the project.
TYPE:
|
fields |
Dictionary of field filters.
TYPE:
|
include |
Fields to include in the output.
TYPE:
|
exclude |
Fields to exclude from the output.
TYPE:
|
summarise |
For a given field (or group of fields), return the frequency of each unique value (or unique group of values).
TYPE:
|
**kwargs |
Additional keyword arguments are interpreted as field filters.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None
|
Generator of records. If a summarise argument is provided, each record will be a dict containing values of the summary fields and a count for the frequency. |
Notes
- Field filters specify requirements that the returned data must satisfy. They can be provided as keyword arguments, or as a dictionary to the
fields
argument. - These filters can be a simple match on a value (e.g.
"published_date" : "2023-01-01"
), or they can use a 'lookup' for more complex matching conditions (e.g."published_date__iso_year" : "2023"
). - Multi-value lookups (e.g.
in
,range
) can also be used. For keyword arguments, multiple values can be provided as a Python list. For thefields
dictionary, multiple values must be provided as a comma-separated string (see examples below).
Examples:
Retrieve all records that match a set of field requirements:
import os
from onyx import OnyxConfig, OnyxEnv, OnyxClient
config = OnyxConfig(
domain=os.environ[OnyxEnv.DOMAIN],
token=os.environ[OnyxEnv.TOKEN],
)
# Field conditions can either be provided as keyword arguments:
with OnyxClient(config) as client:
records = list(
client.filter(
project="project",
field1="abcd",
published_date__range=["2023-01-01", "2023-01-02"],
)
)
# Or as a dictionary to the 'fields' argument:
with OnyxClient(config) as client:
records = list(
client.filter(
project="project",
fields={
"field1": "abcd",
"published_date__range" : "2023-01-01, 2023-01-02",
},
)
)
>>> records
[
{
"climb_id": "C-1234567890",
"published_date": "2023-01-01",
"field1": "abcd",
"field2": 123,
},
{
"climb_id": "C-1234567891",
"published_date": "2023-01-02",
"field1": "abcd",
"field2": 456,
},
]
The summarise
argument can be used to return the frequency of each unique value for a given field, or the frequency of each unique set of values for a group of fields:
import os
from onyx import OnyxConfig, OnyxEnv, OnyxClient
config = OnyxConfig(
domain=os.environ[OnyxEnv.DOMAIN],
token=os.environ[OnyxEnv.TOKEN],
)
with OnyxClient(config) as client:
records_v1 = list(
client.filter(
project="project",
field1="abcd",
published_date__range=["2023-01-01", "2023-01-02"],
summarise="published_date",
)
)
records_v2 = list(
client.filter(
project="project",
field1="abcd",
published_date__range=["2023-01-01", "2023-01-02"],
summarise=["published_date", "field2"],
)
)
query(project, query=None, include=None, exclude=None, summarise=None)
¶
Query records from a project.
This method supports more complex filtering than the OnyxClient.filter
method.
Here, filters can be combined using Python's bitwise operators, representing AND, OR, XOR and NOT operations.
PARAMETER | DESCRIPTION |
---|---|
project |
Name of the project.
TYPE:
|
query |
TYPE:
|
include |
Fields to include in the output.
TYPE:
|
exclude |
Fields to exclude from the output.
TYPE:
|
summarise |
For a given field (or group of fields), return the frequency of each unique value (or unique group of values).
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None
|
Generator of records. If a summarise argument is provided, each record will be a dict containing values of the summary fields and a count for the frequency. |
Notes
- The
query
argument must be an instance ofOnyxField
. OnyxField
instances can be combined into complex expressions using Python's bitwise operators:&
(AND),|
(OR),^
(XOR), and~
(NOT).- Multi-value lookups (e.g.
in
,range
) support passing a Python list (see example below).
Examples:
Retrieve all records that match the query provided by an OnyxField
object:
import os
from onyx import OnyxConfig, OnyxEnv, OnyxClient, OnyxField
config = OnyxConfig(
domain=os.environ[OnyxEnv.DOMAIN],
token=os.environ[OnyxEnv.TOKEN],
)
with OnyxClient(config) as client:
records = list(
client.query(
project="project",
query=(
OnyxField(field1="abcd")
& OnyxField(published_date__range=["2023-01-01", "2023-01-02"])
),
)
)
to_csv(csv_file, data, delimiter=None)
classmethod
¶
Write a set of records to a CSV file.
PARAMETER | DESCRIPTION |
---|---|
csv_file |
File object for the CSV file being written to.
TYPE:
|
data |
The data being written to the CSV file. Must be either a list / generator of dict records.
TYPE:
|
delimiter |
CSV delimiter. If not provided, defaults to
TYPE:
|
Examples:
import os
from onyx import OnyxConfig, OnyxEnv, OnyxClient
config = OnyxConfig(
domain=os.environ[OnyxEnv.DOMAIN],
token=os.environ[OnyxEnv.TOKEN],
)
with OnyxClient(config) as client, open("/path/to/file.csv") as csv_file:
client.to_csv(
csv_file=csv_file,
data=client.filter(
"project",
fields={
"field1": "value1",
"field2": "value2",
},
)
history(project, climb_id)
¶
View the history of a record in a project.
PARAMETER | DESCRIPTION |
---|---|
project |
Name of the project.
TYPE:
|
climb_id |
Unique identifier for the record in the project.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Dict[str, Any]
|
Dict containing the history of the record. |
Examples:
import os
from onyx import OnyxConfig, OnyxEnv, OnyxClient
config = OnyxConfig(
domain=os.environ[OnyxEnv.DOMAIN],
token=os.environ[OnyxEnv.TOKEN],
)
with OnyxClient(config) as client:
history = client.history("project", "C-1234567890")
>>> history
{
"climb_id": "C-1234567890",
"history": [
{
"username": "user",
"timestamp": "2023-01-01T00:00:00Z",
"action": "add",
},
{
"username": "user",
"timestamp": "2023-01-02T00:00:00Z",
"action": "change",
"changes": [
{
"field": "field_1",
"type": "text",
"from": "value1",
"to": "value2",
},
{
"field": "field_2",
"type": "integer",
"from": 3,
"to": 4,
},
{
"field": "nested_field",
"type": "relation",
"action": "add",
"count" : 3,
},
{
"field": "nested_field",
"type": "relation",
"action": "change",
"count" : 10,
},
],
},
],
}
identify(project, field, value, site=None)
¶
Get the anonymised identifier for a value on a field.
PARAMETER | DESCRIPTION |
---|---|
project |
Name of the project.
TYPE:
|
field |
Field on the project.
TYPE:
|
value |
Value to identify.
TYPE:
|
site |
Site to identify the value on. If not provided, defaults to the user's site.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Dict[str, str]
|
Dict containing the project, site, field, value and anonymised identifier. |
Examples:
create(project, fields, test=False)
¶
Create a record in a project.
PARAMETER | DESCRIPTION |
---|---|
project |
Name of the project.
TYPE:
|
fields |
Object representing the record to be created.
TYPE:
|
test |
If
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Dict[str, Any]
|
Dict containing the CLIMB ID of the created record. |
Examples:
update(project, climb_id, fields=None, test=False)
¶
Update a record in a project.
PARAMETER | DESCRIPTION |
---|---|
project |
Name of the project.
TYPE:
|
climb_id |
Unique identifier for the record in the project.
TYPE:
|
fields |
Object representing the record to be updated.
TYPE:
|
test |
If
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Dict[str, Any]
|
Dict containing the CLIMB ID of the updated record. |
Examples:
import os
from onyx import OnyxConfig, OnyxEnv, OnyxClient
config = OnyxConfig(
domain=os.environ[OnyxEnv.DOMAIN],
token=os.environ[OnyxEnv.TOKEN],
)
with OnyxClient(config) as client:
result = client.update(
project="project",
climb_id="C-1234567890",
fields={
"field1": "value1",
"field2": "value2",
},
)
delete(project, climb_id)
¶
Delete a record in a project.
PARAMETER | DESCRIPTION |
---|---|
project |
Name of the project.
TYPE:
|
climb_id |
Unique identifier for the record in the project.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Dict[str, Any]
|
Dict containing the CLIMB ID of the deleted record. |
Examples:
csv_create(project, csv_file, fields=None, delimiter=None, multiline=False, test=False)
¶
Use a CSV file to create record(s) in a project.
PARAMETER | DESCRIPTION |
---|---|
project |
Name of the project.
TYPE:
|
csv_file |
File object for the CSV file being used for record upload.
TYPE:
|
fields |
Additional fields provided for each record being uploaded. Takes precedence over fields in the CSV.
TYPE:
|
delimiter |
CSV delimiter. If not provided, defaults to
TYPE:
|
multiline |
If
TYPE:
|
test |
If
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Union[Dict[str, Any], List[Dict[str, Any]]]
|
Dict containing the CLIMB ID of the created record. If |
Examples:
Create a single record:
import os
from onyx import OnyxConfig, OnyxEnv, OnyxClient
config = OnyxConfig(
domain=os.environ[OnyxEnv.DOMAIN],
token=os.environ[OnyxEnv.TOKEN],
)
with OnyxClient(config) as client, open("/path/to/file.csv") as csv_file:
result = client.csv_create(
project="project",
csv_file=csv_file,
)
Create multiple records:
import os
from onyx import OnyxConfig, OnyxEnv, OnyxClient
config = OnyxConfig(
domain=os.environ[OnyxEnv.DOMAIN],
token=os.environ[OnyxEnv.TOKEN],
)
with OnyxClient(config) as client, open("/path/to/file.csv") as csv_file:
results = client.csv_create(
project="project",
csv_file=csv_file,
multiline=True,
)
csv_update(project, csv_file, fields=None, delimiter=None, multiline=False, test=False)
¶
Use a CSV file to update record(s) in a project.
PARAMETER | DESCRIPTION |
---|---|
project |
Name of the project.
TYPE:
|
csv_file |
File object for the CSV file being used for record upload.
TYPE:
|
fields |
Additional fields provided for each record being uploaded. Takes precedence over fields in the CSV.
TYPE:
|
delimiter |
CSV delimiter. If not provided, defaults to
TYPE:
|
multiline |
If
TYPE:
|
test |
If
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Union[Dict[str, Any], List[Dict[str, Any]]]
|
Dict containing the CLIMB ID of the updated record. If |
Examples:
Update a single record:
import os
from onyx import OnyxConfig, OnyxEnv, OnyxClient
config = OnyxConfig(
domain=os.environ[OnyxEnv.DOMAIN],
token=os.environ[OnyxEnv.TOKEN],
)
with OnyxClient(config) as client, open("/path/to/file.csv") as csv_file:
result = client.csv_update(
project="project",
csv_file=csv_file,
)
Update multiple records:
import os
from onyx import OnyxConfig, OnyxEnv, OnyxClient
config = OnyxConfig(
domain=os.environ[OnyxEnv.DOMAIN],
token=os.environ[OnyxEnv.TOKEN],
)
with OnyxClient(config) as client, open("/path/to/file.csv") as csv_file:
results = client.csv_update(
project="project",
csv_file=csv_file,
multiline=True,
)
csv_delete(project, csv_file, delimiter=None, multiline=False)
¶
Use a CSV file to delete record(s) in a project.
PARAMETER | DESCRIPTION |
---|---|
project |
Name of the project.
TYPE:
|
csv_file |
File object for the CSV file being used for record upload.
TYPE:
|
delimiter |
CSV delimiter. If not provided, defaults to
TYPE:
|
multiline |
If
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Union[Dict[str, Any], List[Dict[str, Any]]]
|
Dict containing the CLIMB ID of the deleted record. If |
Examples:
Delete a single record:
import os
from onyx import OnyxConfig, OnyxEnv, OnyxClient
config = OnyxConfig(
domain=os.environ[OnyxEnv.DOMAIN],
token=os.environ[OnyxEnv.TOKEN],
)
with OnyxClient(config) as client, open("/path/to/file.csv") as csv_file:
result = client.csv_delete(
project="project",
csv_file=csv_file,
)
Delete multiple records:
import os
from onyx import OnyxConfig, OnyxEnv, OnyxClient
config = OnyxConfig(
domain=os.environ[OnyxEnv.DOMAIN],
token=os.environ[OnyxEnv.TOKEN],
)
with OnyxClient(config) as client, open("/path/to/file.csv") as csv_file:
results = client.csv_delete(
project="project",
csv_file=csv_file,
multiline=True,
)
register(domain, first_name, last_name, email, site, password)
classmethod
¶
Create a new user.
PARAMETER | DESCRIPTION |
---|---|
domain |
Name of the domain.
TYPE:
|
first_name |
First name of the user.
TYPE:
|
last_name |
Last name of the user.
TYPE:
|
email |
Email address of the user.
TYPE:
|
site |
Name of the site.
TYPE:
|
password |
Password for the user.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Dict[str, Any]
|
Dict containing the user's information. |
Examples:
login()
¶
Log in the user.
RETURNS | DESCRIPTION |
---|---|
Dict[str, Any]
|
Dict containing the user's authentication token and it's expiry. |
Examples:
logout()
¶
logoutall()
¶
profile()
¶
View the user's information.
RETURNS | DESCRIPTION |
---|---|
Dict[str, str]
|
Dict containing the user's information. |
Examples:
activity()
¶
View the user's latest activity.
RETURNS | DESCRIPTION |
---|---|
List[Dict[str, Any]]
|
List of the user's latest activity. |
Examples:
import os
from onyx import OnyxConfig, OnyxEnv, OnyxClient
config = OnyxConfig(
domain=os.environ[OnyxEnv.DOMAIN],
token=os.environ[OnyxEnv.TOKEN],
)
with OnyxClient(config) as client:
activity = client.activity()
>>> activity
[
{
"date": "2023-01-01T00:00:00.000000Z",
"address": "127.0.0.1",
"endpoint": "/projects/project/",
"method": "POST",
"status": 400,
"exec_time": 29,
"error_messages" : "b'{"status":"fail","code":400,"messages":{"site":["Select a valid choice."]}}'",
},
{
"timestamp": "2023-01-02T00:00:00.000000Z",
"address": "127.0.0.1",
"endpoint": "/accounts/activity/",
"method": "GET",
"status": 200,
"exec_time": 22,
"error_messages": "",
},
]
approve(username)
¶
Approve another user.
PARAMETER | DESCRIPTION |
---|---|
username |
Username of the user to be approved.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Dict[str, Any]
|
Dict confirming user approval success. |
Examples:
waiting()
¶
Get users waiting for approval.
RETURNS | DESCRIPTION |
---|---|
List[Dict[str, Any]]
|
List of users waiting for approval. |
Examples:
site_users()
¶
Get users within the site of the requesting user.
RETURNS | DESCRIPTION |
---|---|
List[Dict[str, Any]]
|
List of users within the site of the requesting user. |
Examples: