Getting Started¶
Installation¶
# install the latest release
pip install twitter-ads-v2
Quick Start¶
import twitter_ads_v2
CONSUMER_KEY = 'your consumer key'
CONSUMER_SECRET = 'your consumer secret'
ACCESS_TOKEN = 'access token'
ACCESS_TOKEN_SECRET = 'access token secret'
ACCOUNT_ID = 'account id'
client = twitter_ads_v2.Client(
ACCOUNT_ID,
CONSUMER_KEY,
CONSUMER_SECRET,
ACCESS_TOKEN,
ACCESS_TOKEN_SECRET,
options={
'handle_rate_limit': True
}
)
# index
data = client.campaigns('all')
print(data.headers)
print(data.body)
# show
data = client.campaigns('load', id='campaign_id')
print(data.headers)
print(data.body)
tweets = client.promoted_tweets('all')
# iterate over until the cursor ("next_cursor") is exhausted
for i in tweets:
print(i.headers)
print(i.body)
Types of endpoint_type
¶
To provide a consistent interface, the first parameter of each API method is always endpoint_type
:
data = client.campaigns('load', id='campaign_id')
in the above example, load
is the endpoint_type
of this call. The terms of each endpoint_type
are described below.
|
description |
HTTP method |
---|---|---|
|
Retrieve *all* or **some*_ of the entities’ details depends on the request parameters from an index endpoint (i.e., most index endpoints have a parameter that can scope the results). |
GET |
|
Retrieve a *specific_ entity details from a show endpoint (i.e., most show endpoints require an entity id to retrieve as part of resource URI). |
GET |
|
Create a new entity. |
POST |
|
Update a specific entity. |
PUT |
|
Delete a specific entity. |
DELETE |
|
Create/Update/Delete entities depends on the POST body data (normally a JSON object). |
POST |
Request Parameters¶
All request parameters except endpoint_type
as mentioned above should be passed as keyword arguments.
Rate-limit handling and request retry¶
client = twitter_ads_v2.Client(
ACCOUNT_ID,
CONSUMER_KEY,
CONSUMER_SECRET,
ACCESS_TOKEN,
ACCESS_TOKEN_SECRET,
options={
'handle_rate_limit': True,
'retry_max': 3,
'retry_delay': 5000,
'retry_on_status': [404, 500, 503]
})
parameter |
default |
description |
---|---|---|
|
|
Set |
|
|
The number of times you want to retry when response code is found in |
|
|
The number of milliseconds you want to sleep before retry. |
|
|
The response codes you want to retry on. You can only set >= 400 status codes. |
Compatibility & Versioning¶
This project is designed to work with Python 3.5 or greater. While it may work on other versions of Python, below are the platform and runtime versions we officially support and regularly test against.
Platform |
Versions |
---|---|
CPython |
3.5, 3.6, 3.7 |
PyPy |
7.x |
All releases adhere to strict semantic versioning. For Example, major.minor.patch-pre (aka. stick.carrot.oops-peek).
Development¶
If you’d like to contribute to the project or try an unreleased development version of this project locally, you can do so quite easily by following the examples below.
# clone the repository
git clone git@github.com:smaeda-ks/twitter-python-ads-sdk-v2.git
cd twitter-python-ads-sdk-v2
# installing a local unsigned release
pip install -e .
Tests¶
$ python setup.py flake8 && python setup.py test
Documentation¶
If you’d like to contribute to the project’s documentation, you need to setup Sphinx and build pages by the following steps.
# install dependencies
$ pip install -e .[doc]
# build pages
$ cd sphinx/
$ make clean && make html