code example

This commit is contained in:
2018-10-11 11:31:17 -04:00
parent f2b2128675
commit 2122f1ef1c
2 changed files with 33 additions and 6 deletions
+28 -1
View File
@@ -6,9 +6,36 @@ I'm adept to keep the "for humans" tagline off this project, until it comes out
The Python world certianly doesn't need more web frameworks. But, it does need more creativity, so I thought I'd bring some of my ideas to the table and see what I could come up with.
## Quickstart, already.
import responder
api = responder.API()
@api.route("/{greeting}")
def greet_world(req, resp, *, greeting):
resp.text = f"{greeting}, world!"
if __name__ == '__main__':
api.run()
Class-based views:
@api.route("/{greeting}")
class GreetingResource
def on_request(req, resp, *, greeting): # or on_get...
resp.text = f"{greeting}, world!"
# The Basic Idea
The primary concept here is to bring the nicities that are brought forth from both Flask and Falcon and unify them into a single framework, along with some new ideas I have. I also wanted to take some of the API primitaves that are instilled in the Requests library and put them into a web framework. So, you'll find a lot of parallels here with Requests.
The primary concept here is to bring the nicities that are brought forth from both Flask and Falcon and unify them into a single framework, along with some new ideas I have. I also wanted to take some of the API primitaves that are instilled in the Requests library and put them into a web framework. So, you'll find a lot of parallels here with Requests:
- Setting `resp.text` sends back unicode, while setting `resp.content` sends back bytes.
- Setting `resp.media` sends back JSON/YAML (`.text`/`.content` override this).
- Case-insensitive `req.headers` (from Requests directly).
- `resp.status_code`, `req.method`, `req.url`, and other familar friends.
## Old Ideas
+5 -5
View File
@@ -69,7 +69,7 @@ class Response:
self.status_code = HTTP_200
self.text = None
self.content = None
self.encoding = None
self.encoding = "utf-8"
self.media = None
self.mimetype = None
self.headers = {}
@@ -82,14 +82,14 @@ class Response:
if self.text:
return (
self.text.encode("utf-8"),
self.text.encode(self.encoding),
self.mimetype or "application/text",
{"Encoding": "utf-8"},
{"Encoding": self.encoding},
)
if self.req.accepts_yaml:
return (
yaml.dump(self.media).encode("utf-8"),
yaml.dump(self.media).encode(self.encoding),
self.mimetype or "application/x-yaml",
{"Content-Type": "application/x-yaml"},
)
@@ -107,7 +107,7 @@ class Response:
body, mimetype, headers = self.body
if isinstance(body, str):
body = body.encode("utf-8")
body = body.encode(self.encoding)
# print(self.req.headers)
if "gzip" in self.req.headers["Accept-Encoding"].lower():
gzip_buffer = io.BytesIO()