Package: apitoolkit-django
API Toolkit Python Django SDKThe API Toolkit django client is an sdk used to integrate django web applications with APIToolkit.
It monitors incoming traffic, gathers the requests and sends the request to the apitoolkit servers.How to Integrate:First install the apitoolkit django sdk:pip install apitoolkit-djangoAdd your APIToolkit API key (APITOOLKIT_KEY) to your django settings:APITOOLKIT_KEY="<YOUR_API_KEY>"Then add apitoolkit middleware into the settings middleware list:MIDDLEWARE=[...,'apitoolkit_django.APIToolkit',...,]This will monitor all requests and send them to the APIToolkit's servers.Client Redacting fieldsWhile it's possible to mark a field as redacted from the apitoolkit dashboard, this client also supports redacting at the client side.
Client side redacting means that those fields would never leave your servers at all. So you feel safer that your sensitive data only stays on your servers.To mark fields that should be redacted, add them to your application's settings.
Eg:APITOOLKIT_REDACT_HEADERS=["Authorization","Cookie","Content-Length","Content-Type"]APITOOLKIT_REDACT_REQ_BODY=["$.password","$.credit_card"]APITOOLKIT_REDACT_RES_BODY=["$.credentials","$.social_security_number"]It is important to note that while theAPITOOLKIT_REDACT_HEADERSconfig field accepts a list of headers(case insensitive),
theAPITOOLKIT_REDACT_REQ_BODYandAPITOOLKIT_REDACT_RES_BODYexpect a list of JSONPath strings as arguments.The choice of JSONPath was selected to allow you have great flexibility in descibing which fields within your responses are sensitive.
Also note that these list of items to be redacted will be aplied to all endpoint requests and responses on your server.
To learn more about jsonpath to help form your queries, please take a look at this cheatsheet:https://lzone.de/cheat-sheet/JSONPathDebuggingYou can addAPITOOLKIT_DEBUGto your app settings file and set it toTrueto enable debug logging from the SDK. This will print out logs for each request/response captured by the middleware. APITOOLKIT_DEBUG defaults toFalse.Eg:APITOOLKIT_DEBUG=TrueTags and Service versionsYou can also add tags and service versions in your requests monitoring by adding them in yoursettings.pyfileExampleAPITOOLKIT_TAGS=["PROD","EU"]APITTOLKIT_SERVICE_VERSION="2.0.0"Outgoing RequestsTo monitor outgoing HTTP requests from your Django application, you can use theobserve_requestfunction from theapitoolkit_djangomodule. This allows you to capture and send copies of all incoming and outgoing requests to an apitoolkit server for monitoring and analysis.Examplefromdjango.httpimportJsonResponsefromapitoolkit_djangoimportobserve_request,report_errordefhello_world(request,name):resp=observe_request(request).get("https://jsonplaceholder.typicode.com/todos/2")resp.read()returnJsonResponse({"data":resp.read()})Theobserve_requestfunction wraps an httpx client and you can use it just like you would normally use httpx for any request you need.Error ReportingIf you’ve used sentry, or bugsnag, or rollbar, then you’re already familiar with this usecase.
But you can report an error to apitoolkit. A difference, is that errors are always associated with a parent request, and helps you query and associate the errors which occured while serving a given customer request. To request errors to APIToolkit use thereport_errorfunction from theapitoolkit_djangomodule to report an error you can report as many errors you want during a requestExamplefromdjango.httpimportJsonResponsefromapitoolkit_djangoimportobserve_request,report_errordefhello_world(request,name):try:resp=observe_request(request).get("https://jsonplaceholder.typicode.com/todos/2")resp.read()returnJsonResponse({"hello":"world"})exceptExceptionase:report_error(request,e)returnJsonResponse({"Error":"Something went wrong"})