From 2122f1ef1c55265aedbc2945a5d201851f56ecb2 Mon Sep 17 00:00:00 2001 From: Kenneth Reitz Date: Thu, 11 Oct 2018 11:31:17 -0400 Subject: [PATCH] code example --- README.md | 29 ++++++++++++++++++++++++++++- responder/models.py | 10 +++++----- 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 3eea327..7a1d023 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/responder/models.py b/responder/models.py index f948435..212de3a 100644 --- a/responder/models.py +++ b/responder/models.py @@ -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()