Package: aio-service-client
Service Client FrameworkService Client Framework powered by Python asyncio.The easiest way to implement a client to work with a service REST API.FeaturesEasy way to make request to service.AsyncIO implementation using aiohttp.Powerful plugin system.Useful plugins.Mock plugin in order to make tests.Opensource license: GNU LGPLv3Installation$pipinstallaio-service-clientGetting startedService client framework is used to call HTTP service API’s. So, you must define how to
communicate with this service API defining its endpoint:spec={"get_users":{"path":"/user","method":"get"},"get_user_detail":{"path":"/user/{user_id}","method":"get"},"create_user":{"path":"/user","method":"post"},"update_user":{"path":"/user/{user_id}","method":"put"}}Imagine you are using a Rest JSON API in order to manage users. So, your data must be sent
as a JSON and response must be a JSON string. It mean you must serialize every request payload
to a JSON, and parse every response as JSON. So, you only need to define JSON parser and serializer
for your service:service=ServiceClient(spec=spec,plugins=[PathToken()],base_path="http://example.com",parser=json_decoder,serializer=json_encoder)So, you are ready to make request to service API:resp=yield fromservice.call("get_users")# it could be called directly# resp = yield from service.get_users()# if response is like:# {"users": {"item": [{"userId": "12", "username": "foo"}, {"userId": "13", "username": "bar"}], "count": 2}print("Count:%d"%resp.data['users']['count'])foruserinresp.data['users']['items']:print("User `%s`:%s"%(user['userId'],user['username']))In order to send a payload you must usepayloadkeyword on call:resp=yield fromservice.call("create_user",payload={"username":"foobar"})# it could be called directly# resp = yield from service.create_user(payload={"username": "foobar"})# it will make a request like:# POST http://example.com/user## {"username": "foobar"}Changelogv0.7.2Fix Mock and Loggers plugins work together.Added RawDataMock in order to allow to set mock data as string or byte-string directly on spec.Added JsonDataMock in order to allow to set mock json data as a dictionary or list on spec.v0.7.1Python 3.9 compatible.Update aiohttp.v0.6.1Pool plugin now addblocked_by_poolattribute to session containing elapsed time (seconds) on pool. It allows
to log this time using log plugins.RateLimit plugin now addblocked_by_ratelimitattribute to session containing elapsed time (seconds) blocked by
rate limit. It allows to log this time using log plugins.Tests improved.Added new exceptions:service_client.plugins.TooManyRequestsPendingErrorandservice_client.plugins.TooMuchTimePendingError.Added decorator in order to help to build service clients. It allows to define a method using a request model
but to call it using keywords to build request model which will be used to call method.classRequestModel:def__init__(param_1=None):self.param_1=param_1classService:@build_parameter_objectasyncdefmethod_1(request:RequestModel):returndo_something(request)serv=Service()awaitserv.method_1(param_1=23)v0.6.0Improved Pool plugin. It now allows to set hard limit of pending requests, if it reach limit requests will
fail raisingRequestLimitError. In same way, it allows to set a timeout, in seconds, for pending requests and
it will raise same exception.Added new RateLimit plugin. It is similar to Pool plugin but using a period parameter, in seconds, in order to limit number
of request in this period.Improved error logging.Added new hookclosein order to notify plugins that client is going to close.Removed compatibility with Python 3.4.v0.5.4Made compatible with aiohttp 2.0.x.v0.5.2Made compatible with aiohttp 1.0.x.Simplified factory code.v0.5.1Resolved problem with requests streamed.v0.5.0Added factoriesAdded spec loadersv0.4.1Fix elapsed data on logs.v0.4.0Added newPoolplugin.ImprovedElapsedplugin.Added new hook in order to allow plugins to override response methods.v0.3.1Fix response when using Timeout plugin.v0.3.0Added TrackingToken plugin. Token is added to session and to response.Added a log formatter.Removed tracking token stuff from log plugins.Improved log plugins. They avoid to print body if it is streamed or must be hidden.Improved session wrapper.PluginsPathTokensIt allows to fill placeholders on path in order to build uri.service=ServiceClient(spec={"endpoint1":{"method":"get","path":"/endpoint/{placeholder1}/{placeholder2}"}},plugins=[PathToken()],base_path="http://example.com")resp=yield fromservice.call("endpoint1",placeholder1=21,placeholder1="foo")# It will make request:# GET http://example.com/endpoint/21/fooHeadersIt allows to define default headers, endpoint headers and request headers.service=ServiceClient(spec={"endpoint1":{"method":"get","path":"/endpoint/foo/bar","headers":{"X-fake-header":"header; data"}}},plugins=[Headers(headers={"content-type":"application/json"})],base_path="http://example.com")resp=yield fromservice.call("endpoint1",headers={"X-other-fake-header":"foo"})# It will make request:# GET http://example.com/endpoint/foo/bar# X-fake-header: header; data# content-type: application/json# X-other-fake-header: fooTimeoutIt allows to define default timeout for service request, endpoint or request.ElapsedIt adds elapsed time to response.TrackingTokenIt allows to assign a token for each pair request/response in order to identify them.QueryParamsIt allows to use query parameters on request. They could be defined at service client, endpoint or request.InnerLoggerIt allows to log request after serialize and response before parse.OuterLoggerIt allows to log request before serialize and response after parse.PoolIt allows to limit concurrent requests. Besides it allows to set a hard limit of pending requests and a timeout
for blocked ones.RateLimitIt allows to limit number of requests in a time period. Besides it allows to set a hard limit of
pending requests and a timeout for blocked ones.