A client library for DDP API
Project description
DDP API python client
This is a python client library for the OAuth2 DDP API. Currently the following services are supported:
UserListService
UserListClientService
Installation
pip install ddpclient
Test
python setup.py test
Requirement
OAuth client ID and OAuth client secret
DDP API uses OAuth 2.0 for authorization, to authorize the DDP API, you must first create a Client ID and a Client Secret. Visit the Developer Console to create a project and a OAuth client with credentials.
You will use the client ID and secret to authorize DDP API.
AdWards Client Customer ID
A unique three-part number that’s assigned to each AdWords account, listed at the top of every page in your account.
You can set it as an environment variable DDP_CLIENT_CUSTOMER_ID. Or you can pass the ID into the Client class constructor (details below).
Commands
When installed, the DDP API client provide two command for authorizing your application access to a DDP account.
These commands are provided to help quickly getting access to the DDP API to run the examples. It stores crendentials in a file named .ddp_credentials. If you are using Flask or Django, consider using the oauth2client.contrib.flask_util and oauth2client.contrib.django_util
Usage
Get UserList example:
from ddpclient import Selector, Client, Auth credentials = Auth().get_credentials() api_service = Client(credentials).user_list_service() selector = Selector(api_service). \ select_fields('Id', 'Size'). \ filter_by('Status', 'CLOSED'). \ order_by('Id', True). \ at_page(1, 3). \ build() response = api_service.service.get(selector) print response # (UserListPage){ # totalNumEntries = 108 # Page.Type = "UserListPage" # entries[] = # (BasicUserList){ # id = 978704062 # isReadOnly = False # name = "Name one" # size = 0 # sizeRange = "LESS_THAN_FIVE_HUNDRED" # listType = "REMARKETING" # UserList.Type = "BasicUserList" # }, # (BasicUserList){ # id = 178703382 # isReadOnly = False # name = "Name two" # size = 0 # sizeRange = "LESS_THAN_FIVE_HUNDRED" # listType = "REMARKETING" # UserList.Type = "BasicUserList" # }, # (BasicUserList){ # id = 138700763 # isReadOnly = False # name = "Name three" # size = 0 # sizeRange = "LESS_THAN_FIVE_HUNDRED" # listType = "REMARKETING" # UserList.Type = "BasicUserList" # }, # }
Add UserList example:
from ddpclient import Selector, Client, Operation, Auth credentials = Auth().get_credentials() api_service = Client(credentials).user_list_service() api_operation = Operation(api_service).add().user_list( name='TEST', description='TEST Description', status='CLOSED', integrationCode='123', accountUserListStatus='INACTIVE', membershipLifeSpan=30).build() response = api_service.service.mutate([api_operation]) print response # (UserListReturnValue){ # ListReturnValue.Type = "UserListReturnValue" # value[] = # (BasicUserList){ # id = 12345678 # isReadOnly = False # name = "TEST" # description = "TEST Description" # status = "CLOSED" # integrationCode = "123" # accessReason = "OWNED" # accountUserListStatus = "INACTIVE" # membershipLifeSpan = 30 # listType = "REMARKETING" # isEligibleForSearch = True # isEligibleForDisplay = True # UserList.Type = "BasicUserList" # }, # }
Update UserList example:
from ddpclient import Auth, Selector, Client, Operation credentials = Auth().get_credentials() api_service = Client(credentials).user_list_service() api_operation = Operation(api_service).set().user_list( id=395677280, name='TEST Updated Name').build() response = api_service.service.mutate([api_operation]) print response # (UserListReturnValue){ # ListReturnValue.Type = "UserListReturnValue" # value[] = # (BasicUserList){ # id = 12345678 # isReadOnly = False # name = "TEST Updated Name" # description = "TEST Description" # status = "CLOSED" # integrationCode = "123" # accessReason = "OWNED" # accountUserListStatus = "INACTIVE" # membershipLifeSpan = 30 # listType = "REMARKETING" # isEligibleForSearch = True # isEligibleForDisplay = True # UserList.Type = "BasicUserList" # }, # }
Remove UserList example:
from ddpclient import Auth, Selector, Client, Operation credentials = Auth().get_credentials() api_service = Client(credentials).user_list_service() api_operation = Operation(api_service).remove().user_list(id=395677280).build() response = api_service.service.mutate([api_operation]) print response # suds.WebFault: Server raised fault: '[OperatorError.OPERATOR_NOT_SUPPORTED @ operations[0]]' # Note: UserListService does not support deleting user list, this code servers as example of 'remove' operations
Classes
Auth
Auth class can be used to generate URL (authorize_url) for user giving authorization:
Auth().authorize_url(client_id, client_secret)
Auth also accept a auth code and obtain credentials after user having visited the above URL and granted the authorization to you application. The credentials object returned will be saved into a storage object.
Auth().authorize(client_id, client_secret, auth_code)
When the authorize method is done, by default Auth save the credentials object ( oauth2client.client.OAuth2Credentials) into a file (.ddp_credentials) using oauth2client.file.Storage. Once saved, this credential can be retrieved by:
credentials = Auth().get_credentials()
Saving credentials into a file for later retrieval is very simple but does not work for environments like Heroku. You might want to save the credentials object into a database so that the credentials can survive between deployments. The Auth constructor can accept a custom storage object with put and get methods defined. Using custom storage object can save/retrieve credentials object into/from a database, for example.
storage = MyDBStorage() auth = Auth(storage) auth.authorize(client_id, client_secret, auth_code) credentials = auth.get_credentials()
Client
Client manages SOAP services. It requires an oauth2client.client.OAuth2Credentials object ( most likely retrieved by Auth) to its constructor. Client then use the crendentials details to make SOAP API calls to available services (UserListService and UserListClientService)
A client customer id is also required to set the SOAP header in every request. You can provide it via an environment variable DDP_CLIENT_CUSTOMER_ID or pass it explicitly to the constructor.
credentials = Auth().get_credentials() client_customer_id = '123-123-1234' api_service = Client(credentials, client_customer_id).user_list_service()
Selector and Operation
Both available services support get and mutate operations. The get operation retrieve resource and mutate operation add, update and remove resource.
Selector is used to specified resource to retrieve and Operation is used to specify resource to mutate.
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.