Skip to content

Commit

Permalink
Add API.user_agent
Browse files Browse the repository at this point in the history
  • Loading branch information
Harmon758 committed May 13, 2021
1 parent 348232f commit bbec64b
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion tweepy/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@
import logging
import mimetypes
import os
from platform import python_version
import sys
import time
from urllib.parse import urlencode

import requests

import tweepy
from tweepy.errors import (
BadRequest, Forbidden, HTTPException, NotFound, TooManyRequests,
TweepyException, TwitterServerError, Unauthorized
Expand Down Expand Up @@ -76,7 +78,8 @@ class API:
def __init__(
self, auth=None, *, cache=None, host='api.twitter.com', parser=None,
proxy=None, retry_count=0, retry_delay=0, retry_errors=None,
timeout=60, upload_host='upload.twitter.com', wait_on_rate_limit=False
timeout=60, upload_host='upload.twitter.com', user_agent=None,
wait_on_rate_limit=False
):
self.auth = auth
self.cache = cache
Expand All @@ -95,6 +98,15 @@ def __init__(
self.retry_errors = retry_errors
self.timeout = timeout
self.upload_host = upload_host

if user_agent is None:
user_agent = (
f"Python/{python_version()} "
f"Requests/{requests.__version__} "
f"Tweepy/{tweepy.__version__}"
)
self.user_agent = user_agent

self.wait_on_rate_limit = wait_on_rate_limit

# Attempt to explain more clearly the parser argument requirements
Expand All @@ -121,6 +133,10 @@ def request(

self.cached_result = False

if headers is None:
headers = {}
headers["User-Agent"] = self.user_agent

# Build the request URL
path = f'/1.1/{endpoint}.json'
if upload_api:
Expand Down

0 comments on commit bbec64b

Please sign in to comment.