Package: simple-api-client
Simple Api ClientA simple API client for connecting to remote services.FeaturesAlong with common methods for creating and performing Json based API
requests, this library supports the following extra features.Convenience methods for adding Basic and Bearer auth headers.A convenience method for adding signed cookies.A timeout limit for all requests.A global or per-request retry limit to retry an API request if it fails.Uses a best effort approach to deal with malformed Json returned from an API endpoint.Each response is guaranteed to contain a status code and well formed parsed Json data.If something goes wrong, an error message will always be included in the data.Fully supports returning binary data (as bytes) from an API request.handles rate limiting when performing an API request. These requests can be configured to be retried before raising an error.InstallationInstall and update usingpipenvpipinstall-Usimple-api-clientUsageSimple use-casefromsimple_api_clientimportApiClientfromloggingimportLoggerlogger=Logger("simple-logger",level="DEBUG")client=ApiClient("http://www.example.com",logger)response=client.get("/example/endpoint")ifresponse.status_code==200:print(response.data)Creating more specific clientsThis client has been created to be as flexible as possible to be used as a
base class for more specific clients. All you uneed to do is extend theApiClientclass and add any suitable methods.fromflaskimportgfromflaskimportcurrent_appasappfromsimple_api_clientimportApiClientclassMyServiceClient(ApiClient):defuse_cookie_auth(self,data):name=app.config.get("COOKIE_NAME")signing_key=app.config.get("COOKIE_SIGNING_KEY")signing_key_id="trusted-service"payload={"data":data}self.add_signed_cookie(name,payload,signing_key_id,signing_key)defget_remote_resource():returnself.get("/example/endpoint")A note on securityIt's import to understand that when a client is initialised with headers and
cookies, these will remain set for the lifetime of the client or until
manually unset. If you don't want this state to remain in-between requests,
it's important to take action to reset the client. In aFlaskapplication this is easily
achieved by assigning the client to the special'g'
object.fromflaskimportgfromflaskimportcurrent_appasappfromsimple_api_clientimportApiClient@app.before_requestdefsetup_api_client():g.client=ApiClient("http://www.example.com",app.logger)Then in order to use it, import'g'fromflaskimportgresponse=g.client.get("/example/endpoint")The benefit of this pattern is that the client is reset for every flask request so you don't need to worry about stale data in the client.DevelopmentThe build pipeline require your tests to pass and code to be formattedMake sure you have Python 3.x installed on your machine (usepyenv).Install the dependencies withpipenv(making sure to include dev and pre-release packages):pipenvinstall--dev--preConfigure your environment:pipenvshell&&exportPYTHONPATH="$PWD"Run the tests:pytestOr with logging:pytest-sOr tests with coverage:pytest--cov=./Format the code withBlack:black$PWDReleasesCleanup the (.gitignored)distfolder (if you have one):rm-rfdistNotch up the version number insetup.pyand build:python3setup.pysdistbdist_wheelPush to PyPi (using the ScholarPack credentials when prompted)python3-mtwineupload--repository-urlhttps://upload.pypi.org/legacy/dist/*LinksReleases:https://pypi.org/project/simple-api-client/Code:https://github.com/ScholarPack/simple-api-client/