Repository: alerta/python-alerta-client

Issue:
SDK passing page size as "None" if not set
**Issue Summary**

api_1  | 192.168.208.4 - - [19/Apr/2020 09:58:23] "GET /alert/0a4e3cdd-65c2-4e20-8d9e-678123fef2af/notes?page=1&page-size=None HTTP/1.1" 200 -


**Environment**
- OS: [eg. Linux, Mac, Windows]
- API version: [eg. 6.8.1]
- Deployment: [eg. Docker, Heroku, AWS, GCloud, self-hosted]
- For self-hosted, WSGI environment: [eg. nginx/uwsgi, apache/mod_wsgi]

- Database: [eg. MongoDB, Postgres]

- Server config:
  Auth enabled? Yes/No
  Auth provider? Google, GitHub, OpenID, Basic, LDAP, etc
  Customer views? Yes/No
  (or provide the `/config` endpoint output)

- web UI version: [eg. 6.8.1]
- CLI version: [eg. 6.8.1]

**To Reproduce**
Steps to reproduce the behavior:
1. Go to '...'
2. Click on '....' or Run '...'
3. Scroll down to '....'
4. See error (include command output)

For web app issues, include any web browser JavaScript console errors.

**Expected behavior**
A clear and concise description of what you expected to happen.

**Screenshots**
If applicable, add screenshots to help explain your problem.

**Additional context**
Add any other context about the problem here.


NOTE: Please provide as much information about your issue as possible.
Failure to provide basic details about your specific environment make
it impossible to know if an issue has already been fixed, can delay a
response and may result in your issue being closed without a resolution.



Discussion:

Patch:
diff --git a/alertaclient/api.py b/alertaclient/api.py
index 9b01293..391d7b3 100644
--- a/alertaclient/api.py
+++ b/alertaclient/api.py
@@ -510,6 +510,9 @@ class TokenAuth(AuthBase):
 
 class HTTPClient:
 
+    DEFAULT_PAGE_NUMBER = 1
+    DEFAULT_PAGE_SIZE = 50
+
     def __init__(self, endpoint, key=None, secret=None, token=None, username=None, password=None, timeout=30.0, ssl_verify=True, headers=None, debug=False):
         self.endpoint = endpoint
         self.auth = None
@@ -542,9 +545,9 @@ class HTTPClient:
     def get(self, path, query=None, **kwargs):
         query = query or []
         if 'page' in kwargs:
-            query.append(('page', kwargs['page']))
+            query.append(('page', kwargs.get('page') or self.DEFAULT_PAGE_NUMBER))
         if 'page_size' in kwargs:
-            query.append(('page-size', kwargs['page_size']))
+            query.append(('page-size', kwargs.get('page_size') or self.DEFAULT_PAGE_SIZE))
 
         url = self.endpoint + path + '?' + urlencode(query, doseq=True)
         try:


Test patch:
diff --git a/tests/unit/test_http_client.py b/tests/unit/test_http_client.py
new file mode 100644
index 0000000..e0deb7e
--- /dev/null
+++ b/tests/unit/test_http_client.py
@@ -0,0 +1,47 @@
+import unittest
+
+import requests_mock
+
+from alertaclient.api import HTTPClient
+
+
+class HttpClientTestCase(unittest.TestCase):
+
+    def setUp(self):
+        self.http = HTTPClient(endpoint='https://httpbin.org')
+
+    @requests_mock.mock()
+    def test_query_string(self, m):
+
+        m.get('https://httpbin.org/get', text='{}')
+        m.post('https://httpbin.org/post', text='{}')
+        m.put('https://httpbin.org/put', text='{}')
+        m.delete('https://httpbin.org/delete', text='{}')
+
+        self.http.get(path='/get', query=None)
+        self.http.get(path='/get', query=(('foo', 'bar'), ('quxx', 'baz')))
+        self.http.get(path='/get', query=None, page=None)
+        self.http.get(path='/get', query=None, page=2, page_size=None)
+        self.http.get(path='/get', query=None, page=None, page_size=None)
+        self.http.get(path='/get', query=None, page=2, page_size=100)
+        self.http.get(path='/get', query=None, page_size=20)
+        self.http.get(path='/get', query=None, page=None, page_size=200)
+
+        history = m.request_history
+        self.assertEqual(history[0].url, 'https://httpbin.org/get')
+        self.assertEqual(history[1].url, 'https://httpbin.org/get?foo=bar&quxx=baz')
+        self.assertEqual(history[2].url, 'https://httpbin.org/get?page=1')
+        self.assertEqual(history[3].url, 'https://httpbin.org/get?page=2&page-size=50')
+        self.assertEqual(history[4].url, 'https://httpbin.org/get?page=1&page-size=50')
+        self.assertEqual(history[5].url, 'https://httpbin.org/get?page=2&page-size=100')
+        self.assertEqual(history[6].url, 'https://httpbin.org/get?page-size=20')
+        self.assertEqual(history[7].url, 'https://httpbin.org/get?page=1&page-size=200')
+
+        self.http.post(path='/post', data='{}')
+        self.assertEqual(history[8].url, 'https://httpbin.org/post')
+
+        self.http.put(path='/put', data='{}')
+        self.assertEqual(history[9].url, 'https://httpbin.org/put')
+
+        self.http.delete(path='/delete')
+        self.assertEqual(history[10].url, 'https://httpbin.org/delete')