This commit is contained in:
2018-10-18 04:07:13 -07:00
parent 5ffa18221f
commit cd799ddfcd
2 changed files with 30 additions and 8 deletions
+12 -1
View File
@@ -162,6 +162,13 @@ class API:
if route_object.does_match(path):
return route
def _prepare_cookies(self, resp):
if resp.cookies:
header = " ".join([f"{k}={v}" for k, v in resp.cookies.items()])
header = f"Set-Cookie: {header}"
resp.headers["Set-Cookie"] = header
async def _dispatch_request(self, req):
# Set formats on Request object.
req.formats = self.formats
@@ -219,9 +226,13 @@ class API:
else:
self.default_response(req, resp)
self._prepare_cookies(resp)
return resp
def add_route(self, route, endpoint=None, *, default=False, static=False, check_existing=True):
def add_route(
self, route, endpoint=None, *, default=False, static=False, check_existing=True
):
"""Add a route to the API.
:param route: A string representation of the route.
+18 -7
View File
@@ -1,17 +1,19 @@
import io
import json
import gzip
from http.cookies import SimpleCookie
import chardet
import rfc3986
import graphene
import yaml
from requests.structures import CaseInsensitiveDict
from requests.cookies import RequestsCookieJar
from starlette.datastructures import MutableHeaders
from starlette.requests import Request as StarletteRequest
from starlette.responses import Response as StarletteResponse
from urllib.parse import parse_qs
from .status_codes import HTTP_200
@@ -88,12 +90,7 @@ class QueryDict(dict):
# TODO: add slots
class Request:
__slots__ = [
"_starlette",
"formats",
"_headers",
"_encoding",
]
__slots__ = ["_starlette", "formats", "_headers", "_encoding"]
def __init__(self, scope, receive):
self._starlette = StarletteRequest(scope, receive)
@@ -130,6 +127,18 @@ class Request:
"""The parsed URL of the Request."""
return rfc3986.urlparse(self.full_url)
@property
def cookies(self):
cookies = RequestsCookieJar()
cookie_header = self.headers.get("cookie", "")
# if cookie_header:
bc = SimpleCookie(cookie_header)
for k, v in bc.items():
cookies[k] = v
return cookies
@property
def params(self):
"""A dictionary of the parsed query parameters used for the Request."""
@@ -216,6 +225,7 @@ class Response:
"media",
"headers",
"formats",
"cookies",
]
def __init__(self, req, *, formats):
@@ -231,6 +241,7 @@ class Response:
{}
) #: A Python dictionary of {Key: value}, representing the headers of the response.
self.formats = formats
self.cookies = {} # req.cookies
@property
async def body(self):