Skip to content

Commit

Permalink
Merge pull request #108 from dapper91/dev
Browse files Browse the repository at this point in the history
- openapi 3.x support added.
- batch size validation support added.
- custom http server response status added.
- raise_for_status flag added for http clients.
- python 3.12, 3.13 support added.
  • Loading branch information
dapper91 authored Nov 11, 2024
2 parents d4ad929 + 5f7adf4 commit ffcebba
Show file tree
Hide file tree
Showing 55 changed files with 8,534 additions and 3,129 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ['3.8', '3.9', '3.10', '3.11']
python-version: ['3.9', '3.10', '3.11', '3.12', '3.13']
steps:
- uses: actions/checkout@v2
- name: Set up Python ${{ matrix.python-version }}
Expand Down
16 changes: 12 additions & 4 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ default_stages:

repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
rev: v5.0.0
hooks:
- id: check-yaml
- id: check-toml
Expand Down Expand Up @@ -39,11 +39,11 @@ repos:
args:
- --diff
- repo: https://github.com/pycqa/flake8
rev: 6.1.0
rev: 7.1.1
hooks:
- id: flake8
- repo: https://github.com/pycqa/isort
rev: 5.12.0
rev: 5.13.2
hooks:
- id: isort
name: fix import order
Expand All @@ -63,7 +63,7 @@ repos:
- --multi-line=9
- --project=pjrpc
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.7.1
rev: v1.13.0
hooks:
- id: mypy
stages:
Expand All @@ -75,4 +75,12 @@ repos:
- aiohttp>=3.7
- httpx>=0.23.0
- pydantic>=2.0
- types-jsonschema>=3.0,<4.0
- types-requests>=2.0
- aio-pika>=8.0
- docstring_parser>=0.8
- werkzeug>=2.0
- pytest>=7.4.0
- starlette>=0.25.0
- flask>=2.0.0
- openapi-ui-bundles>=0.3.0
10 changes: 10 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
Changelog
=========

1.10.0 (2024-11-12)
------------------

- openapi 3.x support added.
- batch size validation support added.
- custom http server response status added.
- raise_for_status flag added for http clients.
- python 3.12, 3.13 support added.


1.9.0 (2024-04-22)
------------------

Expand Down
99 changes: 40 additions & 59 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -454,19 +454,19 @@ and Swagger UI web tool with basic auth:
.. code-block:: python
import uuid
from typing import Any, Optional
from typing import Annotated, Any, Optional
import flask
import flask_httpauth
import pydantic
import flask_cors
import flask_httpauth
import pydantic as pd
from werkzeug import security
import pjrpc.server.specs.extractors.pydantic
from pjrpc.server.integration import flask as integration
from pjrpc.server.specs import extractors
from pjrpc.server.specs import openapi as specs
from pjrpc.server.validators import pydantic as validators
from pjrpc.server.specs import extractors, openapi as specs
app = flask.Flask('myapp')
flask_cors.CORS(app, resources={"/myapp/api/v1/*": {"origins": "*"}})
Expand All @@ -492,30 +492,51 @@ and Swagger UI web tool with basic auth:
class JSONEncoder(pjrpc.JSONEncoder):
def default(self, o: Any) -> Any:
if isinstance(o, pydantic.BaseModel):
return o.dict()
if isinstance(o, pd.BaseModel):
return o.model_dump()
if isinstance(o, uuid.UUID):
return str(o)
return super().default(o)
class UserIn(pydantic.BaseModel):
UserName = Annotated[
str,
pd.Field(description="User name", examples=["John"]),
]
UserSurname = Annotated[
str,
pd.Field(description="User surname", examples=['Doe']),
]
UserAge = Annotated[
int,
pd.Field(description="User age", examples=[25]),
]
UserId = Annotated[
uuid.UUID,
pd.Field(description="User identifier", examples=["c47726c6-a232-45f1-944f-60b98966ff1b"]),
]
class UserIn(pd.BaseModel):
"""
User registration data.
"""
name: str
surname: str
age: int
name: UserName
surname: UserSurname
age: UserAge
class UserOut(UserIn):
"""
Registered user data.
"""
id: uuid.UUID
id: UserId
class AlreadyExistsError(pjrpc.exc.JsonRpcError):
Expand All @@ -537,26 +558,9 @@ and Swagger UI web tool with basic auth:
@specs.annotate(
summary='Creates a user',
tags=['users'],
errors=[AlreadyExistsError],
examples=[
specs.MethodExample(
summary="Simple example",
params=dict(
user={
'name': 'John',
'surname': 'Doe',
'age': 25,
},
),
result={
'id': 'c47726c6-a232-45f1-944f-60b98966ff1b',
'name': 'John',
'surname': 'Doe',
'age': 25,
},
),
],
)
@methods.add
@validator.validate
Expand All @@ -576,30 +580,17 @@ and Swagger UI web tool with basic auth:
user_id = uuid.uuid4().hex
flask.current_app.users_db[user_id] = user
return UserOut(id=user_id, **user.dict())
return UserOut(id=user_id, **user.model_dump())
@specs.annotate(
summary='Returns a user',
tags=['users'],
errors=[NotFoundError],
examples=[
specs.MethodExample(
summary='Simple example',
params=dict(
user_id='c47726c6-a232-45f1-944f-60b98966ff1b',
),
result={
'id': 'c47726c6-a232-45f1-944f-60b98966ff1b',
'name': 'John',
'surname': 'Doe',
'age': 25,
},
),
],
)
@methods.add
@validator.validate
def get_user(user_id: uuid.UUID) -> UserOut:
def get_user(user_id: UserId) -> UserOut:
"""
Returns a user.
Expand All @@ -612,25 +603,17 @@ and Swagger UI web tool with basic auth:
if not user:
raise NotFoundError()
return UserOut(id=user_id, **user.dict())
return UserOut(id=user_id, **user.model_dump())
@specs.annotate(
summary='Deletes a user',
tags=['users'],
errors=[NotFoundError],
examples=[
specs.MethodExample(
summary='Simple example',
params=dict(
user_id='c47726c6-a232-45f1-944f-60b98966ff1b',
),
result=None,
),
],
)
@methods.add
@validator.validate
def delete_user(user_id: uuid.UUID) -> None:
def delete_user(user_id: UserId) -> None:
"""
Deletes a user.
Expand Down Expand Up @@ -664,8 +647,6 @@ and Swagger UI web tool with basic auth:
],
schema_extractor=extractors.pydantic.PydanticSchemaExtractor(),
ui=specs.SwaggerUI(),
# ui=specs.RapiDoc(),
# ui=specs.ReDoc(),
),
)
json_rpc.dispatcher.add_methods(methods)
Expand Down
5 changes: 3 additions & 2 deletions docs/source/pjrpc/extending.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,12 @@ an JSON-RPC server implementation based on :py:mod:`http.server` standard python
self.send_error(http.HTTPStatus.BAD_REQUEST)
return
response_text = self.server.dispatcher.dispatch(request_text, context=self)
if response_text is None:
response = self.server.dispatcher.dispatch(request_text, context=self)
if response is None:
self.send_response_only(http.HTTPStatus.OK)
self.end_headers()
else:
response_text, error_codes = response
self.send_response(http.HTTPStatus.OK)
self.send_header("Content-type", pjrpc.common.DEFAULT_CONTENT_TYPE)
self.end_headers()
Expand Down
Loading

0 comments on commit ffcebba

Please sign in to comment.