Package: applipy-http
Applipy HTTPpip install applipy_httpApplipy HTTP is a library that implements an AppHandle that starts a HTTP
server, usingaiohttp.Basic UsageApplipy HTTP is designed to be used by declaring the HTTP servers in the
application configuration file and registering APIs to those servers.First, lets declare a couple of HTTP servers, one anonymous and one with namedemo:# dev.yamlapp:name:http-demomodules:-applipy_http.HttpModulehttp.servers:-host:0.0.0.0port:8080-name:demohost:0.0.0.0port:8081Running applipy with the configuration above will result in an application that
exposes two HTTP servers: one at0.0.0.0:8080and the other at0.0.0.0:8081.We can now register APIs to those servers and endpoints to the APIs. We do that
by implementing them and binding them in modules.ExampleFirst, lets declare an API that has an endpoint/hellothat returnsHello World!onGETand register that API to the anonymous HTTP server:# hello.pyfromaiohttpimportwebfromapplipyimportModulefromapplipy_httpimportApi,Context,Endpoint,HttpModule,PathFormatterfromapplipy_injectimportwith_names# Endpoint implementationclassHelloEndpoint(Endpoint):def__init__(self):self.name='World'# handler for HTTP method GETasyncdefget(self,request:web.Request,context:Context)->web.StreamResponse:returnweb.Response(body=f'Hello{self.name}!')# handler for HTTP method POSTasyncdefpost(self,request:web.Request,context:Context)->web.StreamResponse:self.name=awaitrequest.text()returnweb.Response(body='Success')# path of the endpointdefpath(self)->str:return'/hello'classHelloModule(Module):defconfigure(self,bind,register):bind(Endpoint,HelloEndpoint,name='hello')# If no instance of PathFormatter (i.e. PrefixPathFormatter) is bound,# the API will fallback to using a PathFormatter instance# bind(PathFormatter, PrefixPathFormatter('v1'), name='hello')# here we register the API to the anonymous HTTP server# (name argument in bind() is not set)bind(with_names(Api,'hello'))@classmethoddefdepends_on(cls):returnHttpModule,Next, lets declare a second API with an endpoint/byethat returnsGood Bye!onGETand register it to thedemoHTTP server:# goodbye.pyfromaiohttpimportwebfromapplipyimportModulefromapplipy_httpimportApi,Context,Endpoint,HttpModule,PathFormatterfromapplipy_injectimportwith_namesclassGoodByeEndpoint(Endpoint):asyncdefget(self,request:web.Request,context:Context)->web.StreamResponse:returnweb.Response(body="Good Bye!")defpath(self)->str:return'/bye'classGoodByeModule(Module):defconfigure(self,bind,register):bind(Endpoint,GoodByeEndpoint,name='bye')bind(PathFormatter,name='bye')# here we register the API to the `demo` HTTP server# (name argument in bind() is set to `demo`)bind(with_names(Api,'bye'),name='demo')@classmethoddefdepends_on(cls):returnHttpModule,Finally, lets update the configuration file to include our modules:# dev.yamlapp:name:http-demomodules:-hello.HelloModule-goodbye.GoodByeModulelogging.level:INFOhttp.servers:-host:0.0.0.0port:8080-name:demohost:0.0.0.0port:8081To test it just installapplipy_http(andpyyaml, because the config is in
YAML and not in JSON) and run the application:pipinstallapplipy_httppyyaml
applipyThe implemented endpoints should be available in:http://0.0.0.0:8080/hellohttp://0.0.0.0:8081/byeAdvanced FeaturesCheck the docs at/docsfor explanations on the advanced functionalities.