Skip to content

Commit 670ea47

Browse files
committed
feat(usage): ✅ Add tests and docs for usage tweets
1 parent dc3bbce commit 670ea47

File tree

6 files changed

+51
-0
lines changed

6 files changed

+51
-0
lines changed

README.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,9 @@ Now covers these features:
124124
- Direct Messages lookup
125125
- Manage Direct Messages
126126

127+
- Usage
128+
- Tweets
129+
127130
- Media Upload
128131
- Media Simple upload
129132
- Media Chunked upload

docs/docs/usage/usage/tweets.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The Usage API in the Twitter API v2 allows developers to programmatically retrieve their project usage. Using thie endpoint, developers can keep a track and monitor of the number of Tweets they have pulled for a given billing cycle.
2+
3+
You can learn more about the Usage API in the [docs](https://developer.twitter.com/en/docs/twitter-api/usage/tweets/introduction).
4+
5+
!!! tip "Note"
6+
7+
The Usage API need `OAuth 2.0 App-only`.
8+
9+
### Get usage
10+
11+
Get the Tweet usage within the context of a project
12+
13+
```python
14+
from pytwitter import Api
15+
16+
api = Api(bearer_token='your bearer token')
17+
18+
resp = api.get_usage_tweets(days=10, usage_fields=["daily_client_app_usage", "daily_project_usage"])
19+
print(resp.data)
20+
# Usage(cap_reset_day=28, project_id='123456789', project_cap='50000000', project_usage='11910737',...)
21+
```

docs/mkdocs.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ nav:
6868
- Direct Messages:
6969
- Direct Messages lookup: usage/direct-messages/direct-messages-lookup.md
7070
- Manage Direct Messages: usage/direct-messages/manage-direct-messages.md
71+
- Usage:
72+
- Tweets: usage/usage/tweets.md
7173
- Steaming: usage/streaming.md
7274
- Changelog: CHANGELOG.md
7375

pytwitter/rate_limit.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,12 @@ def get_limit(self, auth_type, method="GET"):
364364
LIMIT_APP_GET=200,
365365
)
366366

367+
USAGE_TWEETS = Endpoint(
368+
resource="/usage/tweets",
369+
regex=re.compile(r"/usage/tweets"),
370+
LIMIT_APP_GET=50,
371+
)
372+
367373
MEDIA_UPLOAD = Endpoint(
368374
resource="/media/upload.json",
369375
regex=re.compile(r"/media/upload.json"),
@@ -425,6 +431,7 @@ def get_limit(self, auth_type, method="GET"):
425431
DM_MESSAGE_TO_PARTICIPANT,
426432
DM_MESSAGE_TO_CONVERSATION,
427433
DM_CONVERSATIONS,
434+
USAGE_TWEETS,
428435
MEDIA_UPLOAD,
429436
]
430437

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"data":{"cap_reset_day":28,"project_cap":"50000000","project_id":"123457578435","project_usage":"11174038"}}

tests/apis/test_usage.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
"""
2+
tests for usage api
3+
"""
4+
5+
import responses
6+
7+
8+
@responses.activate
9+
def test_get_usage_tweets(api, helpers):
10+
responses.add(
11+
responses.GET,
12+
url=f"https://api.twitter.com/2/usage/tweets",
13+
json=helpers.load_json_data("testdata/apis/usage/usage_tweets_resp.json"),
14+
)
15+
16+
resp = api.get_usage_tweets()
17+
assert resp.data.cap_reset_day == 28

0 commit comments

Comments
 (0)