From 86ffbb2b4a7da63b1dd6f7264df4a3ce4108a80c Mon Sep 17 00:00:00 2001 From: novokrest Date: Fri, 11 May 2018 07:52:09 +0300 Subject: [PATCH] stack_overflow: Pass API query parameters as `params` dict in requests.get(). --- .../fixtures/test_incorrect_query.json | 8 +++++++- .../stack_overflow/fixtures/test_multi_word.json | 8 +++++++- .../stack_overflow/fixtures/test_number_query.json | 8 +++++++- .../stack_overflow/fixtures/test_single_word.json | 8 +++++++- .../stack_overflow/fixtures/test_status_code.json | 8 +++++++- .../zulip_bots/bots/stack_overflow/stack_overflow.py | 12 ++++++++---- 6 files changed, 43 insertions(+), 9 deletions(-) diff --git a/zulip_bots/zulip_bots/bots/stack_overflow/fixtures/test_incorrect_query.json b/zulip_bots/zulip_bots/bots/stack_overflow/fixtures/test_incorrect_query.json index 7093910..26c993c 100644 --- a/zulip_bots/zulip_bots/bots/stack_overflow/fixtures/test_incorrect_query.json +++ b/zulip_bots/zulip_bots/bots/stack_overflow/fixtures/test_incorrect_query.json @@ -1,6 +1,12 @@ { "request": { - "api_url":"http://api.stackexchange.com/2.2/search/advanced?order=desc&sort=relevance&site=stackoverflow&title=narendra" + "api_url":"http://api.stackexchange.com/2.2/search/advanced", + "params": { + "order": "desc", + "sort": "relevance", + "site": "stackoverflow", + "title": "narendra" + } }, "response": { "data": { diff --git a/zulip_bots/zulip_bots/bots/stack_overflow/fixtures/test_multi_word.json b/zulip_bots/zulip_bots/bots/stack_overflow/fixtures/test_multi_word.json index c20004a..5b3a053 100644 --- a/zulip_bots/zulip_bots/bots/stack_overflow/fixtures/test_multi_word.json +++ b/zulip_bots/zulip_bots/bots/stack_overflow/fixtures/test_multi_word.json @@ -1,6 +1,12 @@ { "request": { - "api_url":"http://api.stackexchange.com/2.2/search/advanced?order=desc&sort=relevance&site=stackoverflow&title=what%20is%20flutter" + "api_url":"http://api.stackexchange.com/2.2/search/advanced", + "params": { + "order": "desc", + "sort": "relevance", + "site": "stackoverflow", + "title": "what is flutter" + } }, "response": { "data": { diff --git a/zulip_bots/zulip_bots/bots/stack_overflow/fixtures/test_number_query.json b/zulip_bots/zulip_bots/bots/stack_overflow/fixtures/test_number_query.json index ed6e291..1e4f38d 100644 --- a/zulip_bots/zulip_bots/bots/stack_overflow/fixtures/test_number_query.json +++ b/zulip_bots/zulip_bots/bots/stack_overflow/fixtures/test_number_query.json @@ -1,6 +1,12 @@ { "request": { - "api_url":"http://api.stackexchange.com/2.2/search/advanced?order=desc&sort=relevance&site=stackoverflow&title=113" + "api_url":"http://api.stackexchange.com/2.2/search/advanced", + "params": { + "order": "desc", + "sort": "relevance", + "site": "stackoverflow", + "title": "113" + } }, "response": { "data": { diff --git a/zulip_bots/zulip_bots/bots/stack_overflow/fixtures/test_single_word.json b/zulip_bots/zulip_bots/bots/stack_overflow/fixtures/test_single_word.json index 19d8942..1f64159 100644 --- a/zulip_bots/zulip_bots/bots/stack_overflow/fixtures/test_single_word.json +++ b/zulip_bots/zulip_bots/bots/stack_overflow/fixtures/test_single_word.json @@ -1,6 +1,12 @@ { "request": { - "api_url":"http://api.stackexchange.com/2.2/search/advanced?order=desc&sort=relevance&site=stackoverflow&title=restful" + "api_url":"http://api.stackexchange.com/2.2/search/advanced", + "params": { + "order": "desc", + "sort": "relevance", + "site": "stackoverflow", + "title": "restful" + } }, "response": { "data": { diff --git a/zulip_bots/zulip_bots/bots/stack_overflow/fixtures/test_status_code.json b/zulip_bots/zulip_bots/bots/stack_overflow/fixtures/test_status_code.json index 34480ae..c0aa419 100644 --- a/zulip_bots/zulip_bots/bots/stack_overflow/fixtures/test_status_code.json +++ b/zulip_bots/zulip_bots/bots/stack_overflow/fixtures/test_status_code.json @@ -1,6 +1,12 @@ { "request": { - "api_url":"http://api.stackexchange.com/2.2/search/advanced?order=desc&sort=relevance&site=stackoverflow&title=Zulip" + "api_url":"http://api.stackexchange.com/2.2/search/advanced", + "params": { + "order": "desc", + "sort": "relevance", + "site": "stackoverflow", + "title": "Zulip" + } }, "response": { "data": { diff --git a/zulip_bots/zulip_bots/bots/stack_overflow/stack_overflow.py b/zulip_bots/zulip_bots/bots/stack_overflow/stack_overflow.py index cd1e1e1..f0c1f3f 100644 --- a/zulip_bots/zulip_bots/bots/stack_overflow/stack_overflow.py +++ b/zulip_bots/zulip_bots/bots/stack_overflow/stack_overflow.py @@ -45,11 +45,15 @@ class StackOverflowHandler(object): if query == '' or query == 'help': return help_text - query_stack_link = ('http://api.stackexchange.com/2.2/search/advanced?' - 'order=desc&sort=relevance&site=stackoverflow&title=%s' - % (urllib.parse.quote(query),)) + query_stack_url = 'http://api.stackexchange.com/2.2/search/advanced' + query_stack_params = dict( + order='desc', + sort='relevance', + site='stackoverflow', + title=query + ) try: - data = requests.get(query_stack_link) + data = requests.get(query_stack_url, params=query_stack_params) except requests.exceptions.RequestException: logging.error('broken link')