Skip to content

Commit 0900ecd

Browse files
Merge pull request #30 from janrain/topic/add-customer-user-agent
Add custom user-agent string
2 parents 3c5d158 + 94b4525 commit 0900ecd

File tree

5 files changed

+35
-8
lines changed

5 files changed

+35
-8
lines changed

janrain/capture/__init__.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
11
from janrain.capture.api import Api
22
from janrain.capture.exceptions import *
3-
4-
VERSION = (0, 3, 7)
5-
6-
def get_version():
7-
return "%s.%s.%s" % (VERSION[0], VERSION[1], VERSION[2])
8-
9-
__version__ = get_version()
3+
from janrain.capture.version import *

janrain/capture/api.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# pylint: disable=E0611
33
from __future__ import unicode_literals
44
from janrain.capture.exceptions import ApiResponseError
5+
from janrain.capture.version import get_version
56
from json import dumps as to_json
67
from contextlib import closing
78
from base64 import b64encode
@@ -151,15 +152,23 @@ class Api(object):
151152
api = janrain.capture.Api("https://...", defaults)
152153
count = api.call("entity.count", type_name="user")
153154
"""
154-
def __init__(self, api_url, defaults={}, compress=True, sign_requests=True):
155+
def __init__(self, api_url, defaults={}, compress=True, sign_requests=True,
156+
user_agent=None):
157+
155158
if api_url[0:4] == "http":
156159
self.api_url = api_url
157160
else:
158161
self.api_url = "https://" + api_url
162+
159163
self.defaults = defaults
160164
self.sign_requests = sign_requests
161165
self.compress = compress
162166

167+
if not user_agent:
168+
self.user_agent = "janrain-python-api {}".format(get_version())
169+
else:
170+
self.user_agent = user_agent
171+
163172

164173
def call(self, api_call, **kwargs):
165174
"""
@@ -197,6 +206,9 @@ def call(self, api_call, **kwargs):
197206
else:
198207
headers = {}
199208

209+
# Custom user agent string
210+
headers['User-Agent'] = self.user_agent
211+
200212
# Print the parameters (for debugging)
201213
print_params = params.copy()
202214
if 'client_secret' in print_params:

janrain/capture/cli.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,8 @@ def main():
142142
help="sign HTTP requests")
143143
parser.add_argument('-b', '--debug', action='store_true',
144144
help="log debug messages to stdout")
145+
parser.add_argument('-a', '--user-agent',
146+
help="user agent to use for the API call")
145147
args = parser.parse_args()
146148

147149
try:
@@ -152,6 +154,9 @@ def main():
152154
if args.disable_signed_requests:
153155
api.sign_requests = False
154156

157+
if args.user_agent:
158+
api.user_agent = args.user_agent
159+
155160
if args.debug:
156161
logging.basicConfig(level=logging.DEBUG)
157162

janrain/capture/test/test_api.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from janrain.capture import Api, config
44
from janrain.capture.api import api_encode
55
from janrain.capture.exceptions import *
6+
from janrain.capture import version
67
from os.path import exists
78
from os.path import expanduser
89
from os import environ
@@ -44,3 +45,12 @@ def test_api_object(self):
4445
# oauth/token returns 400 or 401 which should *not* be a HTTPError
4546
with self.assertRaises(ApiResponseError):
4647
self.api.call("/oauth/token")
48+
49+
def test_user_agent(self):
50+
# default user agent string will be used if not defined when object is
51+
# instantiated
52+
api1 = Api("foo.janrain.com")
53+
self.assertIsNotNone(api1.user_agent)
54+
55+
api2 = Api("foo.janrain.com", user_agent="foobar")
56+
self.assertEqual(api2.user_agent, "foobar")

janrain/capture/version.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
VERSION = (0, 4, 0)
2+
3+
def get_version():
4+
return "%s.%s.%s" % (VERSION[0], VERSION[1], VERSION[2])
5+
6+
__version__ = get_version()

0 commit comments

Comments
 (0)