Skip to main content

The web framework

Project description

Star resty

Object-oriented rest framework based on starlette, marshmallow and apispec.

Requirements

  • [Python] 3.7+
  • [Starlette] 0.12.0+
  • [Marshmallow] 3.0.0rc8+
  • [APISpec] 2.0.2+

Installation

$ pip install star_resty

Example

from dataclasses import dataclass
from typing import Optional

from marshmallow import Schema, fields, post_load, ValidationError
from starlette.applications import Starlette
from starlette.responses import UJSONResponse

from star_resty import Method, Operation, endpoint, json_schema, query, setup_spec


class EchoInput(Schema):
    a = fields.Int()


@dataclass
class Payload:
    a: int
    s: Optional[str] = None


class PayloadSchema(Schema):
    a = fields.Int(required=True)
    s = fields.String()

    @post_load
    def create_payload(self, data, **kwargs):
        return Payload(**data)


app = Starlette(debug=True)

@app.exception_handler(ValidationError)
def register_error(request, e: ValidationError):
    return UJSONResponse(e.normalized_messages(), status_code=400)


@app.route('/echo')
@endpoint
class Echo(Method):
    meta = Operation(tag='default',
                     description='echo')
    response_schema = EchoInput

    async def execute(self, query_params: query(EchoInput)):
        return query_params


@app.route('/post', methods=['POST'])
@endpoint
class Post(Method):
    meta = Operation(tag='default', description='post')

    async def execute(self, item: json_schema(PayloadSchema, Payload)):
        return {'a': item.a * 2, 's': item.s}


if __name__ == '__main__':
    import uvicorn

    setup_spec(app, title='Example')
    uvicorn.run(app, port=8080)

Open http://localhost:8080/apidocs.json to view generated openapi schema.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

star_resty-0.0.8.tar.gz (9.7 kB view details)

Uploaded Source

File details

Details for the file star_resty-0.0.8.tar.gz.

File metadata

  • Download URL: star_resty-0.0.8.tar.gz
  • Upload date:
  • Size: 9.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.33.0 CPython/3.7.4

File hashes

Hashes for star_resty-0.0.8.tar.gz
Algorithm Hash digest
SHA256 e4a5c6f97feaedf5ce7a2532e55a10db57c76d28d8f76625b7c2d1c8bf415a38
MD5 39308e4bf8111d16a0f99c17966310d2
BLAKE2b-256 911ba86b6325d4f222cdd99cb5f99b78d93ef6e564a93127bff4f342d4506d9d

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page