-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Tuerke Erik
committed
Mar 10, 2020
1 parent
374cf99
commit 70222f1
Showing
18 changed files
with
300 additions
and
228 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
marshmallow | ||
marshmallow | ||
werkzeug |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
from werkzeug.datastructures import MIMEAccept | ||
from werkzeug.exceptions import HTTPException | ||
|
||
from restit.response import Response | ||
|
||
|
||
class ExceptionResponseMaker: | ||
HTML_TEMPLATE = """<title>{code} {name}</title> | ||
<h1>{name}</h1> | ||
<p>{description}</p> | ||
""" | ||
|
||
def __init__(self, http_exception: HTTPException): | ||
self.http_exception = http_exception | ||
|
||
def create_response(self, accept: MIMEAccept) -> Response: | ||
if accept.accept_html: | ||
return self.create_html_response() | ||
elif accept.accept_json: | ||
return self.create_json_response() | ||
else: | ||
return self.create_plain_text_response() | ||
|
||
def create_html_response(self) -> Response: | ||
html_text = ExceptionResponseMaker.HTML_TEMPLATE.format( | ||
code=self.http_exception.code, | ||
name=self.http_exception.name, | ||
description=self.http_exception.description | ||
) | ||
response = Response(html_text, status_code=self.http_exception.code) | ||
return response | ||
|
||
def create_json_response(self) -> Response: | ||
response_dict = { | ||
"code": self.http_exception.code, | ||
"name": self.http_exception.name, | ||
"description": self.http_exception.description | ||
} | ||
response = Response(response_dict, self.http_exception.code) | ||
return response | ||
|
||
def create_plain_text_response(self) -> Response: | ||
return Response( | ||
f"{self.http_exception.name}: {self.http_exception.description} ({self.http_exception.code})", | ||
self.http_exception.code | ||
) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,43 @@ | ||
import json | ||
from json import JSONDecodeError | ||
|
||
from restit import DEFAULT_ENCODING | ||
from restit._internal.common import create_dict_from_query_parameter_syntax | ||
|
||
|
||
class Request: | ||
def __init__(self, query_parameters: dict, wsgi_environment: dict = None, body: bytes = None): | ||
self.query_parameters = query_parameters | ||
self.wsgi_environment = wsgi_environment | ||
self.body: bytes = body or b"" | ||
self.body_as_json: dict = {} | ||
self.__set_body_as_json() | ||
|
||
def __set_body_as_json(self): | ||
body_as_string = self.body.decode(encoding=DEFAULT_ENCODING) | ||
try: | ||
self.body_as_json = json.loads(body_as_string) | ||
except JSONDecodeError: | ||
self.body_as_json = create_dict_from_query_parameter_syntax(body_as_string) | ||
from functools import lru_cache | ||
|
||
import werkzeug.wrappers | ||
from werkzeug.utils import escape | ||
|
||
from restit import _DEFAULT_ENCODING | ||
|
||
|
||
class Request(werkzeug.wrappers.Request): | ||
|
||
def __init__(self, *args, **kwargs): | ||
super().__init__(*args, **kwargs) | ||
|
||
self.query_parameters = {} | ||
self.body_as_dict = {} | ||
self._init() | ||
|
||
def _init(self): | ||
self.query_parameters = \ | ||
self._create_dict_from_assignment_syntax(self.query_string.decode(encoding=_DEFAULT_ENCODING)) | ||
if self.is_json(): | ||
self.body_as_dict.update(json.loads(self.data.decode(encoding=_DEFAULT_ENCODING))) | ||
else: | ||
self.body_as_dict.update(dict(self.form)) | ||
|
||
def is_json(self) -> bool: | ||
return self.content_type.lower() == "application/json" | ||
|
||
@staticmethod | ||
@lru_cache() | ||
def _create_dict_from_assignment_syntax(assignment_syntax_data: str) -> dict: | ||
return_dict = {} | ||
if assignment_syntax_data is None or "=" not in assignment_syntax_data: | ||
return return_dict | ||
for query_string_pair in assignment_syntax_data.split("&"): | ||
try: | ||
key, value = query_string_pair.split("=") | ||
return_dict[key] = escape(value) | ||
except ValueError: | ||
pass | ||
|
||
return return_dict |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.