Integration Guide - Python Requests

Moesif Python Requests

Built For Latest Version Language Versions Software License Source Code

Interceptor for Python Requests lib to capture outgoing API calls and sends to Moesif for API analytics and monitoring.

If you are using Moesif’s API monitoring SDKs like Moesif Django or Moesif WSGI to log incoming API calls, this library is already included.

How to install

pip install moesifpythonrequest

Import the Moesif lib and call start_capture_outgoing. Moesif will start logging all API calls made from the requests lib.

from moesifpythonrequest.start_capture.start_capture import StartCapture
import requests

moesif_settings = {
    'APPLICATION_ID': 'Your Moesif Application Id'
}

def main():
    # Outgoing API call to third party like Github / Stripe or to your own dependencies
    response = requests.get("http://httpbin.org/uuid")
    print(response.json())

StartCapture().start_capture_outgoing(moesif_settings)
main()

Your Moesif Application Id can be found in the Moesif Portal. After signing up for a Moesif account, your Moesif Application Id will be displayed during the onboarding steps.

Configuration options

APPLICATION_ID

(required) string, Your Moesif Application Id which can be found by logging into the Moesif Portal, click on the top right menu, and then clicking Installation.

GET_METADATA_OUTGOING

(optional) (req, res) => dictionary, a function that enables you to return custom metadata associated with the logged API calls. Takes in the Requests request and response object as arguments. You should implement a function that returns a dictionary containing your custom metadata. (must be able to be encoded into JSON). For example, you may want to save a VM instance_id, a trace_id, or a resource_id with the request.

IDENTIFY_USER_OUTGOING

(optional, but highly recommended) (req, res) => string, a function that takes Requests request and response, and returns a string that is the user id used by your system. While Moesif tries to identify users automatically, but different frameworks and your implementation might be very different, it would be helpful and much more accurate to provide this function.

IDENTIFY_COMPANY_OUTGOING

(optional) (req, res) => string, a function that takes Requests request and response, and returns a string that is the company id for this event.

GET_SESSION_TOKEN_OUTGOING

(optional) (req, res) => string, a function that takes Requests request and response, and returns a string that is the session token for this event. Again, Moesif tries to get the session token automatically, but if you setup is very different from standard, this function will be very help for tying events together, and help you replay the events.

LOG_BODY_OUTGOING

(optional) boolean, default True, Set to False to remove logging request and response body.

SKIP_OUTGOING

(optional) (req, res) => boolean, a function that takes a Requests request and response, and returns true if you want to skip this particular event.

MASK_EVENT_MODEL

(optional) (EventModel) => EventModel, a function that takes a Moesif EventModel and returns an EventModel with desired data removed. For details regarding EventModel please see the Moesif Python API Documentation.

Example

An example Moesif integration is available on GitHub

Other integrations

To view more documentation on integration options, please visit the Integration Options Documentation.