Compare commits
6 Commits
invite-urls
...
v1.1.0
| Author | SHA1 | Date | |
|---|---|---|---|
| 719035e9a9 | |||
| 3a4af08827 | |||
| 5d334f1d80 | |||
| 361bf77949 | |||
| aeb763c42c | |||
| 69867b3c10 |
@@ -1,6 +1,6 @@
|
||||
name: Tests
|
||||
|
||||
on: [push]
|
||||
on: [push, pull_request]
|
||||
|
||||
jobs:
|
||||
test:
|
||||
|
||||
@@ -9,4 +9,4 @@ Scott Nixon
|
||||
Jason Dorweiler
|
||||
Pierre-Alain Dupont
|
||||
Karl Goetz
|
||||
|
||||
Alex Kerney
|
||||
|
||||
+10
@@ -3,6 +3,16 @@
|
||||
Release history
|
||||
===============
|
||||
|
||||
1.1.0
|
||||
-----
|
||||
|
||||
- Added ability to follow redirects in requests
|
||||
|
||||
1.0.0
|
||||
-----
|
||||
|
||||
- Authenticate with headers
|
||||
|
||||
0.9.0
|
||||
-----
|
||||
|
||||
|
||||
+2
-2
@@ -51,9 +51,9 @@ copyright = u'2014, Marc Sibson'
|
||||
# built documents.
|
||||
#
|
||||
# The short X.Y version.
|
||||
version = '0.9'
|
||||
version = '1.1'
|
||||
# The full version, including alpha/beta/rc tags.
|
||||
release = '0.9.0'
|
||||
release = '1.1.0'
|
||||
|
||||
# The language for content autogenerated by Sphinx. Refer to documentation
|
||||
# for a list of supported languages.
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
__version__ = "0.9.0"
|
||||
__version__ = "1.1.0"
|
||||
|
||||
from pydiscourse.client import DiscourseClient
|
||||
|
||||
+35
-19
@@ -489,7 +489,11 @@ class DiscourseClient(object):
|
||||
JSON API response
|
||||
|
||||
"""
|
||||
return self._get("/c/{0}.json".format(category_id), **kwargs)
|
||||
return self._get(
|
||||
"/c/{0}.json".format(category_id),
|
||||
override_request_kwargs={"allow_redirects": True},
|
||||
**kwargs
|
||||
)
|
||||
|
||||
def hot_topics(self, **kwargs):
|
||||
"""
|
||||
@@ -1281,7 +1285,7 @@ class DiscourseClient(object):
|
||||
kwargs["parent_tag_name"] = parent_tag_name
|
||||
return self._post("/tag_groups", json=True, **kwargs)["tag_group"]
|
||||
|
||||
def _get(self, path, **kwargs):
|
||||
def _get(self, path, override_request_kwargs=None, **kwargs):
|
||||
"""
|
||||
|
||||
Args:
|
||||
@@ -1291,9 +1295,9 @@ class DiscourseClient(object):
|
||||
Returns:
|
||||
|
||||
"""
|
||||
return self._request(GET, path, params=kwargs)
|
||||
return self._request(GET, path, params=kwargs, override_request_kwargs=override_request_kwargs)
|
||||
|
||||
def _put(self, path, json=False, **kwargs):
|
||||
def _put(self, path, json=False, override_request_kwargs=None, **kwargs):
|
||||
"""
|
||||
|
||||
Args:
|
||||
@@ -1304,12 +1308,12 @@ class DiscourseClient(object):
|
||||
|
||||
"""
|
||||
if not json:
|
||||
return self._request(PUT, path, data=kwargs)
|
||||
return self._request(PUT, path, data=kwargs, override_request_kwargs=override_request_kwargs)
|
||||
|
||||
else:
|
||||
return self._request(PUT, path, json=kwargs)
|
||||
return self._request(PUT, path, json=kwargs, override_request_kwargs=override_request_kwargs)
|
||||
|
||||
def _post(self, path, files={}, json=False, **kwargs):
|
||||
def _post(self, path, files=None, json=False, override_request_kwargs=None, **kwargs):
|
||||
"""
|
||||
|
||||
Args:
|
||||
@@ -1320,12 +1324,12 @@ class DiscourseClient(object):
|
||||
|
||||
"""
|
||||
if not json:
|
||||
return self._request(POST, path, files=files, data=kwargs)
|
||||
return self._request(POST, path, files=files, data=kwargs, override_request_kwargs=override_request_kwargs)
|
||||
|
||||
else:
|
||||
return self._request(POST, path, files=files, json=kwargs)
|
||||
return self._request(POST, path, files=files, json=kwargs, override_request_kwargs=override_request_kwargs)
|
||||
|
||||
def _delete(self, path, **kwargs):
|
||||
def _delete(self, path, override_request_kwargs=None, **kwargs):
|
||||
"""
|
||||
|
||||
Args:
|
||||
@@ -1335,9 +1339,11 @@ class DiscourseClient(object):
|
||||
Returns:
|
||||
|
||||
"""
|
||||
return self._request(DELETE, path, params=kwargs)
|
||||
return self._request(DELETE, path, params=kwargs, override_request_kwargs=override_request_kwargs)
|
||||
|
||||
def _request(self, verb, path, params={}, files={}, data={}, json={}):
|
||||
def _request(
|
||||
self, verb, path, params=None, files=None, data=None, json=None, override_request_kwargs=None
|
||||
):
|
||||
"""
|
||||
Executes HTTP request to API and handles response
|
||||
|
||||
@@ -1345,17 +1351,25 @@ class DiscourseClient(object):
|
||||
verb: HTTP verb as string: GET, DELETE, PUT, POST
|
||||
path: the path on the Discourse API
|
||||
params: dictionary of parameters to include to the API
|
||||
override_request_kwargs: dictionary of requests.request keyword arguments to override defaults
|
||||
|
||||
Returns:
|
||||
dictionary of response body data or None
|
||||
|
||||
"""
|
||||
params["api_key"] = self.api_key
|
||||
if "api_username" not in params:
|
||||
params["api_username"] = self.api_username
|
||||
params = params or {}
|
||||
files = files or {}
|
||||
data = data or {}
|
||||
json = json or {}
|
||||
override_request_kwargs = override_request_kwargs or {}
|
||||
|
||||
url = self.host + path
|
||||
|
||||
headers = {"Accept": "application/json; charset=utf-8"}
|
||||
headers = {
|
||||
"Accept": "application/json; charset=utf-8",
|
||||
"Api-Key": self.api_key,
|
||||
"Api-Username": self.api_username,
|
||||
}
|
||||
|
||||
# How many times should we retry if rate limited
|
||||
retry_count = 4
|
||||
@@ -1363,9 +1377,7 @@ class DiscourseClient(object):
|
||||
retry_backoff = 1
|
||||
|
||||
while retry_count > 0:
|
||||
response = requests.request(
|
||||
verb,
|
||||
url,
|
||||
request_kwargs = dict(
|
||||
allow_redirects=False,
|
||||
params=params,
|
||||
files=files,
|
||||
@@ -1375,6 +1387,10 @@ class DiscourseClient(object):
|
||||
timeout=self.timeout,
|
||||
)
|
||||
|
||||
request_kwargs.update(override_request_kwargs)
|
||||
|
||||
response = requests.request(verb, url, **request_kwargs)
|
||||
|
||||
log.debug("response %s: %s", response.status_code, repr(response.text))
|
||||
if response.ok:
|
||||
break
|
||||
|
||||
@@ -34,7 +34,7 @@ setup(
|
||||
]
|
||||
},
|
||||
classifiers=[
|
||||
"Development Status :: 5 - Production/Stable"
|
||||
"Development Status :: 5 - Production/Stable",
|
||||
"Environment :: Web Environment",
|
||||
"Intended Audience :: Developers",
|
||||
"License :: OSI Approved :: MIT License",
|
||||
|
||||
@@ -49,12 +49,12 @@ class ClientBaseTestCase(unittest.TestCase):
|
||||
self.assertEqual(args[0], verb)
|
||||
self.assertEqual(args[1], self.host + url)
|
||||
|
||||
kwargs = kwargs["params"]
|
||||
self.assertEqual(kwargs.pop("api_username"), self.api_username)
|
||||
self.assertEqual(kwargs.pop("api_key"), self.api_key)
|
||||
headers = kwargs["headers"]
|
||||
self.assertEqual(headers.pop("Api-Username"), self.api_username)
|
||||
self.assertEqual(headers.pop("Api-Key"), self.api_key)
|
||||
|
||||
if verb == "GET":
|
||||
self.assertEqual(kwargs, params)
|
||||
self.assertEqual(kwargs["params"], params)
|
||||
|
||||
|
||||
class TestClientRequests(ClientBaseTestCase):
|
||||
|
||||
Reference in New Issue
Block a user