Skip to content

Commit

Permalink
Adhering User-Agent strings to OneAll standards.
Browse files Browse the repository at this point in the history
  • Loading branch information
Liz4v committed Oct 4, 2015
1 parent c776bac commit fd4c536
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
35 changes: 30 additions & 5 deletions pyoneall/connection.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# -*- coding: utf-8 -*-
from __future__ import absolute_import, division, print_function, unicode_literals
from warnings import warn

try:
from urllib.error import HTTPError
Expand All @@ -14,12 +15,13 @@

from base64 import standard_b64encode
from json import dumps, loads
from re import compile
from sys import version

from .base import OADict
from .classes import Users, Connections, Connection, User, BadOneAllCredentials

__version__ = '0.2.2'
__version__ = '0.2.3'


class OneAll(object):
Expand All @@ -30,27 +32,29 @@ class OneAll(object):
FORMAT__JSON = 'json'

bindings = {}
_version_info = (__version__, 'pyoneall', __version__, version.split()[0])

def __init__(self, site_name, public_key, private_key, base_url=None, ua_prefix=None):
"""
:param str site_name: The name of the OneAll site
:param str public_key: API public key for the site
:param str private_key: API private key for the site
:param str base_url: An alternate format for the API URL
:param str ua_prefix: DEPRECATED and ignored. Will be removed.
"""
self.base_url = base_url if base_url else OneAll.DEFAULT_API_DOMAIN.format(site_name=site_name)
self.public_key = public_key
self.private_key = private_key
ua = str(ua_prefix or '').split() + ['pyoneall-' + __version__] + ('python-' + version).split()
self.user_agent_string = ' '.join(w for w in ua if w)
if ua_prefix is not None:
warn('The argument ua_prefix is no longer used. Use set_version() instead.', DeprecationWarning, 1)

def _exec(self, action, params=None, post_params=None):
"""
Execute an API action
:param str action: The action to be performed. Translated to REST call
:param dict params: Additional GET parameters for action
:post_params: POST parameters for action
:param dict post_params: POST parameters for action
:returns dict: The JSON result of the call in a dictionary format
"""
request_url = '%s/%s.%s' % (self.base_url, action, OneAll.FORMAT__JSON)
Expand All @@ -61,7 +65,7 @@ def _exec(self, action, params=None, post_params=None):
token = '%s:%s' % (self.public_key, self.private_key)
auth = standard_b64encode(token.encode())
req.add_header('Authorization', 'Basic %s' % auth.decode())
req.add_header('User-Agent', self.user_agent_string)
req.add_header('User-Agent', self._get_user_agent_string())
try:
request = urlopen(req)
except HTTPError as e:
Expand Down Expand Up @@ -173,3 +177,24 @@ def publish(self, user_token, post_params):
:returns OADict: The API response
"""
return OADict(**self._exec('users/%s/publish' % user_token, post_params=post_params))

def set_version(self, social_version, platform_name, platform_version):
"""
Sets the version informed to OneAll.com in User-Agent strings. This info is used by OneAll.com to keep track of
which implementations are in use; all languages and environments.
:param str social_version: PEP-440 compliant version number of client code.
:param str platform_name: Name of platform, no spaces.
Make sure it's unique so your library project won't be confused with someone else's.
:param str platform_version: PEP-440 compliant version number of platform, e.g. Django or Flask version.
"""
result = (social_version, platform_name, platform_version, version.split()[0])
invalid = tuple(filter(compile(r'[\s/]').search, result))
if invalid:
raise ValueError('The following values are invalid: [%s]' % ','.join(invalid))
self._version_info = result

def _get_user_agent_string(self):
ua = 'SocialLogin/%s %s/%s-%s pyoneall +http://oneall.com' % self._version_info
print(ua)
return ua
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
README = open(join(dirname(__file__), 'README.rst')).read()
setup(
name='pyoneall',
version='0.2.2',
version='0.2.3',
packages=['pyoneall'],
install_requires=['future'],
license='MIT License, see LICENSE file',
Expand Down

0 comments on commit fd4c536

Please sign in to comment.