idonethis: Pass API query parameters as params dict in requests methods.

This commit is contained in:
novokrest 2018-05-11 07:20:37 +03:00 committed by Tim Abbott
parent e9a518e93e
commit a38917723a
8 changed files with 24 additions and 12 deletions

View file

@ -3,7 +3,8 @@
"api_url": "https://beta.idonethis.com/api/v2/noop", "api_url": "https://beta.idonethis.com/api/v2/noop",
"headers": { "headers": {
"Authorization": "Token 12345678" "Authorization": "Token 12345678"
} },
"params": null
}, },
"response": {"doesnt": "matter"}, "response": {"doesnt": "matter"},
"response-headers": { "response-headers": {

View file

@ -3,7 +3,8 @@
"api_url": "https://beta.idonethis.com/api/v2/teams", "api_url": "https://beta.idonethis.com/api/v2/teams",
"headers": { "headers": {
"Authorization": "Token 12345678" "Authorization": "Token 12345678"
} },
"params": null
}, },
"response": [ "response": [
{ {

View file

@ -3,7 +3,8 @@
"api_url": "https://beta.idonethis.com/api/v2/teams", "api_url": "https://beta.idonethis.com/api/v2/teams",
"headers": { "headers": {
"Authorization": "Token 87654321" "Authorization": "Token 87654321"
} },
"params": null
}, },
"response": { "response": {
"error": "Invalid API Authentication" "error": "Invalid API Authentication"

View file

@ -8,7 +8,8 @@
}, },
"headers": { "headers": {
"Authorization": "Token 12345678" "Authorization": "Token 12345678"
} },
"params": null
}, },
"response": { "response": {
"body": "something and something else", "body": "something and something else",

View file

@ -8,7 +8,8 @@
}, },
"headers": { "headers": {
"Authorization": "Token 12345678" "Authorization": "Token 12345678"
} },
"params": null
}, },
"response": { "response": {
"body": "something and something else", "body": "something and something else",

View file

@ -1,8 +1,11 @@
{ {
"request": { "request": {
"api_url": "https://beta.idonethis.com/api/v2/entries?team_id=31415926535", "api_url": "https://beta.idonethis.com/api/v2/entries",
"headers": { "headers": {
"Authorization": "Token 12345678" "Authorization": "Token 12345678"
},
"params": {
"team_id": "31415926535"
} }
}, },
"response": [ "response": [

View file

@ -3,7 +3,8 @@
"api_url": "https://beta.idonethis.com/api/v2/teams/31415926535", "api_url": "https://beta.idonethis.com/api/v2/teams/31415926535",
"headers": { "headers": {
"Authorization": "Token 12345678" "Authorization": "Token 12345678"
} },
"params": null
}, },
"response": { "response": {
"hash_id": "31415926535", "hash_id": "31415926535",

View file

@ -25,12 +25,15 @@ class UnknownCommandSyntax(Exception):
class UnspecifiedProblemException(Exception): class UnspecifiedProblemException(Exception):
pass 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} headers = {'Authorization': 'Token ' + api_key}
if method == "GET": 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": 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: if r.status_code == 200:
return r.json() return r.json()
elif r.status_code == 401 and 'error' in r.json() and r.json()['error'] == "Invalid API Authentication": 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: def api_list_entries(team_id: Optional[str]=None) -> Any:
if team_id: 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: else:
return make_API_request("/entries".format(team_id)) return make_API_request("/entries")
def api_create_entry(body: str, team_id: str) -> Any: def api_create_entry(body: str, team_id: str) -> Any:
return make_API_request("/entries", "POST", {"body": body, "team_id": team_id}) return make_API_request("/entries", "POST", {"body": body, "team_id": team_id})