server/middleware: download files
This commit is contained in:
parent
ab493a01b4
commit
148858fde9
7
API.md
7
API.md
|
@ -106,6 +106,13 @@ Requests that upload files must use `multipart/form-data` encoding. JSON
|
||||||
metadata must then be included as field of name `metadata`, whereas files must
|
metadata must then be included as field of name `metadata`, whereas files must
|
||||||
be included as separate fields with names specific to each request type.
|
be included as separate fields with names specific to each request type.
|
||||||
|
|
||||||
|
Alternatively, the server can download the files from the Internet on client's
|
||||||
|
behalf. In that case, the request doesn't need to be specially encoded in any
|
||||||
|
way. The files, however, should be passed as regular fields appended with `Url`
|
||||||
|
suffix. For example, to download a file named `content` from
|
||||||
|
`http://example.com/file.jpg`, the client should pass
|
||||||
|
`{"contentUrl":"http://example.com/file.jpg"}` as part of the message body.
|
||||||
|
|
||||||
## Error handling
|
## Error handling
|
||||||
|
|
||||||
All errors (except for unhandled fatal server errors) send relevant HTTP status
|
All errors (except for unhandled fatal server errors) send relevant HTTP status
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
import falcon
|
import falcon
|
||||||
from szurubooru import errors
|
from szurubooru import errors
|
||||||
|
from szurubooru.func import net
|
||||||
|
|
||||||
class Context(object):
|
class Context(object):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
|
@ -13,11 +14,13 @@ class Context(object):
|
||||||
return name in self.input
|
return name in self.input
|
||||||
|
|
||||||
def has_file(self, name):
|
def has_file(self, name):
|
||||||
return name in self.files
|
return name in self.files or name + 'Url' in self.input
|
||||||
|
|
||||||
def get_file(self, name, required=False):
|
def get_file(self, name, required=False):
|
||||||
if name in self.files:
|
if name in self.files:
|
||||||
return self.files[name]
|
return self.files[name]
|
||||||
|
if name + 'Url' in self.input:
|
||||||
|
return net.download(self.input[name + 'Url'])
|
||||||
if not required:
|
if not required:
|
||||||
return None
|
return None
|
||||||
raise errors.MissingRequiredFileError(
|
raise errors.MissingRequiredFileError(
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
|
import unittest.mock
|
||||||
import pytest
|
import pytest
|
||||||
from szurubooru import api, errors
|
from szurubooru import api, errors
|
||||||
|
from szurubooru.func import net
|
||||||
|
|
||||||
def test_has_param():
|
def test_has_param():
|
||||||
ctx = api.Context()
|
ctx = api.Context()
|
||||||
|
@ -13,6 +15,15 @@ def test_get_file():
|
||||||
assert ctx.get_file('key') == b'content'
|
assert ctx.get_file('key') == b'content'
|
||||||
assert ctx.get_file('key2') is None
|
assert ctx.get_file('key2') is None
|
||||||
|
|
||||||
|
def test_get_file_from_url():
|
||||||
|
with unittest.mock.patch('szurubooru.func.net.download'):
|
||||||
|
net.download.return_value = b'content'
|
||||||
|
ctx = api.Context()
|
||||||
|
ctx.input = {'keyUrl': 'example.com'}
|
||||||
|
assert ctx.get_file('key') == b'content'
|
||||||
|
assert ctx.get_file('key2') is None
|
||||||
|
net.download.assert_called_once_with('example.com')
|
||||||
|
|
||||||
def test_getting_list_parameter():
|
def test_getting_list_parameter():
|
||||||
ctx = api.Context()
|
ctx = api.Context()
|
||||||
ctx.input = {'key': 'value', 'list': ['1', '2', '3']}
|
ctx.input = {'key': 'value', 'list': ['1', '2', '3']}
|
||||||
|
|
Loading…
Reference in New Issue