mirror of
https://github.com/ihabunek/toot.git
synced 2024-11-03 04:17:21 -05:00
Standardize http request method parameters
This allows setting either json or data. Until now we were always using data and this is not enough for some endpoints.
This commit is contained in:
parent
8c3baa704e
commit
b15cb85a23
@ -17,7 +17,7 @@ def test_create_app(mock_post):
|
|||||||
|
|
||||||
create_app('bigfish.software')
|
create_app('bigfish.software')
|
||||||
|
|
||||||
mock_post.assert_called_once_with('https://bigfish.software/api/v1/apps', {
|
mock_post.assert_called_once_with('https://bigfish.software/api/v1/apps', json={
|
||||||
'website': CLIENT_WEBSITE,
|
'website': CLIENT_WEBSITE,
|
||||||
'client_name': CLIENT_NAME,
|
'client_name': CLIENT_NAME,
|
||||||
'scopes': SCOPES,
|
'scopes': SCOPES,
|
||||||
@ -48,7 +48,7 @@ def test_login(mock_post):
|
|||||||
login(app, 'user', 'pass')
|
login(app, 'user', 'pass')
|
||||||
|
|
||||||
mock_post.assert_called_once_with(
|
mock_post.assert_called_once_with(
|
||||||
'https://bigfish.software/oauth/token', data, allow_redirects=False)
|
'https://bigfish.software/oauth/token', data=data, allow_redirects=False)
|
||||||
|
|
||||||
|
|
||||||
@mock.patch('toot.http.anon_post')
|
@mock.patch('toot.http.anon_post')
|
||||||
@ -70,4 +70,4 @@ def test_login_failed(mock_post):
|
|||||||
login(app, 'user', 'pass')
|
login(app, 'user', 'pass')
|
||||||
|
|
||||||
mock_post.assert_called_once_with(
|
mock_post.assert_called_once_with(
|
||||||
'https://bigfish.software/oauth/token', data, allow_redirects=False)
|
'https://bigfish.software/oauth/token', data=data, allow_redirects=False)
|
||||||
|
@ -38,10 +38,10 @@ def test_post_defaults(mock_post, mock_uuid, capsys):
|
|||||||
|
|
||||||
console.run_command(app, user, 'post', ['Hello world'])
|
console.run_command(app, user, 'post', ['Hello world'])
|
||||||
|
|
||||||
mock_post.assert_called_once_with(app, user, '/api/v1/statuses', {
|
mock_post.assert_called_once_with(app, user, '/api/v1/statuses', json={
|
||||||
'status': 'Hello world',
|
'status': 'Hello world',
|
||||||
'visibility': 'public',
|
'visibility': 'public',
|
||||||
'media_ids[]': [],
|
'media_ids': [],
|
||||||
'sensitive': "false",
|
'sensitive': "false",
|
||||||
'spoiler_text': None,
|
'spoiler_text': None,
|
||||||
'in_reply_to_id': None,
|
'in_reply_to_id': None,
|
||||||
@ -74,9 +74,9 @@ def test_post_with_options(mock_post, mock_uuid, capsys):
|
|||||||
|
|
||||||
console.run_command(app, user, 'post', args)
|
console.run_command(app, user, 'post', args)
|
||||||
|
|
||||||
mock_post.assert_called_once_with(app, user, '/api/v1/statuses', {
|
mock_post.assert_called_once_with(app, user, '/api/v1/statuses', json={
|
||||||
'status': 'Hello world',
|
'status': 'Hello world',
|
||||||
'media_ids[]': [],
|
'media_ids': [],
|
||||||
'visibility': 'unlisted',
|
'visibility': 'unlisted',
|
||||||
'sensitive': "true",
|
'sensitive': "true",
|
||||||
'spoiler_text': "Spoiler!",
|
'spoiler_text': "Spoiler!",
|
||||||
|
16
toot/api.py
16
toot/api.py
@ -27,14 +27,14 @@ def _status_action(app, user, status_id, action):
|
|||||||
def create_app(domain, scheme='https'):
|
def create_app(domain, scheme='https'):
|
||||||
url = '{}://{}/api/v1/apps'.format(scheme, domain)
|
url = '{}://{}/api/v1/apps'.format(scheme, domain)
|
||||||
|
|
||||||
data = {
|
json = {
|
||||||
'client_name': CLIENT_NAME,
|
'client_name': CLIENT_NAME,
|
||||||
'redirect_uris': 'urn:ietf:wg:oauth:2.0:oob',
|
'redirect_uris': 'urn:ietf:wg:oauth:2.0:oob',
|
||||||
'scopes': SCOPES,
|
'scopes': SCOPES,
|
||||||
'website': CLIENT_WEBSITE,
|
'website': CLIENT_WEBSITE,
|
||||||
}
|
}
|
||||||
|
|
||||||
return http.anon_post(url, data).json()
|
return http.anon_post(url, json=json).json()
|
||||||
|
|
||||||
|
|
||||||
def login(app, username, password):
|
def login(app, username, password):
|
||||||
@ -49,7 +49,7 @@ def login(app, username, password):
|
|||||||
'scope': SCOPES,
|
'scope': SCOPES,
|
||||||
}
|
}
|
||||||
|
|
||||||
response = http.anon_post(url, data, allow_redirects=False)
|
response = http.anon_post(url, data=data, allow_redirects=False)
|
||||||
|
|
||||||
# If auth fails, it redirects to the login page
|
# If auth fails, it redirects to the login page
|
||||||
if response.is_redirect:
|
if response.is_redirect:
|
||||||
@ -79,7 +79,7 @@ def request_access_token(app, authorization_code):
|
|||||||
'redirect_uri': 'urn:ietf:wg:oauth:2.0:oob',
|
'redirect_uri': 'urn:ietf:wg:oauth:2.0:oob',
|
||||||
}
|
}
|
||||||
|
|
||||||
return http.anon_post(url, data, allow_redirects=False).json()
|
return http.anon_post(url, data=data, allow_redirects=False).json()
|
||||||
|
|
||||||
|
|
||||||
def post_status(
|
def post_status(
|
||||||
@ -104,9 +104,9 @@ def post_status(
|
|||||||
# if the request is retried.
|
# if the request is retried.
|
||||||
headers = {"Idempotency-Key": uuid.uuid4().hex}
|
headers = {"Idempotency-Key": uuid.uuid4().hex}
|
||||||
|
|
||||||
params = {
|
json = {
|
||||||
'status': status,
|
'status': status,
|
||||||
'media_ids[]': media_ids,
|
'media_ids': media_ids,
|
||||||
'visibility': visibility,
|
'visibility': visibility,
|
||||||
'sensitive': str_bool(sensitive),
|
'sensitive': str_bool(sensitive),
|
||||||
'spoiler_text': spoiler_text,
|
'spoiler_text': spoiler_text,
|
||||||
@ -116,9 +116,9 @@ def post_status(
|
|||||||
}
|
}
|
||||||
|
|
||||||
if content_type:
|
if content_type:
|
||||||
params['content_type'] = content_type
|
json['content_type'] = content_type
|
||||||
|
|
||||||
return http.post(app, user, '/api/v1/statuses', params, headers=headers).json()
|
return http.post(app, user, '/api/v1/statuses', json=json, headers=headers).json()
|
||||||
|
|
||||||
|
|
||||||
def delete_status(app, user, status_id):
|
def delete_status(app, user, status_id):
|
||||||
|
34
toot/http.py
34
toot/http.py
@ -47,9 +47,11 @@ def process_response(response):
|
|||||||
return response
|
return response
|
||||||
|
|
||||||
|
|
||||||
def get(app, user, url, params=None):
|
def get(app, user, path, params=None, headers=None):
|
||||||
url = app.base_url + url
|
url = app.base_url + path
|
||||||
headers = {"Authorization": "Bearer " + user.access_token}
|
|
||||||
|
headers = headers or {}
|
||||||
|
headers["Authorization"] = f"Bearer {user.access_token}"
|
||||||
|
|
||||||
request = Request('GET', url, headers, params=params)
|
request = Request('GET', url, headers, params=params)
|
||||||
response = send_request(request)
|
response = send_request(request)
|
||||||
@ -64,29 +66,29 @@ def anon_get(url, params=None):
|
|||||||
return process_response(response)
|
return process_response(response)
|
||||||
|
|
||||||
|
|
||||||
def post(app, user, url, data=None, files=None, allow_redirects=True, headers={}):
|
def post(app, user, path, headers=None, files=None, data=None, json=None, allow_redirects=True):
|
||||||
url = app.base_url + url
|
url = app.base_url + path
|
||||||
|
|
||||||
headers["Authorization"] = "Bearer " + user.access_token
|
headers = headers or {}
|
||||||
|
headers["Authorization"] = f"Bearer {user.access_token}"
|
||||||
|
|
||||||
request = Request('POST', url, headers, files, data)
|
return anon_post(url, headers=headers, files=files, data=data, json=json, allow_redirects=allow_redirects)
|
||||||
response = send_request(request, allow_redirects)
|
|
||||||
|
|
||||||
return process_response(response)
|
|
||||||
|
|
||||||
|
|
||||||
def delete(app, user, url, data=None):
|
def delete(app, user, path, data=None, headers=None):
|
||||||
url = app.base_url + url
|
url = app.base_url + path
|
||||||
headers = {"Authorization": "Bearer " + user.access_token}
|
|
||||||
|
|
||||||
request = Request('DELETE', url, headers=headers, data=data)
|
headers = headers or {}
|
||||||
|
headers["Authorization"] = f"Bearer {user.access_token}"
|
||||||
|
|
||||||
|
request = Request('DELETE', url, headers=headers, json=data)
|
||||||
response = send_request(request)
|
response = send_request(request)
|
||||||
|
|
||||||
return process_response(response)
|
return process_response(response)
|
||||||
|
|
||||||
|
|
||||||
def anon_post(url, data=None, files=None, allow_redirects=True):
|
def anon_post(url, headers=None, files=None, data=None, json=None, allow_redirects=True):
|
||||||
request = Request('POST', url, {}, files, data)
|
request = Request(method="POST", url=url, headers=headers, files=files, data=data, json=json)
|
||||||
response = send_request(request, allow_redirects)
|
response = send_request(request, allow_redirects)
|
||||||
|
|
||||||
return process_response(response)
|
return process_response(response)
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
|
import json
|
||||||
|
|
||||||
from logging import getLogger
|
from logging import getLogger
|
||||||
|
|
||||||
logger = getLogger('toot')
|
logger = getLogger('toot')
|
||||||
@ -22,6 +24,9 @@ def log_request(request):
|
|||||||
if request.data:
|
if request.data:
|
||||||
logger.debug(">>> DATA: \033[33m{}\033[0m".format(request.data))
|
logger.debug(">>> DATA: \033[33m{}\033[0m".format(request.data))
|
||||||
|
|
||||||
|
if request.json:
|
||||||
|
logger.debug(">>> JSON: \033[33m{}\033[0m".format(json.dumps(request.json)))
|
||||||
|
|
||||||
if request.files:
|
if request.files:
|
||||||
logger.debug(">>> FILES: \033[33m{}\033[0m".format(request.files))
|
logger.debug(">>> FILES: \033[33m{}\033[0m".format(request.files))
|
||||||
|
|
||||||
@ -32,10 +37,10 @@ def log_request(request):
|
|||||||
def log_response(response):
|
def log_response(response):
|
||||||
if response.ok:
|
if response.ok:
|
||||||
logger.debug("<<< \033[32m{}\033[0m".format(response))
|
logger.debug("<<< \033[32m{}\033[0m".format(response))
|
||||||
logger.debug("<<< \033[33m{}\033[0m".format(response.content))
|
logger.debug("<<< \033[33m{}\033[0m".format(response.content.decode()))
|
||||||
else:
|
else:
|
||||||
logger.debug("<<< \033[31m{}\033[0m".format(response))
|
logger.debug("<<< \033[31m{}\033[0m".format(response))
|
||||||
logger.debug("<<< \033[31m{}\033[0m".format(response.content))
|
logger.debug("<<< \033[31m{}\033[0m".format(response.content.decode()))
|
||||||
|
|
||||||
|
|
||||||
def log_debug(*msgs):
|
def log_debug(*msgs):
|
||||||
|
Loading…
Reference in New Issue
Block a user