Toot-Mastodon-CLI-TUI-clien.../toot/api.py

157 lines
3.7 KiB
Python
Raw Normal View History

2017-04-16 14:14:33 +02:00
# -*- coding: utf-8 -*-
import logging
import requests
from requests import Request, Session
from toot import App, User, CLIENT_NAME, CLIENT_WEBSITE
SCOPES = 'read write follow'
logger = logging.getLogger('toot')
2017-04-16 17:15:05 +02:00
class ApiError(Exception):
pass
class NotFoundError(ApiError):
pass
2017-04-16 14:14:33 +02:00
def _log_request(request, prepared_request):
logger.debug(">>> \033[32m{} {}\033[0m".format(request.method, request.url))
logger.debug(">>> DATA: \033[33m{}\033[0m".format(request.data))
logger.debug(">>> FILES: \033[33m{}\033[0m".format(request.files))
logger.debug(">>> HEADERS: \033[33m{}\033[0m".format(request.headers))
def _log_response(response):
2017-04-16 17:15:05 +02:00
if response.ok:
logger.debug("<<< \033[32m{}\033[0m".format(response))
logger.debug("<<< \033[33m{}\033[0m".format(response.json()))
else:
logger.debug("<<< \033[31m{}\033[0m".format(response))
logger.debug("<<< \033[31m{}\033[0m".format(response.content))
2017-04-16 14:14:33 +02:00
def _get(app, user, url, params=None):
url = app.base_url + url
headers = {"Authorization": "Bearer " + user.access_token}
response = requests.get(url, params, headers=headers)
response.raise_for_status()
return response.json()
def _post(app, user, url, data=None, files=None):
url = app.base_url + url
headers = {"Authorization": "Bearer " + user.access_token}
session = Session()
request = Request('POST', url, headers, files, data)
prepared_request = request.prepare()
_log_request(request, prepared_request)
response = session.send(prepared_request)
_log_response(response)
2017-04-16 17:15:05 +02:00
if not response.ok:
try:
error = response.json()['error']
except:
error = "Unknown error"
if response.status_code == 404:
raise NotFoundError(error)
raise ApiError(error)
2017-04-16 14:14:33 +02:00
response.raise_for_status()
return response.json()
def create_app(base_url):
url = base_url + '/api/v1/apps'
response = requests.post(url, {
'client_name': CLIENT_NAME,
'redirect_uris': 'urn:ietf:wg:oauth:2.0:oob',
'scopes': SCOPES,
'website': CLIENT_WEBSITE,
})
response.raise_for_status()
data = response.json()
client_id = data.get('client_id')
client_secret = data.get('client_secret')
return App(base_url, client_id, client_secret)
def login(app, username, password):
url = app.base_url + '/oauth/token'
response = requests.post(url, {
'grant_type': 'password',
'client_id': app.client_id,
'client_secret': app.client_secret,
'username': username,
'password': password,
'scope': SCOPES,
})
response.raise_for_status()
data = response.json()
access_token = data.get('access_token')
return User(username, access_token)
def post_status(app, user, status, visibility='public', media_ids=None):
return _post(app, user, '/api/v1/statuses', {
'status': status,
'media_ids[]': media_ids,
'visibility': visibility,
})
def timeline_home(app, user):
return _get(app, user, '/api/v1/timelines/home')
def upload_media(app, user, file):
return _post(app, user, '/api/v1/media', files={
'file': file
})
2017-04-16 15:07:27 +02:00
def search(app, user, query, resolve):
return _get(app, user, '/api/v1/search', {
'q': query,
'resolve': resolve,
})
2017-04-16 17:15:05 +02:00
def follow(app, user, account):
url = '/api/v1/accounts/%d/follow' % account
return _post(app, user, url)
def unfollow(app, user, account):
url = '/api/v1/accounts/%d/unfollow' % account
return _post(app, user, url)
2017-04-16 17:52:54 +02:00
def verify_credentials(app, user):
return _get(app, user, '/api/v1/accounts/verify_credentials')