wrolpi/wrolpi/schema.py
Roland 02f8584762
Squashed commit of the following:
commit 1663e1be9e
Author: Roland <roland@learningselfreliance.com>
Date:   Thu Dec 30 19:37:25 2021 -0700

    Setting downloader to video when a channel schedules it's videos for download.

commit a6811f9e87
Author: Roland <roland@learningselfreliance.com>
Date:   Thu Dec 30 19:33:27 2021 -0700

    Setting channel downloader when spreading channel downloads.

commit b63c8f132a
Author: Roland <roland@learningselfreliance.com>
Date:   Thu Dec 30 19:28:53 2021 -0700

    Fixing DownloadManager.kill

commit 120ce7cd2c
Author: Roland <roland@learningselfreliance.com>
Date:   Thu Dec 30 19:10:12 2021 -0700

    Fixing OTP api urls.

commit d3b985241b
Author: Roland <roland@learningselfreliance.com>
Date:   Thu Dec 30 19:07:48 2021 -0700

    Channel Cancel button does not submit form.

commit 9d1a0ecd55
Author: Roland <roland@learningselfreliance.com>
Date:   Thu Dec 30 19:00:53 2021 -0700

    A Channel without an existing Download cannot be downloaded.

commit e7c920f382
Author: Roland <roland@learningselfreliance.com>
Date:   Thu Dec 30 18:41:02 2021 -0700

    Fixing typing.

commit d56c8fde24
Author: Roland <roland@learningselfreliance.com>
Date:   Thu Dec 30 18:36:02 2021 -0700

    Testing bad downloader param.

commit e3c57ef674
Author: Roland <roland@learningselfreliance.com>
Date:   Thu Dec 30 18:31:35 2021 -0700

    Adding __json__ to Downloader for sending them to the FE.  Hiding ChannelDownloader because it will be handled specifically.

commit 047b507119
Author: Roland <roland@learningselfreliance.com>
Date:   Thu Dec 30 18:20:37 2021 -0700

    Downloader can be specified by the user.

commit 4a169ba43c
Author: Roland <roland@learningselfreliance.com>
Date:   Tue Dec 28 18:37:30 2021 -0700

    Download info a set in create_downloads.

commit 8778d53fa1
Author: Roland <roland@learningselfreliance.com>
Date:   Tue Dec 28 18:21:43 2021 -0700

    Fixing Download.info_json name.

commit b28bacfd5b
Author: Roland <roland@learningselfreliance.com>
Date:   Tue Dec 28 18:17:53 2021 -0700

    Downloader names are in the class, not in the init.

commit f5ce952da0
Author: Roland <roland@learningselfreliance.com>
Date:   Tue Dec 28 18:07:42 2021 -0700

    Download info can be saved during the download process and can be used by the Downloader.
2021-12-30 20:45:11 -07:00

200 lines
6.3 KiB
Python

import inspect
from datetime import datetime
from functools import wraps
from uuid import UUID
import sanic
from sanic import response
from sanic_openapi import doc
from sanic_openapi.doc import Field
from wrolpi.common import string_to_boolean, logger
from wrolpi.errors import NoBodyContents, ValidationError, MissingRequiredField, ExcessJSONFields, API_ERRORS, APIError
from wrolpi.vars import DATE_FORMAT
def validate_data(model: type, data: dict):
"""
Convert a JSON object to the model's specification. If the JSON object matches the model's specification, this
function will return a python dict of that data. If it doesn't match, this will return a Sanic response object
containing an error.
"""
if not data:
raise NoBodyContents()
new_data = {}
# Get the public attributes of the model
attrs = [i for i in dir(model) if not str(i).startswith('__')]
# Convert each json value to it's respective doc field's python type
# i.e. "json value" -> doc.String -> str
missing_fields = []
for attr in attrs:
field = getattr(model, attr)
try:
if isinstance(field, doc.String):
new_data[attr] = str(data.pop(attr))
elif isinstance(field, doc.Integer):
new_data[attr] = int(data.pop(attr))
elif isinstance(field, doc.Tuple):
new_data[attr] = tuple(data.pop(attr))
elif isinstance(field, doc.UUID):
new_data[attr] = UUID(data.pop(attr))
elif isinstance(field, doc.Boolean):
new_data[attr] = string_to_boolean(data.pop(attr))
elif isinstance(field, doc.Float):
val = data.pop(attr)
new_data[attr] = float(val) if val else None
elif isinstance(field, doc.Dictionary):
new_data[attr] = dict(data.pop(attr))
elif isinstance(field, doc.List):
new_data[attr] = list(data.pop(attr))
elif isinstance(field, Trinary):
val = data.pop(attr)
if val is not None:
val = string_to_boolean(val)
new_data[attr] = val
elif isinstance(field, doc.Date):
val = data.pop(attr)
if val:
new_data[attr] = datetime.strptime(val, DATE_FORMAT)
elif isinstance(field, doc.DateTime):
val = data.pop(attr)
if val:
new_data[attr] = datetime.utcfromtimestamp(val)
else:
raise ValidationError(f'Bad field type {field} specified in the API model!')
except KeyError:
if field.required:
missing_fields.append(attr)
if missing_fields:
raise MissingRequiredField(f'Missing fields: {missing_fields}')
if data:
raise ExcessJSONFields(f'Extra fields: {data.keys()}')
return new_data
def validate_doc(summary: str = None, consumes=None, produces=None, responses=(), tag: str = None):
"""
Apply Sanic OpenAPI docs to the wrapped route. Perform simple validation on requests.
"""
def wrapper(func):
@wraps(func)
async def wrapped(request, *a, **kw):
try:
if consumes:
if 'data' in kw:
raise OverflowError(f'data kwarg already being passed to {func}')
data = validate_data(consumes, request.json)
if isinstance(data, sanic.response.HTTPResponse):
# Error in validation
return data
kw['data'] = data
result = func(request, *a, **kw)
if inspect.iscoroutine(result):
result = await result
return result
except ValidationError as e:
error = API_ERRORS[type(e)]
body = {
'error': error['message'],
'code': error['code'],
}
logger.error(e, exc_info=True)
if e.__cause__:
cause = e.__cause__
cause = API_ERRORS[type(cause)] if cause else None
body['cause'] = {'error': cause['message'], 'code': cause['code']}
r = response.json(body, error['status'])
return r
except APIError as e:
# The endpoint returned a standardized APIError, convert it to a json response
error = API_ERRORS[type(e)]
r = response.json({'message': str(e), 'api_error': error['message'], 'code': error['code']},
error['status'])
return r
# Apply the docs to the wrapped function so sanic-openapi can find the wrapped function when
# building the schema. If these docs are applied to `func`, sanic-openapi won't be able to lookup `wrapped`
if summary:
wrapped = doc.summary(summary)(wrapped)
if consumes:
wrapped = doc.consumes(consumes, location='body')(wrapped)
if produces:
wrapped = doc.produces(produces)(wrapped)
for resp in responses:
wrapped = doc.response(*resp)(wrapped)
if tag:
wrapped = doc.tag(tag)(wrapped)
return wrapped
return wrapper
class SettingsObject:
media_directory = doc.String()
class SettingsResponse:
config = doc.Object(SettingsObject)
class SettingsRequest:
media_directory = doc.String()
wrol_mode = doc.Boolean()
timezone = doc.String()
class RegexRequest:
regex = doc.String()
class RegexResponse:
regex = doc.String()
valid = doc.Boolean()
class EchoResponse:
form = doc.Dictionary()
headers = doc.Dictionary()
json = doc.String()
method = doc.String()
class EventObject:
name = doc.String()
is_set = doc.Boolean()
class EventsResponse:
events = doc.List(EventObject)
class DownloadRequest:
urls = doc.String(required=True)
downloader = doc.String()
class Trinary(Field):
"""
A field for API docs. Can be True/False/None.
"""
def __init__(self, *a, **kw):
kw['choices'] = (True, False, None)
super().__init__(*a, **kw)
def serialize(self):
return {"type": "trinary", **super().serialize()}
class JSONErrorResponse:
error = doc.String()