Skip to content

Commit

Permalink
Merge pull request #135 from uploadcare/user_agent
Browse files Browse the repository at this point in the history
New format of User-Agent, and use it for Upload API requests too
  • Loading branch information
sharov committed Apr 25, 2018
2 parents 99017b0 + 2743ef7 commit a8fef59
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 7 deletions.
5 changes: 5 additions & 0 deletions HISTORY.rst
Expand Up @@ -3,6 +3,11 @@
History
-------

2.4.0
~~~~~

- Change data format in 'User-Agent' request header.

2.3.1
~~~~~

Expand Down
2 changes: 1 addition & 1 deletion pyuploadcare/__init__.py
Expand Up @@ -15,7 +15,7 @@

from __future__ import unicode_literals

__version__ = '2.3.1'
__version__ = '2.4.0'

from .api_resources import File, FileList, FileGroup
from .exceptions import (
Expand Down
16 changes: 13 additions & 3 deletions pyuploadcare/api.py
Expand Up @@ -8,6 +8,7 @@
"""

from __future__ import unicode_literals
from platform import python_implementation, python_version
import email.utils
import hashlib
import hmac
Expand Down Expand Up @@ -54,8 +55,13 @@ def _content_type_from_response(response):


def _build_user_agent():
return '{0}/{1}/{2}'.format(conf.user_agent_name, __version__,
conf.pub_key)
extension_info = ''
if conf.user_agent_extension:
extension_info = '; {0}'.format(conf.user_agent_extension)
return 'PyUploadcare/{0} ({1}/{2}{3})'.format(__version__,
python_implementation(),
python_version(),
extension_info)


def rest_request(verb, path, data=None, timeout=conf.DEFAULT,
Expand Down Expand Up @@ -222,11 +228,15 @@ def uploading_request(verb, path, data=None, files=None, timeout=conf.DEFAULT):
data['pub_key'] = conf.pub_key
data['UPLOADCARE_PUB_KEY'] = conf.pub_key

headers = {
'User-Agent': _build_user_agent(),
}

try:
response = session.request(
str(verb), url, allow_redirects=True,
verify=conf.verify_upload_ssl, data=data, files=files,
timeout=_get_timeout(timeout),
headers=headers, timeout=_get_timeout(timeout),
)
except requests.RequestException as exc:
raise APIConnectionError(exc.args[0])
Expand Down
2 changes: 1 addition & 1 deletion pyuploadcare/conf.py
Expand Up @@ -28,4 +28,4 @@
# retry throttled requests this many times
retry_throttled = 1

user_agent_name = 'pyuploadcare'
user_agent_extension = None
5 changes: 4 additions & 1 deletion pyuploadcare/dj/conf.py
@@ -1,10 +1,12 @@
# coding: utf-8
from __future__ import unicode_literals

from django import get_version as django_version
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured

from .. import conf
from .. import __version__ as library_version


if not hasattr(settings, 'UPLOADCARE'):
Expand All @@ -18,7 +20,8 @@

conf.pub_key = settings.UPLOADCARE['pub_key']
conf.secret = settings.UPLOADCARE['secret']
conf.user_agent_name = 'pyuploadcare-django'
conf.user_agent_extension = 'Django/{0}; PyUploadcare-Django/{1}'.format(
django_version(), library_version)

if 'cdn_base' in settings.UPLOADCARE:
conf.cdn_base = settings.UPLOADCARE['cdn_base']
Expand Down
3 changes: 3 additions & 0 deletions pyuploadcare/dj/forms.py
Expand Up @@ -30,6 +30,9 @@ def __init__(self, attrs=None):
'data-public-key': conf.pub_key,
}

if conf.user_agent_extension is not None:
default_attrs['data-integration'] = conf.user_agent_extension

if dj_conf.upload_base_url is not None:
default_attrs['data-upload-base-url'] = dj_conf.upload_base_url

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Expand Up @@ -22,7 +22,7 @@

setup(
name='pyuploadcare',
version='2.3.1',
version='2.4.0',
description='Python library for Uploadcare.com',
long_description=(long_description),
author='Uploadcare LLC',
Expand Down

0 comments on commit a8fef59

Please sign in to comment.