Skip to content

Instantly share code, notes, and snippets.

@ramazanpolat
Created September 7, 2018 22:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ramazanpolat/72ba5d00b5defbf73755db9e70d4c0a3 to your computer and use it in GitHub Desktop.
Save ramazanpolat/72ba5d00b5defbf73755db9e70d4c0a3 to your computer and use it in GitHub Desktop.
import os
from typing import Any
from apistar import App, Route, http
from apistar.codecs.multipart import MultiPartCodec
from werkzeug.datastructures import FileStorage
UPLOAD_FOLDER = os.path.join('.', 'uploads')
PORT = 8080
def build_response(success: bool = False, error: str = None, data: Any = None):
result = {
'success': success,
'error': error,
'data': data
}
return result
def upload(request: http.Request, body: http.Body) -> dict:
try:
md = MultiPartCodec().decode(body, dict(request.headers))
file_storage: FileStorage = md.get('file')
filename = os.path.join(UPLOAD_FOLDER, str(file_storage.filename))
file_storage.save(filename)
return build_response(success=True, data="Upload complete")
except Exception as all_exceptions:
return build_response(error=str(all_exceptions))
routes = [
Route('/upload', method='POST', handler=upload),
]
app = App(routes=routes)
if __name__ == '__main__':
app.serve('0.0.0.0', 5000, debug=False)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment