-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #39 from dapper91/dev
- starlette integration added - django integration added - sub endpoints support implemented
- Loading branch information
Showing
32 changed files
with
1,365 additions
and
47 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
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
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,22 @@ | ||
#!/usr/bin/env python | ||
"""Django's command-line utility for administrative tasks.""" | ||
import os | ||
import sys | ||
|
||
|
||
def main(): | ||
"""Run administrative tasks.""" | ||
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings') | ||
try: | ||
from django.core.management import execute_from_command_line | ||
except ImportError as exc: | ||
raise ImportError( | ||
"Couldn't import Django. Are you sure it's installed and " | ||
"available on your PYTHONPATH environment variable? Did you " | ||
"forget to activate a virtual environment?", | ||
) from exc | ||
execute_from_command_line(sys.argv) | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |
Empty file.
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,29 @@ | ||
import uuid | ||
from typing import Any | ||
|
||
import pydantic | ||
import pjrpc.server | ||
import pjrpc.server.specs.extractors.pydantic | ||
from pjrpc.server.specs import extractors, openapi as specs | ||
|
||
|
||
class JSONEncoder(pjrpc.server.JSONEncoder): | ||
def default(self, o: Any) -> Any: | ||
if isinstance(o, pydantic.BaseModel): | ||
return o.dict() | ||
if isinstance(o, uuid.UUID): | ||
return str(o) | ||
|
||
return super().default(o) | ||
|
||
|
||
spec = specs.OpenAPI( | ||
info=specs.Info(version="1.0.0", title="User storage"), | ||
servers=[ | ||
specs.Server( | ||
url='http://127.0.0.1:8000', | ||
), | ||
], | ||
schema_extractor=extractors.pydantic.PydanticSchemaExtractor(), | ||
ui=specs.SwaggerUI(), | ||
) |
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,151 @@ | ||
import uuid | ||
from collections import defaultdict | ||
|
||
import pydantic | ||
from django.http.request import HttpRequest | ||
|
||
import pjrpc.server.specs.extractors.pydantic | ||
from pjrpc.server.validators import pydantic as validators | ||
from pjrpc.server.specs import openapi as specs | ||
|
||
methods = pjrpc.server.MethodRegistry() | ||
validator = validators.PydanticValidator() | ||
db = defaultdict(dict) | ||
|
||
|
||
class PostIn(pydantic.BaseModel): | ||
""" | ||
Post data. | ||
""" | ||
|
||
title: str | ||
content: str | ||
|
||
|
||
class PostOut(PostIn): | ||
""" | ||
Created post data. | ||
""" | ||
|
||
id: uuid.UUID | ||
|
||
|
||
class AlreadyExistsError(pjrpc.exc.JsonRpcError): | ||
""" | ||
Post already registered error. | ||
""" | ||
|
||
code = 2003 | ||
message = "post already exists" | ||
|
||
|
||
class NotFoundError(pjrpc.exc.JsonRpcError): | ||
""" | ||
Post not found error. | ||
""" | ||
|
||
code = 2004 | ||
message = "post not found" | ||
|
||
|
||
@specs.annotate( | ||
tags=['posts'], | ||
errors=[AlreadyExistsError], | ||
examples=[ | ||
specs.MethodExample( | ||
summary="Simple example", | ||
params=dict( | ||
post={ | ||
'title': 'Super post', | ||
'content': 'My first post', | ||
}, | ||
), | ||
result={ | ||
'id': 'c47726c6-a232-45f1-944f-60b98966ff1b', | ||
'title': 'Super post', | ||
'content': 'My first post', | ||
}, | ||
), | ||
], | ||
) | ||
@methods.add(context='request') | ||
@validator.validate | ||
def add_post(request: HttpRequest, post: PostIn) -> PostOut: | ||
""" | ||
Creates a post. | ||
:param request: http request | ||
:param object post: post data | ||
:return object: created post | ||
:raise AlreadyExistsError: post already exists | ||
""" | ||
|
||
post_id = uuid.uuid4().hex | ||
db['posts'][post_id] = post | ||
|
||
return PostOut(id=post_id, **post.dict()) | ||
|
||
|
||
@specs.annotate( | ||
tags=['posts'], | ||
errors=[NotFoundError], | ||
examples=[ | ||
specs.MethodExample( | ||
summary="Simple example", | ||
params=dict( | ||
post_id='c47726c6-a232-45f1-944f-60b98966ff1b', | ||
), | ||
result={ | ||
'id': 'c47726c6-a232-45f1-944f-60b98966ff1b', | ||
'title': 'Super post', | ||
'content': 'My first post', | ||
}, | ||
), | ||
], | ||
) | ||
@methods.add(context='request') | ||
@validator.validate | ||
def get_post(request: HttpRequest, post_id: uuid.UUID) -> PostOut: | ||
""" | ||
Returns a post. | ||
:param request: http request | ||
:param object post_id: post id | ||
:return object: registered post | ||
:raise NotFoundError: post not found | ||
""" | ||
|
||
post = db['posts'].get(post_id) | ||
if not post: | ||
raise NotFoundError() | ||
|
||
return PostOut(**post.dict()) | ||
|
||
|
||
@specs.annotate( | ||
tags=['posts'], | ||
errors=[NotFoundError], | ||
examples=[ | ||
specs.MethodExample( | ||
summary='Simple example', | ||
params=dict( | ||
post_id='c47726c6-a232-45f1-944f-60b98966ff1b', | ||
), | ||
result=None, | ||
), | ||
], | ||
) | ||
@methods.add(context='request') | ||
@validator.validate | ||
def delete_post(request: HttpRequest, post_id: uuid.UUID) -> None: | ||
""" | ||
Deletes a post. | ||
:param request: http request | ||
:param object post_id: post id | ||
:raise NotFoundError: post not found | ||
""" | ||
|
||
post = db['posts'].pop(post_id, None) | ||
if not post: | ||
raise NotFoundError() |
Oops, something went wrong.