Skip to content

Commit

Permalink
version bump and release notes
Browse files Browse the repository at this point in the history
  • Loading branch information
berislavlopac committed Mar 4, 2023
1 parent 80a4a2d commit 794116a
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 17 deletions.
17 changes: 4 additions & 13 deletions pyapi/server/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,7 @@
from starlette.exceptions import HTTPException
from stringcase import snakecase

from pyapi.server.openapi import (
JSONResponse,
OpenAPIRequestWrapper,
OpenAPIResponseWrapper,
Request,
Response,
)

from .openapi import JSONResponse, OpenAPIRequest, OpenAPIResponse, Request, Response
from .utils import get_spec_from_file, OperationSpec

log = getLogger(__name__)
Expand Down Expand Up @@ -136,11 +129,9 @@ def set_endpoint(

@wraps(endpoint_fn)
async def wrapper(request: Request, **kwargs) -> Response:
openapi_request = OpenAPIRequestWrapper(request)
openapi_request = OpenAPIRequest(request)
validated_request = self.request_validator.validate(
spec=self.spec,
request=openapi_request,
base_url=self.spec_base_uri
spec=self.spec, request=openapi_request, base_url=self.spec_base_uri
)
try:
validated_request.raise_for_errors()
Expand Down Expand Up @@ -169,7 +160,7 @@ async def wrapper(request: Request, **kwargs) -> Response:
self.response_validator.validate(
spec=self.spec,
request=openapi_request,
response=OpenAPIResponseWrapper(response),
response=OpenAPIResponse(response),
base_url=self.spec_base_uri,
).raise_for_errors()
return response
Expand Down
18 changes: 16 additions & 2 deletions pyapi/server/openapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
from starlette.responses import JSONResponse, Response # noqa: F401


class OpenAPIRequestWrapper:
class OpenAPIRequest:
"""Wrapper for PyAPI Server requests."""

def __init__(self, request: Request):
self.request = request
self._body: Optional[dict] = None
Expand All @@ -27,23 +29,28 @@ def __init__(self, request: Request):
else:
task = ensure_future(body_coroutine, loop=loop)
task.add_done_callback(lambda fut: self._set_body(fut.result()))

def _set_body(self, value):
self._body = value

@property
def host_url(self) -> str:
"""Return the request host url."""
return self.request.base_url._url

@property
def path(self) -> str:
"""Return the request path."""
return self.request.url.path

@property
def method(self) -> str:
"""Return the request HTTP method."""
return self.request.method.lower()

@property
def body(self) -> Optional[str]:
"""Return the request body as string, if present."""
body = self._body
if isinstance(body, bytes):
return body.decode("utf-8")
Expand All @@ -52,32 +59,39 @@ def body(self) -> Optional[str]:

@property
def mimetype(self) -> str:
"""Return the request content type."""
content_type = self.request.headers["Content-Type"]
if content_type:
return content_type.partition(";")[0]

return ""


class OpenAPIResponseWrapper:
class OpenAPIResponse:
"""Wrapper for PyAPI Server responses."""

def __init__(self, response: Response):
self.response = response

@property
def data(self) -> str:
"""Return the response content as string."""
if isinstance(self.response.body, bytes):
return self.response.body.decode("utf-8")
assert isinstance(self.response.body, str)
return self.response.body

@property
def status_code(self) -> int:
"""Return the response HTTP status code."""
return self.response.status_code

@property
def mimetype(self) -> str:
"""Return the response content type."""
return self.response.media_type or ""

@property
def headers(self) -> Headers:
"""Return the response headers."""
return self.response.headers
2 changes: 1 addition & 1 deletion pyapi/server/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from enum import Enum
from itertools import chain
from pathlib import Path
from typing import Callable, Dict, TYPE_CHECKING, Union
from typing import Callable, Dict, TYPE_CHECKING

import yaml
from stringcase import camelcase
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "pyapi-server"
version = "0.2.7"
version = "0.2.8"
description = "Lightweight API framework using an OpenAPI spec for routing and validation."
readme = "README.md"
license = {text = "MIT"}
Expand Down
2 changes: 2 additions & 0 deletions release-notes/0.2.8.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
* add base uri when creating from file
* add custom OpenAPI request and response classes

0 comments on commit 794116a

Please sign in to comment.