Skip to content

Commit b3f7d93

Browse files
authored
Merge pull request #102 from MerleLiuKun/feat-me
Feat me
2 parents a89a131 + f3cf7cd commit b3f7d93

File tree

4 files changed

+127
-0
lines changed

4 files changed

+127
-0
lines changed

pytwitter/api.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1234,6 +1234,40 @@ def get_user(
12341234
return_json=return_json,
12351235
)
12361236

1237+
def get_me(
1238+
self,
1239+
*,
1240+
user_fields: Optional[Union[str, List, Tuple]] = None,
1241+
expansions: Optional[Union[str, List, Tuple]] = None,
1242+
tweet_fields: Optional[Union[str, List, Tuple]] = None,
1243+
return_json: bool = False,
1244+
):
1245+
"""
1246+
Returns information about an authorized user.
1247+
1248+
:param user_fields: Fields for the user object.
1249+
:param expansions: Fields for expansions.
1250+
:param tweet_fields: Fields for the tweet object.
1251+
:param return_json: Type for returned data. If you set True JSON data will be returned.
1252+
:returns:
1253+
- data: data for the user
1254+
- includes: expansions data.
1255+
"""
1256+
args = {
1257+
"user.fields": enf_comma_separated(name="user_fields", value=user_fields),
1258+
"tweet.fields": enf_comma_separated(
1259+
name="tweet_fields", value=tweet_fields
1260+
),
1261+
"expansions": enf_comma_separated(name="expansions", value=expansions),
1262+
}
1263+
1264+
return self._get(
1265+
url=f"{self.BASE_URL_V2}/users/me",
1266+
params=args,
1267+
cls=md.User,
1268+
return_json=return_json,
1269+
)
1270+
12371271
def get_following(
12381272
self,
12391273
user_id: str,

testdata/apis/user/me_resp.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"data":{"name":"Twitter Dev","username":"TwitterDev","pinned_tweet_id":"1460323737035677698","created_at":"2013-12-14T04:35:55.000Z","id":"2244994945"},"includes":{"tweets":[{"created_at":"2021-11-15T19:08:05.000Z","id":"1460323737035677698","text":"Introducing a new era for the Twitter Developer Platform! \\n\\n\\ud83d\\udce3The Twitter API v2 is now the primary API and full of new features\\n\\u23f1Immediate access for most use cases, or apply to get more access for free\\n\\ud83d\\udcd6Removed certain restrictions in the Policy\\nhttps://t.co/Hrm15bkBWJ https://t.co/YFfCDErHsg"}]}}

tests/apis/test_users.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,24 @@ def test_get_user(api, helpers):
9898
assert username_resp["data"]["verified"]
9999

100100

101+
@responses.activate
102+
def test_get_me(api_with_user, helpers):
103+
me_data = helpers.load_json_data("testdata/apis/user/me_resp.json")
104+
105+
responses.add(
106+
responses.GET,
107+
url=f"https://api.twitter.com/2/users/me",
108+
json=me_data,
109+
)
110+
111+
me_resp = api_with_user.get_me(
112+
user_fields="created_at",
113+
expansions="pinned_tweet_id",
114+
tweet_fields="created_at",
115+
)
116+
assert me_resp.data.id == "2244994945"
117+
118+
101119
@responses.activate
102120
def test_block_and_unblock_user(api_with_user):
103121
user_id, target_user_id = "123456", "78910"

tests/test_response.py

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
"""
2+
Tests for response parser.
3+
"""
4+
5+
import pytest
6+
7+
import responses
8+
from pytwitter import PyTwitterError
9+
10+
11+
@responses.activate
12+
def test_parser_response_no_json(api):
13+
responses.add(
14+
responses.GET,
15+
url="https://api.twitter.com/2/users/123",
16+
body="",
17+
)
18+
with pytest.raises(PyTwitterError):
19+
api.get_user(user_id="123")
20+
21+
22+
@responses.activate
23+
def test_parser_response_not_ok(api):
24+
responses.add(
25+
responses.GET,
26+
url="https://api.twitter.com/2/users/123",
27+
json={
28+
"title": "Unauthorized",
29+
"type": "about:blank",
30+
"status": 401,
31+
"detail": "Unauthorized",
32+
},
33+
status=400,
34+
)
35+
with pytest.raises(PyTwitterError):
36+
api.get_user(user_id="123")
37+
38+
39+
@responses.activate
40+
def test_parser_response_have_error(api):
41+
responses.add(
42+
responses.GET,
43+
url="https://api.twitter.com/2/users/123",
44+
json={
45+
"errors": {
46+
"title": "Unauthorized",
47+
"type": "about:blank",
48+
"status": 401,
49+
"detail": "Unauthorized",
50+
}
51+
},
52+
)
53+
with pytest.raises(PyTwitterError):
54+
api.get_user(user_id="123")
55+
56+
57+
@responses.activate
58+
def test_parser_response_have_reason_error(api):
59+
# Refer: https://developer.twitter.com/en/support/twitter-api/error-troubleshooting
60+
responses.add(
61+
responses.GET,
62+
url="https://api.twitter.com/2/users/123",
63+
json={
64+
"client_id": "101010101",
65+
"required_enrollment": "Standard Basic",
66+
"registration_url": "https://developer.twitter.com/en/account",
67+
"title": "Client Forbidden",
68+
"detail": "This request must be made using an approved developer account that is enrolled in the requested endpoint. Learn more by visiting our documentation.",
69+
"reason": "client-not-enrolled",
70+
"type": "https://api.twitter.com/2/problems/client-forbidden",
71+
},
72+
)
73+
with pytest.raises(PyTwitterError):
74+
api.get_user(user_id="123")

0 commit comments

Comments
 (0)