General purpose Python client for the AppNexus API
Project description
General purpose Python client for the AppNexus API.
This library exists because most of the open-source solutions we found were for specific AppNexus tasks, such as reporting. Our solution, however, is meant to be used with any AppNexus service.
As it heavily relies on the AppNexus API, we advise you to read its documentation.
This client uses models in the same way that database ORM would do, but you can also hook it to your own data representation class, or simply use Python dictionaries.
Install
$ pip install appnexus-client
Getting started
Services
A service is an endpoint on the AppNexus API, representing an entity such as a creative. Here is the complete list of services usable with AppNexus-client: AccountRecovery, AdProfile, AdQualityRule, Adserver, Advertiser, BatchSegment, Brand, Broker, Browser, BudgetSplitter, Campaign, Carrier, Category, ChangeLog, ChangeLogDetail, City, ContentCategory, Country, Creative, CreativeFormat, Currency, CustomModel, CustomModelHash, CustomModelLogit, CustomModelLUT, CustomModelParser, Deal, DealBuyerAccess, DealFromPackage, DMA, DeviceMake, DeviceModel, DomainAuditStatus, DomainList, ExternalInvCode, InsertionOrder, InventoryAttribute, InventoryResold, IPRangeList, Label, Language, LineItem, LineItemModel, Lookup, ManualOfferRanking, MediaSubtype, MediaType, Member, MemberProfile, MobileApp, MobileAppInstance, MobileAppInstanceList, MobileAppStore, NativeCustomKey, ObjectLimit, OperatingSystem, OperatingSystemExtended, OperatingSystemFamily, OptimizationZone, Package, PackageBuyerAccess, PaymentRule, Pixel, Placement, PlatformMember, PostalCode, Profile, ProfileSummary, Publisher, Region, Report, ReportStatus, Search, Segment, Site, TechnicalAttribute, Template, ThirdpartyPixel, User, UsergroupPattern, VisibilityProfile
Connecting
First of all, you need to connect the client to AppNexus. One simple way is to use the connect function with your credentials:
from appnexus import connect
connect("my-username", "my-password")
From there, you can use all the features of the library.
Models
A model in AppNexus-client is an abstraction for a service. Most of them are already declared and you just have to import them.
You can access the fields of an AppNexus just like any object: entity.field_name
For example, to print the name of each and every city registered in AppNexus, you could do:
from appnexus import City
for city in City.find():
print(city.name)
You can also retrieve a single result (the first one returned by the API) using the find_one method:
city = City.find_one(id=1337)
Filtering and sorting
Sorting with AppNexus-client is easy: just give a sort parameter with a value indicating which field is sorted in which order (asc or desc). This parameter will be supplied to the AppNexus API which will return a sorted response.
You can filter entities using parameters of the methods find and find_one. Each parameter stand as a new filter for the field it is named after. For example, you can search for cities whose country_code field is equal to “FR” and sort them by name:
for city in City.find(country_code="FR", sort="name.desc"):
print(city.name)
The parameters you give to the find and find_one methods are translated into query parameters for the requests being send. For example, the snippet Creative.find(state="active", advertiser_id=[1, 2, 3]) will result in a get request on http://api.appnexus.com/creative?state=active&advertiser_id=1,2,3
Please search in the AppNexus API documentation to understand the meaning of each parameter.
Custom data representation
By default, AppNexus-client relies on Thingy to represent data as objects.
But you can also hook your own data representation class. For this, you must use a function that exposes this signature:
function(client, service, object)
The client argument is an AppNexusClient instance. service is the string representation of the service to which the object belongs. object is a dictionary containing the data about the AppNexus entity. The return value of this function will be used as the data representation.
To use this function and get the desired data representation, you must pass it to the client as the representation keyword argument.
If you want your data to be in the form of simple dictionaries rather than Thingy instances, AppNexus-client provides a raw representation that you can use pretty easily:
from appnexus.representations import raw
connect("username", "password", representation=raw)
But if, for example, you would prefer to get lists of tuples, you would have to craft your own representation function:
def custom_representation(client, service_name, object):
return object.items()
connect("username", "password", representation=custom_representation)
Reports
Retrieving report data has 3 steps:
Creating a report
Checking if the report is ready to download
Downloading the report
from appnexus import Report
json = {
"report_type": "network_analytics",
"columns": [
"clicks",
"total_convs",
"insertion_order_id",
"line_item_id",
],
"report_interval": "lifetime",
"format": "csv"
}
report = Report(json).save()
data = report.download()
The download method on Report object takes care of checking if the report is available for download and retires it by default for 3 times with an interval of 1 second. The number of retries can be overridden by passing the parameter retry_count to the download method:
data = report.download(retry_count=5)
Changelogs
The ChangeLog service allows to retrieve information about changes that have been made to an object of those services: campaign, insertion-order, line-item and profile.
For example, you can print the date of every change that was made on a campaign:
from appnexus import Campaign
campaign = Campaign.find_one()
for change in campaign.changelog:
print(change.created_on)
For more information on a change, you can use the ChangeLogDetail service with the returned transaction_id as a parameter:
from appnexus import ChangeLogDetail
detail = ChangeLogDetail.find_one(service="campaign",
resource_id=change.resource_id,
transaction_id=change.transaction_id)
print(detail.user_full_name)
Tests
To run AppNexus-client tests:
install developers requirements with pip install -r requirements.txt;
run pytest.
License
MIT
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
File details
Details for the file AppNexus-client-0.8.1.tar.gz
.
File metadata
- Download URL: AppNexus-client-0.8.1.tar.gz
- Upload date:
- Size: 19.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.6.0 requests-toolbelt/0.9.1 tqdm/4.38.0 CPython/3.5.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3974e557fb044f39aed6712b23be5586e79beaa2aebc3ad905b7f75679ccf9c6 |
|
MD5 | b02ebc8798848c716762b63aba44c26c |
|
BLAKE2b-256 | 0b3b5c9e96a1e5f4c61a3bd6018b54d49ce3aff3f3f0207950fde4a3c2bd57f1 |
File details
Details for the file AppNexus_client-0.8.1-py2.py3-none-any.whl
.
File metadata
- Download URL: AppNexus_client-0.8.1-py2.py3-none-any.whl
- Upload date:
- Size: 13.0 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.6.0 requests-toolbelt/0.9.1 tqdm/4.38.0 CPython/3.5.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b45d662df979a175fd1fd37f1d40fd29529477b6b9d13c95fed822ecfd800136 |
|
MD5 | 24a01d709bf73a8bf5bdf5298e2c41a9 |
|
BLAKE2b-256 | 9d7102cf831566003d5f4b95066c5f56ce51126af42aeef5222bcf7e43aeddf3 |