Anura SDK for Python
The Anura SDK for Python provides a simpler integration process for Anura Direct within their Python projects. Empowering developers to begin analyzing their traffic.
Getting Started
- Have an open active account with Anura - You can see more about Anura's offerings here.
- Minimum Requirements - To use the SDK, you will need Python >=3.10.
- Install the SDK
- View our Quick Examples to immediately begin using the SDK!
Installing the SDK
The easiest and recommended way to install the SDK is to use pip. You can install it with the following command:
Terminal
pip3 install anura
Or, install from source by using one of the following examples according to your operating system:
Linux/Mac:
Terminal
git clone https://github.com/anuraio/anura-sdk-python.git
cd anura-sdk-python
python3 -m pip3 install -r requirements.txt
python3 -m pip3 install -e .
Windows:
Terminal
git clone https://github.com/anuraio/anura-sdk-python.git
cd anura-sdk-python
py -m pip3 install -r requirements.txt
py -m pip3 install -e .
Quick Examples
Create the Anura Direct client
from anura.direct.client import AnuraDirect
import asyncio # used for asynchronous result fetching
import aiohttp # used for asynchronous result fetching
direct = AnuraDirect('your-instance-id-goes-here')
Set a custom source, campaign, and additional data for Anura Direct
direct.source = 'your-source-value'
direct.campaign = 'your-campaign-value'
direct.add_additional_data('1', 'your-data-value')
Updating additional data at a specific index
# To update an element of additional data at a specific index,
# simply add the element again but with a new value.
direct.add_additional_data('1', 'your-new-data-value')
Removing an element from additional data
index_to_remove = 1
direct.remove_additional_data(index_to_remove)
Get a result from Anura Direct
try:
result = direct.get_result(
'visitors-ip-address', # required
'visitors-user-agent', # optional
'visitors-app-package-id', # optional
'visitors-device-id' # optional
)
print('result: ' + result)
except Exception as e:
print(e)
Get a result from Anura Direct asynchronously
async def main():
direct = AnuraDirect('your-instance-id')
async with aiohttp.ClientSession() as session:
try:
result = await direct.get_result_async(
session,
'visitors-ip-address', # required
'visitors-user-agent', # optional
'visitors-app-package-id', # optional
'visitors-device-id' # optional
)
print('result: ' + result)
except Exception as e:
print(e)
# Use asyncio to run the coroutine (asynchronous function).
asyncio.run(main())
API Reference
AnuraDirect
Can get results from Anura Direct. These results are fetched using Direct's /direct.json API endpoint.
Methods
get_result() -> DirectResult- Gets a result synchronously from Anura Direct. Raises an exception if an error occurs throughout the result fetching process.
- Exceptions thrown:
- AnuraException: if a 4XX, 5XX, or any other erroneous response from the Anura Direct API
Parameters:
Name | Type | Description | Required |
---|---|---|---|
ip_address | str | The IP address of your visitor. Both IPv4 & IPv6 addresses are supported. | Yes |
user_agent | str | The user agent string of your visitor. | |
app | str | The application package identifier of your visitor (when available.) | |
device | str | The device identifier of your visitor (when available.) |
- Gets a result asynchronously from Anura Direct. Raises an exception if an error occurs throughout the result fetching process.
- Exception thrown:
- AnuraException: if a 4XX, 5XX, or any other erroneous response from the Anura Direct API
Parameters:
Name | Type | Description | Required |
---|---|---|---|
session | aiohttp.ClientSession | The aiohttp client session object. | Yes |
ip_address | str | The IP address of your visitor. Both IPv4 & IPv6 addresses are supported. | Yes |
user_agent | str | The user agent string of your visitor. | |
app | str | The application package identifier of your visitor (when available.) | |
device | str | The device identifier of your visitor (when available.) |
- Adds an element of additional data to your AnuraDirect client.
- Removes the element from your additional data array located at the provided key
- Returns the Instance ID you have set within the AnuraDirect client.
- Returns the source you have set within the AnuraDirect client.
- Returns the campaign you have set within the AnuraDirect client.
- Returns the additional data you have set within the AnuraDirect client.
- Sets the Instance ID of the AnuraDirect client to the instance value passed.
- Sets the source of the AnuraDirect client to the source value passed.
- Sets the campaign of the AnuraDirect client to the campaign value passed.
- Sets the additional data of the AnuraDirect client to the additional_data value passed.
DirectResult
The result upon a successful call to get_result() or get_result_async() from the AnuraDirect client. It contains not only the result from the Anura Direct API, but some other methods to help you use the result as well.
Methods
is_suspect() -> bool- Returns whether or not the visitor has been determined to be suspect.
- Returns whether or not the visitor has been determined to be non-suspect.
- Returns whether or not the visitor has been determined to be on a mobile device.
Properties
result: str- Besides using the is_suspect() or is_non_suspect() methods, you are also able to directly access the result value.
- If you have return rule sets enabled, you will be able to see which specific rules were violated upon a suspect result. This value will be None if the visitor is non-suspect, or if you do not have return rule sets enabled.
- You can talk to support about enabling or disabling the return rule sets feature.
- If you have return invalid traffic type enabled, you will be able to access which type of invalid traffic occurred upon a suspect result.
- You can talk to support about enabling or disabling the return invalid traffic type feature.