hotfn 0.0.6

Creator: railscoder56

Last updated:

Add to Cart

Description:

hotfn 0.0.6

[![Build Status](https://travis-ci.org/denismakogon/hotfn-py.svg?branch=master)](https://travis-ci.org/denismakogon/hotfn-py)HTTP over STDIN/STDOUT lib parser=================================Purpose of this library to provide simple interface to parse HTTP 1.1 requests represented as stringRaw HTTP request----------------Parses raw HTTP request that contains: - method - route + query - headers - protocol version Optionally: - dataRaw HTTP request may have next look: GET /v1/apps?something=something&etc=etc HTTP/1.1 Host: localhost:8080 Content-Length: 5 Content-Type: application/x-www-form-urlencoded User-Agent: curl/7.51.0 helloEach new line define by set of special characters: \n \rand combination is: \r\nThis type of class stands for HTTP request parsing to a sane structure of: - HTTP request method - HTTP request URL - HTTP request query string represented as map - HTTP request headers represented as map - HTTP protocol version represented as tuple of major and minor versions - HTTP request body```pythonimport osimport sysfrom hotfn.http import requestwith os.fdopen(sys.stdin.fileno(), 'rb') as stdin: req = request.RawRequest(stdin) method, url, query_parameters, headers, (major, minor), body = req.parse_raw_request()```Raw HTTP response-----------------This type of class stands for transforming HTTP request object into valid string representation```pythonimport sysimport osfrom hotfn.http import requestfrom hotfn.http import responsewith os.fdopen(sys.stdin.fileno(), 'rb') as stdin: req = request.RawRequest(stdin) method, url, query_parameters, headers, (major, minor), body = req.parse_raw_request() resp = response.RawResponse((major, minor), 200, "OK", response_data=body) with os.fdopen(sys.stdout.fileno(), 'wb') as stdout: resp.dump(stdout)```Example-------Assume we have HTTP 1.1 request:```bashGET /v1/apps?something=something&etc=etc HTTP/1.1Host: localhost:8080Content-Length: 11Content-Type: application/x-www-form-urlencodedUser-Agent: curl/7.51.0hello:hello```This request can be transformed into data structure described above.Using code snippet mentioned above request data can be used to assemble a response object of the following view:```bashHTTP/1.1 200 OKContent-Length: 11Content-Type: text/plain; charset=utf-8hello:hello```This is totally valid HTTP response object.Notes-----Please be aware that response object by default sets content type as `text/plain; charset=utf-8`. If you need to change it use following code:```pythonimport osimport sysfrom hotfn.http import requestfrom hotfn.http import responsewith os.fdopen(sys.stdin.fileno(), 'rb') as stdin: req = request.RawRequest(stdin) method, url, query_parameters, headers, (major, minor), body = req.parse_raw_request() resp = response.RawResponse((major, minor), 200, "OK", response_data=body) resp.headers["Content-Type"] = "application/json" with os.fdopen(sys.stdout.fileno(), 'wb') as stdout: resp.dump(stdout)```Handling Hot Functions----------------------A main loop is supplied that can repeatedly call a user function with a series of HTTP requests.(TODO: should this use the WSGI API?)In order to utilise this, you can write your `app.py` as follows:```pythonfrom hotfn.http import workerfrom hotfn.http import responsedef app(context, **kwargs): body = kwargs.get('data') return response.RawResponse(context.version, 200, "OK", body.readall())if __name__ == "__main__": worker.run(app)```Automatic input coercions-------------------------Decorators are provided that will attempt to coerce input values to Python types.Some attempt is made to coerce return values from these functions also:```pythonfrom hotfn.http import worker@worker.coerce_input_to_content_typedef app(context, **kwargs): """ body is a request body, it's type depends on content type """ return kwargs.get('data')if __name__ == "__main__": worker.run(app)```Working with async automatic input coercions--------------------------------------------Latest version (from 0.0.6) supports async coroutines as a request body processors:```pythonimport asynciofrom hotfn.http import workerfrom hotfn.http import response@worker.coerce_input_to_content_typeasync def app(context, **kwargs): headers = { "Content-Type": "plain/text", } return response.RawResponse( context.version, 200, "OK", http_headers=headers, response_data="OK")if __name__ == "__main__": loop = asyncio.get_event_loop() worker.run(app, loop=loop)```As you can see `app` function is no longer callable, because its type: coroutine, so we need to bypass event loop inside

License

For personal and professional use. You cannot resell or redistribute these repositories in their original state.

Customer Reviews

There are no reviews.