CleanAPI
Pretty tornado wrapper for making lightweight REST API services
Installation:
pip install cleanapi
Example:
Project folders structure:
.
├── handlers
│ └── simple_handler.py
├── log
├── ssl
│ ├── ca.csr
│ └── ca.key
├── static_html
│ └── index.html
└── server_example.py
server_example.py
from cleanapi import server
if __name__ == '__main__':
# uses http protocol
server.start('http', 8080, '/', './handlers', './static_html')
# # uses https protocol
# server.start('https', 8443, '/', './handlers', './static_html',
# path_to_ssl='./ssl', ssl_certfile_name='ca.csr', ssl_keyfile_name='ca.key')
simple_handler.py
from cleanapi.server import BaseHandler
url_tail = '/example.json'
# noinspection PyAbstractClass
class Handler(BaseHandler):
"""
Test API request handler
"""
async def get(self):
self.set_status(200)
self.write({'status': 'working'})
index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Cleanapy demo</title>
<link rel="icon" href="/favicon.ico" type="image/x-icon" />
</head>
<body>
<h1>Cleanapy demo page</h1>
<p>Everything OK</p>
</body>
</html>
You also may put 'favicon.ico' file to the 'static_html' folder, but it is not necessary.
Then you can test server responses on http://localhost:8080 and http://localhost:8080/example.json
See log/cleanapi.log for information about externel access to the server
Example with PydanticHandler:
pydantic_handler.py
from cleanapi.server import PydanticHandler
from pydantic import BaseModel, validator, NonNegativeInt
from typing import Optional, List
url_tail = '/pydantic.json'
class PydanticRequest(BaseModel):
"""
Pydantic dataclass for request
"""
foo: NonNegativeInt
bar: NonNegativeInt
@validator('foo', 'bar')
def _validate_foo_bar(cls, val: str):
if val == 666:
raise ValueError(f'Values of foo and bar should not be equal to 666')
return val
class PydanticResponse(BaseModel):
"""
Pydantic dataclass for response
"""
summ: Optional[NonNegativeInt]
errors: Optional[List[dict]]
# noinspection PyAbstractClass
class Handler(PydanticHandler):
"""
Example of using PydanticHandler
"""
request_dataclass = PydanticRequest
result_dataclass = PydanticResponse
# noinspection PyUnusedLocal
def process(self, request: request_dataclass) -> result_dataclass:
"""
What the handler should do
:param request: incoming request
:type request: request_dataclass
:return: processing result
:type: result_data class
"""
result = PydanticResponse(summ=request.foo + request.bar, errors=[])
if result.summ > 1000:
raise ValueError('The sum of foo and bar is more than 1000')
return result
def if_exception(self, errors: list) -> None:
"""
What to do if an exception was thrown
:param errors: list of errors
:type errors: list
"""
self.set_status(400)
self.write({'errors': errors})
return
You can not test it with a browser because of POST method using. You have to use a program like Postman or some custom util like my pynger.py
Metadata
Release files for cleanapi 0.1.6
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| cleanapi-0.1.6.tar.gz | 20.2 kB | Without provenance | Details for cleanapi-0.1.6.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| cleanapi-0.1.6-py3-none-any.whl | Python 3 | none | any | Details for cleanapi-0.1.6-py3-none-any.whl |
Total release size: 41.4 kB
Release files / cleanapi-0.1.6.tar.gz
| Download URL | cleanapi-0.1.6.tar.gz |
|---|---|
| Size | 20.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
d022bf548fd7593c364a9cf5b6e67efbca6ad24149869d1ea6fbfdae94272d1c
|
|
BLAKE2b-256 checksum How to use checksums |
a8e53c5653104a752f3ef90437ec68b1b98827cd4ebf68104fca295e263d8c91
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.4.2 importlib_metadata/4.6.4 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.9.6
|
Release files / cleanapi-0.1.6-py3-none-any.whl
| Download URL | cleanapi-0.1.6-py3-none-any.whl |
|---|---|
| Size | 21.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
ea04ca92a4a58be561f731b7663b5a17d92c0e436d7e29e76dd8b6cf6dbef10b
|
|
BLAKE2b-256 checksum How to use checksums |
cfa2cb0d88a0f33a3810174ba9ebb2317bed0b33ed031bac85c6308626893df7
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.4.2 importlib_metadata/4.6.4 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.9.6
|
Security for cleanapi 0.1.6
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.