Skip to content

Commit b9a945f

Browse files
authored
Merge pull request #80 from pusher/899-report-name-and-version
[PROD-899] report library name and version in X-Pusher-Library header
2 parents ae73d77 + 438a4db commit b9a945f

File tree

4 files changed

+41
-5
lines changed

4 files changed

+41
-5
lines changed

pusher/http.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
division)
55
from pusher.errors import *
66
from pusher.signature import sign
7+
from pusher.version import VERSION
78

89
import copy
910
import hashlib
@@ -117,7 +118,7 @@ def base_url(self):
117118

118119
@property
119120
def headers(self):
121+
hdrs = {"X-Pusher-Library": "pusher-http-python " + VERSION}
120122
if self.method == POST:
121-
return {"Content-Type": "application/json"}
122-
else:
123-
return {}
123+
hdrs["Content-Type"] = "application/json"
124+
return hdrs

pusher/version.py

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Don't change the format of this line: the version is extracted by ../setup.py
2+
VERSION = '1.4rc3'

pusher_tests/test_request.py

+23-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from __future__ import print_function, absolute_import, division
44

55
import unittest
6+
import re
67

78
from pusher import Pusher
89
from pusher.http import Request
@@ -41,13 +42,34 @@ def test_post_signature_generation(self):
4142
}
4243

4344
with mock.patch('time.time', return_value=1000):
44-
# patching this, because json can be unambiguously parsed, but not
45+
# patching this, because json can be unambiguously parsed, but not
4546
# unambiguously generated (think whitespace).
4647
with mock.patch('json.dumps', return_value='{"foo": "bar"}') as json_dumps_mock:
4748
req = Request(conf, u'POST', u'/some/obscure/api', {u'foo': u'bar'})
4849
self.assertEqual(req.query_params, expected)
4950

5051
json_dumps_mock.assert_called_once_with({u"foo": u"bar"})
5152

53+
# Copied wholesale from https://github.com/python/cpython/blob/2d305e1c46abfcd609bf8b2dff8d2065e6af8ab2/Lib/unittest/case.py#L1279-L1289
54+
# This can be removed when we no longer support Python 2.6
55+
def assertRegexpMatches(self, text, expected_regex, msg=None):
56+
"""Fail the test unless the text matches the regular expression."""
57+
if isinstance(expected_regex, (str, bytes)):
58+
assert expected_regex, "expected_regex must not be empty."
59+
expected_regex = re.compile(expected_regex)
60+
if not expected_regex.search(text):
61+
standardMsg = "Regex didn't match: %r not found in %r" % (
62+
expected_regex.pattern, text)
63+
# _formatMessage ensures the longMessage option is respected
64+
msg = self._formatMessage(msg, standardMsg)
65+
raise self.failureException(msg)
66+
67+
def test_x_pusher_library_header(self):
68+
conf = Pusher.from_url(u'http://key:secret@somehost/apps/4')
69+
req = Request(conf, u'GET', u'/some/obscure/api', {u'foo': u'bar'})
70+
self.assertTrue('X-Pusher-Library' in req.headers)
71+
pusherLib = req.headers['X-Pusher-Library']
72+
self.assertRegexpMatches(pusherLib, r'^pusher-http-python \d+(\.\d+)+(rc\d+)?$')
73+
5274
if __name__ == '__main__':
5375
unittest.main()

setup.py

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,19 @@
11
# -*- coding: utf-8 -*-
22
from setuptools import setup
3+
import os
4+
import re
5+
6+
# Lovingly adapted from https://github.com/kennethreitz/requests/blob/39d693548892057adad703fda630f925e61ee557/setup.py#L50-L55
7+
with open(os.path.join(os.path.dirname(os.path.abspath(__file__)), 'pusher/version.py'), 'r') as fd:
8+
VERSION = re.search(r'^VERSION = [\']([^\']*)[\']',
9+
fd.read(), re.MULTILINE).group(1)
10+
11+
if not VERSION:
12+
raise RuntimeError('Ensure `VERSION` is correctly set in ./pusher/version.py')
13+
314
setup(
415
name='pusher',
5-
version='1.4rc3',
16+
version=VERSION,
617
description='A Python library to interract with the Pusher API',
718
url='https://github.com/pusher/pusher-http-python',
819
author='Pusher',

0 commit comments

Comments
 (0)