Separate Client.get_user from Client.get_users
authorHarmon <Harmon758@gmail.com>
Wed, 24 Feb 2021 03:32:33 +0000 (21:32 -0600)
committerHarmon <Harmon758@gmail.com>
Wed, 24 Feb 2021 03:32:33 +0000 (21:32 -0600)
tweepy/client.py

index 3970e2fd9fbe0ebbbfb076cb17b6f2e6fca24536..83c7802ef6773e4f8d759a92076fd8160b53de4d 100644 (file)
@@ -164,34 +164,46 @@ class Client:
             data_type=Tweet
         )
 
-    def get_users(self, *, ids=None, usernames=None, **params):
+    def get_user(self, *, id=None, username=None, **params):
         """
         User lookup
-        https://developer.twitter.com/en/docs/twitter-api/users/lookup/api-reference/get-users
         https://developer.twitter.com/en/docs/twitter-api/users/lookup/api-reference/get-users-id
-        https://developer.twitter.com/en/docs/twitter-api/users/lookup/api-reference/get-users-by
         https://developer.twitter.com/en/docs/twitter-api/users/lookup/api-reference/get-users-by-username-username
         """
+        if id is not None and username is not None:
+            raise TypeError("Expected ID or username, not both")
+
+        route = "/2/users"
+
+        if id is not None:
+            route += f"/{id}"
+        elif username is not None:
+            route += f"/by/username/{username}"
+        else:
+            raise TypeError("ID or username is required")
+
+        return self._make_request(
+            "GET", route, params=params,
+            allowed_params=("expansions", "tweet_fields", "user_fields"),
+            data_type=User
+        )
+
+    def get_users(self, *, ids=None, usernames=None, **params):
+        """
+        Users lookup
+        https://developer.twitter.com/en/docs/twitter-api/users/lookup/api-reference/get-users
+        https://developer.twitter.com/en/docs/twitter-api/users/lookup/api-reference/get-users-by
+        """
         if ids is not None and usernames is not None:
             raise TypeError("Expected IDs or usernames, not both")
 
         route = "/2/users"
 
         if ids is not None:
-            if isinstance(ids, (int, str)):
-                route += f"/{ids}"
-            elif len(ids) == 1:
-                route += f"/{ids[0]}"
-            else:
-                params["ids"] = ids
+            params["ids"] = ids
         elif usernames is not None:
-            if isinstance(usernames, str):
-                route += f"/by/username/{usernames}"
-            elif len(usernames) == 1:
-                route += f"/by/username/{usernames[0]}"
-            else:
-                route += "/by"
-                params["usernames"] = usernames
+            route += "/by"
+            params["usernames"] = usernames
         else:
             raise TypeError("IDs or usernames are required")