Compare commits

...

13 Commits

Author SHA1 Message Date
Alex Kerney 6e31953118 Fix 413 response from Discourse due to empty dict passed as json 2020-08-11 13:17:52 -04:00
Christian Kindel 2ad158e195 Fix call to get group info by name 2020-08-10 21:13:26 -04:00
Ben Lopatin 719035e9a9 Fix classifier error 2020-07-21 17:22:55 -04:00
Ben Lopatin 3a4af08827 Bump version 1.1.0 2020-07-21 17:21:10 -04:00
Alex Kerney 5d334f1d80 Use immutable arguments and adjust naming to simplify 2020-07-21 17:14:54 -04:00
Alex Kerney 361bf77949 Allow client methods to override requests kwargs
Due to some changes in the Discourse API, certain methods now work better if redirects are allowed.

Get /c/{id}.json will redirect to /c/{category_slug}.json which will cause the client.category_topics(id) method to fail as the redirect is not followed by default.

Now the keyword arguments to requests.request can be overridden by individual methods. This is implemented for .category_topics
2020-07-21 17:14:54 -04:00
Alex Kerney aeb763c42c Authenticate via headers
Closes bennylope/pydiscourse#27
2019-12-04 18:09:00 -05:00
Ben Lopatin 69867b3c10 Run test workflow on pull request 2019-10-13 18:30:40 -04:00
Ben Lopatin c3ae5b3c76 Update return documentation 2019-10-13 18:12:23 -04:00
Ben Lopatin d02ab15d3f Formatting 2019-10-13 18:10:12 -04:00
Richard Leyton 9a8641e596 Added invite() and invite_link() methods 2019-10-12 18:09:52 +01:00
Ben Lopatin 802f018519 Replace bad link with route documentation
Closes gh-173
2019-10-06 12:12:43 -04:00
Ben Lopatin be74c4e5b7 Add GitHub Actions testing workflow (#24)
* Add GitHub Actions testing workflow

* Update named Python versions and dev status

* Use unittest.mock as default mock source

Fall back to package for Python 2.7

* Try installing mock outside of setup

* Switch to GitHub actions shield
2019-10-06 12:04:51 -04:00
10 changed files with 133 additions and 36 deletions
+23
View File
@@ -0,0 +1,23 @@
name: Tests
on: [push, pull_request]
jobs:
test:
name: Test on Python ${{ matrix.py_version }}
runs-on: ubuntu-latest
strategy:
matrix:
py_version: [2.7, 3.5, 3.6, 3.7]
steps:
- uses: actions/checkout@v1
- name: Set up Python ${{ matrix.py_version }}
uses: actions/setup-python@v1
with:
python-version: ${{ matrix.py_version }}
- name: Install mock for Python 2.7
run: pip install mock
- name: Run tests
run: python setup.py test
+1 -1
View File
@@ -9,4 +9,4 @@ Scott Nixon
Jason Dorweiler
Pierre-Alain Dupont
Karl Goetz
Alex Kerney
+5 -2
View File
@@ -69,5 +69,8 @@ Once running you can access the Discourse install at http://localhost:4000.
TODO
====
Refer to, https://github.com/discourse/discourse_api/blob/master/routes.txt for
a list of all operations available in Discourse.
For a list of all operations:
you can just run rake routes inside of the discourse repo to get an up to date list
Or check the old [`routes.txt`](https://github.com/discourse/discourse_api/blob/aa75df6cd851f0666f9e8071c4ef9dfdd39fc8f8/routes.txt) file, though this is certainly outdated.
+10
View File
@@ -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
View File
@@ -2,9 +2,9 @@
pydiscourse
===========
.. image:: https://secure.travis-ci.org/bennylope/pydiscourse.svg?branch=master
.. image:: https://github.com/bennylope/pydiscourse/workflows/Tests/badge.svg
:alt: Build Status
:target: http://travis-ci.org/bennylope/pydiscourse
:target: https://github.com/bennylope/pydiscourse/actions
.. image:: https://img.shields.io/badge/Check%20out%20the-Docs-blue.svg
:alt: Check out the Docs
+2 -2
View File
@@ -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 -1
View File
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
__version__ = "0.9.0"
__version__ = "1.1.0"
from pydiscourse.client import DiscourseClient
+77 -20
View File
@@ -84,6 +84,50 @@ class DiscourseClient(object):
"""
return self._get("/admin/users/{0}.json".format(user_id))
def invite(self, email, group_names, custom_message, **kwargs):
"""
Invite a user by email to join your forum
Args:
email: their email, will be used for activation and summary emails
group_names: the group names
custom_message: message to include
**kwargs: ???? what else can be sent through?
Returns:
API response body (dict)
"""
return self._post(
"/invites",
email=email,
group_names=group_names,
custom_message=custom_message,
**kwargs
)
def invite_link(self, email, group_names, custom_message, **kwargs):
"""
Generate an invite link for a user to join your forum
Args:
email: their email, will be used for activation and summary emails
group_names: the group names
custom_message: message to include
**kwargs: ???? what else can be sent through?
Returns:
Invite link
"""
return self._post(
"/invites/link",
email=email,
group_names=group_names,
custom_message=custom_message,
**kwargs
)
def create_user(self, name, username, email, password, **kwargs):
"""
Create a Discourse user
@@ -445,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):
"""
@@ -901,7 +949,7 @@ class DiscourseClient(object):
"""
Get all infos of a group by group name
"""
return self._get("/groups/{0}/members.json".format(group_name))
return self._get("/groups/{0}.json".format(group_name))
def create_group(
self,
@@ -1237,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:
@@ -1247,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:
@@ -1260,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:
@@ -1276,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:
@@ -1291,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
@@ -1301,16 +1351,21 @@ 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
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
@@ -1318,9 +1373,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,
@@ -1330,6 +1383,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
+3 -2
View File
@@ -34,15 +34,16 @@ setup(
]
},
classifiers=[
"Development Status :: 3 - Alpha",
"Development Status :: 5 - Production/Stable",
"Environment :: Web Environment",
"Intended Audience :: Developers",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
"Programming Language :: Python",
"Programming Language :: Python :: 2.7",
"Programming Language :: Python :: 3.4",
"Programming Language :: Python :: 3.5",
"Programming Language :: Python :: 3.6",
"Programming Language :: Python :: 3.7",
'Programming Language :: Python :: Implementation :: PyPy',
],
zip_safe=False,
+9 -6
View File
@@ -1,10 +1,13 @@
import sys
import unittest
import mock
try:
from unittest import mock
except ImportError:
import mock
from pydiscourse import client
import sys
if sys.version_info < (3,):
@@ -46,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):