From a38917723af4e105e58b093645c7a461cbd480b5 Mon Sep 17 00:00:00 2001 From: novokrest Date: Fri, 11 May 2018 07:20:37 +0300 Subject: [PATCH] idonethis: Pass API query parameters as `params` dict in requests methods. --- .../bots/idonethis/fixtures/api_noop.json | 3 ++- .../bots/idonethis/fixtures/team_list.json | 3 ++- .../bots/idonethis/fixtures/test_401.json | 3 ++- .../bots/idonethis/fixtures/test_create_entry.json | 3 ++- .../fixtures/test_create_entry_team_2.json | 3 ++- .../bots/idonethis/fixtures/test_entries_list.json | 5 ++++- .../bots/idonethis/fixtures/test_show_team.json | 3 ++- zulip_bots/zulip_bots/bots/idonethis/idonethis.py | 13 ++++++++----- 8 files changed, 24 insertions(+), 12 deletions(-) diff --git a/zulip_bots/zulip_bots/bots/idonethis/fixtures/api_noop.json b/zulip_bots/zulip_bots/bots/idonethis/fixtures/api_noop.json index 1b055b5..331f8d5 100644 --- a/zulip_bots/zulip_bots/bots/idonethis/fixtures/api_noop.json +++ b/zulip_bots/zulip_bots/bots/idonethis/fixtures/api_noop.json @@ -3,7 +3,8 @@ "api_url": "https://beta.idonethis.com/api/v2/noop", "headers": { "Authorization": "Token 12345678" - } + }, + "params": null }, "response": {"doesnt": "matter"}, "response-headers": { diff --git a/zulip_bots/zulip_bots/bots/idonethis/fixtures/team_list.json b/zulip_bots/zulip_bots/bots/idonethis/fixtures/team_list.json index d45e542..30d5312 100644 --- a/zulip_bots/zulip_bots/bots/idonethis/fixtures/team_list.json +++ b/zulip_bots/zulip_bots/bots/idonethis/fixtures/team_list.json @@ -3,7 +3,8 @@ "api_url": "https://beta.idonethis.com/api/v2/teams", "headers": { "Authorization": "Token 12345678" - } + }, + "params": null }, "response": [ { diff --git a/zulip_bots/zulip_bots/bots/idonethis/fixtures/test_401.json b/zulip_bots/zulip_bots/bots/idonethis/fixtures/test_401.json index bab3b09..a4d0adb 100644 --- a/zulip_bots/zulip_bots/bots/idonethis/fixtures/test_401.json +++ b/zulip_bots/zulip_bots/bots/idonethis/fixtures/test_401.json @@ -3,7 +3,8 @@ "api_url": "https://beta.idonethis.com/api/v2/teams", "headers": { "Authorization": "Token 87654321" - } + }, + "params": null }, "response": { "error": "Invalid API Authentication" diff --git a/zulip_bots/zulip_bots/bots/idonethis/fixtures/test_create_entry.json b/zulip_bots/zulip_bots/bots/idonethis/fixtures/test_create_entry.json index 9a560ab..427c297 100644 --- a/zulip_bots/zulip_bots/bots/idonethis/fixtures/test_create_entry.json +++ b/zulip_bots/zulip_bots/bots/idonethis/fixtures/test_create_entry.json @@ -8,7 +8,8 @@ }, "headers": { "Authorization": "Token 12345678" - } + }, + "params": null }, "response": { "body": "something and something else", diff --git a/zulip_bots/zulip_bots/bots/idonethis/fixtures/test_create_entry_team_2.json b/zulip_bots/zulip_bots/bots/idonethis/fixtures/test_create_entry_team_2.json index d8a2f5a..cf27b10 100644 --- a/zulip_bots/zulip_bots/bots/idonethis/fixtures/test_create_entry_team_2.json +++ b/zulip_bots/zulip_bots/bots/idonethis/fixtures/test_create_entry_team_2.json @@ -8,7 +8,8 @@ }, "headers": { "Authorization": "Token 12345678" - } + }, + "params": null }, "response": { "body": "something and something else", diff --git a/zulip_bots/zulip_bots/bots/idonethis/fixtures/test_entries_list.json b/zulip_bots/zulip_bots/bots/idonethis/fixtures/test_entries_list.json index 5a3ec9e..463533f 100644 --- a/zulip_bots/zulip_bots/bots/idonethis/fixtures/test_entries_list.json +++ b/zulip_bots/zulip_bots/bots/idonethis/fixtures/test_entries_list.json @@ -1,8 +1,11 @@ { "request": { - "api_url": "https://beta.idonethis.com/api/v2/entries?team_id=31415926535", + "api_url": "https://beta.idonethis.com/api/v2/entries", "headers": { "Authorization": "Token 12345678" + }, + "params": { + "team_id": "31415926535" } }, "response": [ diff --git a/zulip_bots/zulip_bots/bots/idonethis/fixtures/test_show_team.json b/zulip_bots/zulip_bots/bots/idonethis/fixtures/test_show_team.json index 43febcb..c88a355 100644 --- a/zulip_bots/zulip_bots/bots/idonethis/fixtures/test_show_team.json +++ b/zulip_bots/zulip_bots/bots/idonethis/fixtures/test_show_team.json @@ -3,7 +3,8 @@ "api_url": "https://beta.idonethis.com/api/v2/teams/31415926535", "headers": { "Authorization": "Token 12345678" - } + }, + "params": null }, "response": { "hash_id": "31415926535", diff --git a/zulip_bots/zulip_bots/bots/idonethis/idonethis.py b/zulip_bots/zulip_bots/bots/idonethis/idonethis.py index 88af20e..54514d9 100644 --- a/zulip_bots/zulip_bots/bots/idonethis/idonethis.py +++ b/zulip_bots/zulip_bots/bots/idonethis/idonethis.py @@ -25,12 +25,15 @@ class UnknownCommandSyntax(Exception): class UnspecifiedProblemException(Exception): pass -def make_API_request(endpoint: str, method: str="GET", body: Optional[Dict[str, str]]=None) -> Any: +def make_API_request(endpoint: str, + method: str="GET", + body: Optional[Dict[str, str]]=None, + params: Optional[Dict[str, str]]=None) -> Any: headers = {'Authorization': 'Token ' + api_key} if method == "GET": - r = requests.get(API_BASE_URL + endpoint, headers=headers) + r = requests.get(API_BASE_URL + endpoint, headers=headers, params=params) elif method == "POST": - r = requests.post(API_BASE_URL + endpoint, headers=headers, json=body) + r = requests.post(API_BASE_URL + endpoint, headers=headers, params=params, json=body) if r.status_code == 200: return r.json() elif r.status_code == 401 and 'error' in r.json() and r.json()['error'] == "Invalid API Authentication": @@ -54,9 +57,9 @@ def api_show_users(hash_id: str) -> Any: def api_list_entries(team_id: Optional[str]=None) -> Any: if team_id: - return make_API_request("/entries?team_id={}".format(team_id)) + return make_API_request("/entries", params=dict(team_id=team_id)) else: - return make_API_request("/entries".format(team_id)) + return make_API_request("/entries") def api_create_entry(body: str, team_id: str) -> Any: return make_API_request("/entries", "POST", {"body": body, "team_id": team_id})