{"id":19294650,"url":"https://github.com/moesif/moesifwsgi","last_synced_at":"2025-11-11T20:06:46.386Z","repository":{"id":39633633,"uuid":"89900556","full_name":"Moesif/moesifwsgi","owner":"Moesif","description":"Moesif Middleware for Python WSGI-based Frameworks, for API Monitoring, Analytics, and Monetization.","archived":false,"fork":false,"pushed_at":"2024-10-29T22:28:56.000Z","size":282,"stargazers_count":6,"open_issues_count":0,"forks_count":8,"subscribers_count":7,"default_branch":"master","last_synced_at":"2024-10-30T00:40:08.244Z","etag":null,"topics":["analytics","api-logs","api-monetization","api-monitoring","bottle","bottle-framework","flask","logger","logging","logging-library","monitoring","observability","pyramid-framework","python","python-3","usage-based-billing","wsgi","wsgi-framework","wsgi-middleware"],"latest_commit_sha":null,"homepage":"https://www.moesif.com","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Moesif.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-05-01T05:45:08.000Z","updated_at":"2024-10-29T21:57:47.000Z","dependencies_parsed_at":"2024-06-20T00:07:27.629Z","dependency_job_id":"70d6a47f-c33d-4f78-be25-4d759c6414aa","html_url":"https://github.com/Moesif/moesifwsgi","commit_stats":null,"previous_names":[],"tags_count":45,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Moesif%2Fmoesifwsgi","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Moesif%2Fmoesifwsgi/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Moesif%2Fmoesifwsgi/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Moesif%2Fmoesifwsgi/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Moesif","download_url":"https://codeload.github.com/Moesif/moesifwsgi/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250205978,"owners_count":21392159,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["analytics","api-logs","api-monetization","api-monitoring","bottle","bottle-framework","flask","logger","logging","logging-library","monitoring","observability","pyramid-framework","python","python-3","usage-based-billing","wsgi","wsgi-framework","wsgi-middleware"],"created_at":"2024-11-09T22:39:02.204Z","updated_at":"2025-11-11T20:06:41.334Z","avatar_url":"https://github.com/Moesif.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Moesif Middleware for Python WSGI-based Frameworks\nby [Moesif](https://moesif.com), the [API analytics](https://www.moesif.com/features/api-analytics) and [API monetization](https://www.moesif.com/solutions/metered-api-billing) platform.\n\n[![Built For][ico-built-for]][link-built-for]\n[![Latest Version][ico-version]][link-package]\n[![Language Versions][ico-language]][link-language]\n[![Software License][ico-license]][link-license]\n[![Source Code][ico-source]][link-source]\n\nWith Moesif middleware for Python WSGI-based frameworks, you can automatically log API calls\nand send them to [Moesif](https://www.moesif.com) for API analytics and monitoring.\n\n\u003e If you're new to Moesif, see [our Getting Started](https://www.moesif.com/docs/) resources to quickly get up and running.\n\n## Overview\nThis middleware allows you to integrate Moesif's API analytics and\nAPI monetization features with minimal configuration into APIs that are built on Python WSGI-based (Web Server Gateway Interface) frameworks.\n\n[WSGI (Web Server Gateway Interface)](https://wsgi.readthedocs.io/en/latest/)\nis a standard (PEP 3333) that describes\nhow a web server communicates with web applications. Many Python Frameworks\nare build on top of WSGI, such as [Flask](http://flask.pocoo.org/),\n[Bottle](https://bottlepy.org/docs/dev/), and [Pyramid](https://trypyramid.com/).\n\n## Prerequisites\nBefore using this middleware, make sure you have the following:\n\n- [An active Moesif account](https://moesif.com/wrap)\n- [A Moesif Application ID](#get-your-moesif-application-id)\n\n### Get Your Moesif Application ID\nAfter you log into [Moesif Portal](https://www.moesif.com/wrap), you can get your Moesif Application ID during the onboarding steps. You can always access the Application ID any time by following these steps from Moesif Portal after logging in:\n\n1. Select the account icon to bring up the settings menu.\n2. Select **Installation** or **API Keys**.\n3. Copy your Moesif Application ID from the **Collector Application ID** field.\n\n\u003cimg class=\"lazyload blur-up\" src=\"images/app_id.png\" width=\"700\" alt=\"Accessing the settings menu in Moesif Portal\"\u003e\n\n## Install the Middleware\nInstall with `pip` using the following command:\n\n```shell\npip install moesifwsgi\n```\n\n## Configure the Middleware\nSee the available [configuration options](#configuration-options) to learn how to configure the middleware for your use case.\n\n## How to Use\n\n### Flask\n\nWrap your `wsgi_app` with the Moesif middleware.\n\n```python\nfrom moesifwsgi import MoesifMiddleware\n\nmoesif_settings = {\n    'APPLICATION_ID': 'YOUR_MOESIF_APPLICATION_ID',\n    'LOG_BODY': True,\n    # ... For other options see below.\n}\n\napp.wsgi_app = MoesifMiddleware(app.wsgi_app, moesif_settings)\n```\n\nReplace *`YOUR_MOESIF_APPLICATION_ID`* with your [Moesif Application ID](#get-your-moesif-application-id).\n\n\nFor an example with Flask, see the `/examples/flask` folder of this repository.\n\n### Bottle\nWrap your Bottle application with the Moesif middleware:\n\n```python\n\nfrom moesifwsgi import MoesifMiddleware\n\napp = bottle.Bottle()\n\nmoesif_settings = {\n    'APPLICATION_ID': 'YOUR_MOESIF_APPLICATION_ID',\n    'LOG_BODY': True,\n    # ... For other options see below.\n}\n\nbottle.run(app=MoesifMiddleware(app, moesif_settings))\n```\n\nReplace *`YOUR_MOESIF_APPLICATION_ID`* with your [Moesif Application ID](#get-your-moesif-application-id).\n\nFor an example with Bottle, see the `/examples/bottle` folder of this repository.\n\n### Pyramid\n\n\n```python\nfrom pyramid.config import Configurator\nfrom moesifwsgi import MoesifMiddleware\n\nif __name__ == '__main__':\n    config = Configurator()\n    config.add_route('hello', '/')\n    config.scan()\n    app = config.make_wsgi_app()\n\n    # configure your moesif settings\n    moesif_settings = {\n        'APPLICATION_ID': 'YOUR_MOESIF_APPLICATION_ID',\n        'LOG_BODY': True,\n        # ... For other options see below.\n    }\n    # Put middleware\n    app = MoesifMiddleware(app, moesif_settings)\n\n    server = make_server('0.0.0.0', 8080, app)\n    server.serve_forever()\n\n```\n\nReplace *`YOUR_MOESIF_APPLICATION_ID`* with your [Moesif Application ID](#get-your-moesif-application-id).\n\n### Other WSGI Frameworks\n\nIf you are using a framework that is built on top of WSGI, it should work just by adding the Moesif middleware.\nPlease read the documentation for your specific framework on how to add middlewares.\n\n### Optional: Capturing Outgoing API Calls\nIn addition to your own APIs, you can also start capturing calls out to third party services through by setting the `CAPTURE_OUTGOING_REQUESTS` option:\n\n```python\nfrom moesifwsgi import MoesifMiddleware\nfrom flask import Flask\n\nmoesif_settings = {\n    'APPLICATION_ID': 'YOUR_MOESIF_APPLICATION_ID',\n    'LOG_BODY': True,\n    'CAPTURE_OUTGOING_REQUESTS': False\n}\n\napp = Flask(__name__)\n\napp.wsgi_app = MoesifMiddleware(app.wsgi_app, moesif_settings)\n```\n\nFor configuration options specific to capturing outgoing API calls, see [Options For Outgoing API Calls](#options-for-outgoing-api-calls).\n\n## Troubleshoot\nFor a general troubleshooting guide that can help you solve common problems, see [Server Troubleshooting Guide](https://www.moesif.com/docs/troubleshooting/server-troubleshooting-guide/).\n\nOther troubleshooting supports:\n\n- [FAQ](https://www.moesif.com/docs/faq/)\n- [Moesif support email](mailto:support@moesif.com)\n\n### Thread Pool Issues\n\nThis library manages a thread pool to send data to Moesif in the background without impacting your app's latency.\n\nHowever, the `preload` feature of Gunicorn (or `preload-app` for Hypercorn) may interfere with the thread pool before the worker is forked. If you encounter issues, avoid setting `preload` to `True`.\n\n### Solve Timezone Issue with Docker\nWhen using Docker with Ubuntu-based image, events may not be captured if the image fails to find any timezone configuration. To solve this issue, add the following line to your Dockerfile:\n\n```\nENV TZ=UTC\n```\n\nOtherwise, you can add `RUN apt-get install tzdata` in the Dockerfile.\n\n## Repository Structure\n\n```\n.\n├── BUILDING.md\n├── examples/\n├── images/\n├── LICENSE\n├── MANIFEST.in\n├── moesifwsgi/\n├── README.md\n├── requirements.txt\n├── setup.cfg\n└── setup.py\n```\n\n## Configuration options\nThe following sections describe the available configuration options for this middleware. You can set these options in a Python dictionary and then pass that as a parameter when you create the middleware instance. See the `examples/` folder for better understanding.\n\nNotice the following about the configuration options:\n\n- The `app` is the original WSGI app instance.\n- The `environ` is a [WSGI `environ`](http://wsgi.readthedocs.io/en/latest/definitions.html).\n\nMoesif also adds the following keys to `environ`:\n\n- __`environ['moesif.request_body']`__: a JSON object or base64 encoded string if couldn't parse the request body as JSON\n\n- __`environ[\"moesif.response_body_chunks\"]`__: a response body chunks\n\n- __`environ[\"moesif.response_headers\"]`__: a dictionary representing the response headers\n\n### `APPLICATION_ID`\n\u003ctable\u003e\n  \u003ctr\u003e\n   \u003cth scope=\"col\"\u003e\n    Data type\n   \u003c/th\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n   \u003ctd\u003e\n    String\n   \u003c/td\u003e\n  \u003c/tr\u003e\n\u003c/table\u003e\n\nA string that [identifies your application in Moesif](#get-your-moesif-application-id).\n\n### `SKIP`\n\u003ctable\u003e\n  \u003ctr\u003e\n   \u003cth scope=\"col\"\u003e\n    Data type\n   \u003c/th\u003e\n   \u003cth scope=\"col\"\u003e\n    Parameters\n   \u003c/th\u003e\n   \u003cth scope=\"col\"\u003e\n    Return type\n   \u003c/th\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n   \u003ctd\u003e\n    Function\n   \u003c/td\u003e\n   \u003ctd\u003e\n    \u003ccode\u003e(app, environ)\u003c/code\u003e\n   \u003c/td\u003e\n   \u003ctd\u003e\n    Boolean\n   \u003c/td\u003e\n  \u003c/tr\u003e\n\u003c/table\u003e\n\nOptional.\n\nA function that takes a WSGI application and an `environ` object,\nand returns `True` if you want to skip this particular event.\n\n### `IDENTIFY_USER`\n\u003ctable\u003e\n  \u003ctr\u003e\n   \u003cth scope=\"col\"\u003e\n    Data type\n   \u003c/th\u003e\n   \u003cth scope=\"col\"\u003e\n    Parameters\n   \u003c/th\u003e\n   \u003cth scope=\"col\"\u003e\n    Return type\n   \u003c/th\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n   \u003ctd\u003e\n    Function\n   \u003c/td\u003e\n   \u003ctd\u003e\n    \u003ccode\u003e(app, environ, response_headers)\u003c/code\u003e\n   \u003c/td\u003e\n   \u003ctd\u003e\n    String\n   \u003c/td\u003e\n  \u003c/tr\u003e\n\u003c/table\u003e\n\nOptional, but highly recommended.\n\nA function with the following arguments:\n\n- A WSGI application\n- An `environ` object\n- An optional parameter for response headers\n\nReturns returns a string that represents the user ID used by your system.\n\nMoesif identifies users automatically. However, due to the differences arising from different frameworks and implementations, provide this function to ensure user identification properly.\n\n### `IDENTIFY_COMPANY`\n\u003ctable\u003e\n  \u003ctr\u003e\n   \u003cth scope=\"col\"\u003e\n    Data type\n   \u003c/th\u003e\n   \u003cth scope=\"col\"\u003e\n    Parameters\n   \u003c/th\u003e\n   \u003cth scope=\"col\"\u003e\n    Return type\n   \u003c/th\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n   \u003ctd\u003e\n    Function\n   \u003c/td\u003e\n   \u003ctd\u003e\n    \u003ccode\u003e(app, environ, response_headers)\u003c/code\u003e\n   \u003c/td\u003e\n   \u003ctd\u003e\n    String\n   \u003c/td\u003e\n  \u003c/tr\u003e\n\u003c/table\u003e\n\nOptional.\n\nA function with the following arguments:\n\n- A WSGI application\n- An `environ` object\n- An optional parameter for response headers\n\nReturns a string that represents the company ID for this event.\n\n### `GET_METADATA`\n\u003ctable\u003e\n  \u003ctr\u003e\n   \u003cth scope=\"col\"\u003e\n    Data type\n   \u003c/th\u003e\n   \u003cth scope=\"col\"\u003e\n    Parameters\n   \u003c/th\u003e\n   \u003cth scope=\"col\"\u003e\n    Return type\n   \u003c/th\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n   \u003ctd\u003e\n    Function\n   \u003c/td\u003e\n   \u003ctd\u003e\n    \u003ccode\u003e(app, environ)\u003c/code\u003e\n   \u003c/td\u003e\n   \u003ctd\u003e\n    Dictionary\n   \u003c/td\u003e\n  \u003c/tr\u003e\n\u003c/table\u003e\n\nOptional.\n\nA function that takes a WSGI application and an `environ` object, and\nreturns a dictionary.\n\nThis function allows you\nto add custom metadata that Moesif can associate with the event. The metadata must be a simple Python dictionary that can be converted to JSON.\n\nFor example, you may want to save a virtual machine instance ID, a trace ID, or a resource ID with the request.\n\n### `GET_SESSION_TOKEN`\n\u003ctable\u003e\n  \u003ctr\u003e\n   \u003cth scope=\"col\"\u003e\n    Data type\n   \u003c/th\u003e\n   \u003cth scope=\"col\"\u003e\n    Parameters\n   \u003c/th\u003e\n   \u003cth scope=\"col\"\u003e\n    Return type\n   \u003c/th\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n   \u003ctd\u003e\n    Function\n   \u003c/td\u003e\n   \u003ctd\u003e\n    \u003ccode\u003e(app, environ)\u003c/code\u003e\n   \u003c/td\u003e\n   \u003ctd\u003e\n    String\n   \u003c/td\u003e\n  \u003c/tr\u003e\n\u003c/table\u003e\n\n\nOptional.\n\nA function that takes a WSGI application and an `environ`, and returns a string that represents the session token for this event.\n\nSimilar to users and companies, Moesif tries to retrieve session tokens automatically. But if it doesn't work for your service, provide this function to help identify sessions.\n\n### `MASK_EVENT_MODEL`\n\u003ctable\u003e\n  \u003ctr\u003e\n   \u003cth scope=\"col\"\u003e\n    Data type\n   \u003c/th\u003e\n   \u003cth scope=\"col\"\u003e\n    Parameters\n   \u003c/th\u003e\n   \u003cth scope=\"col\"\u003e\n    Return type\n   \u003c/th\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n   \u003ctd\u003e\n    Function\n   \u003c/td\u003e\n   \u003ctd\u003e\n    \u003ccode\u003e(EventModel)\u003c/code\u003e\n   \u003c/td\u003e\n   \u003ctd\u003e\n    \u003ccode\u003eEventModel\u003c/code\u003e\n   \u003c/td\u003e\n  \u003c/tr\u003e\n\u003c/table\u003e\n\nOptional.\n\nA function that takes the final Moesif event model and returns an event model with desired data removed.\n\nThe return value must be a valid eventt model required by Moesif data ingestion API. For more information about the `EventModel` object, see the [Moesif Python API documentation](https://www.moesif.com/docs/api?python).\n\n### `DEBUG`\n\u003ctable\u003e\n  \u003ctr\u003e\n   \u003cth scope=\"col\"\u003e\n    Data type\n   \u003c/th\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n   \u003ctd\u003e\n    Boolean\n   \u003c/td\u003e\n  \u003c/tr\u003e\n\u003c/table\u003e\n\nOptional.\n\nSet to `True` to print debug logs if you're having integration issues.\n\n### `LOG_BODY`\n\u003ctable\u003e\n  \u003ctr\u003e\n   \u003cth scope=\"col\"\u003e\n    Data type\n   \u003c/th\u003e\n   \u003cth scope=\"col\"\u003e\n    Default\n   \u003c/th\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n   \u003ctd\u003e\n    Boolean\n   \u003c/td\u003e\n   \u003ctd\u003e\n    \u003ccode\u003eTrue\u003c/code\u003e\n   \u003c/td\u003e\n  \u003c/tr\u003e\n\u003c/table\u003e\n\nOptional.\n\nWhether to log request and response body to Moesif.\n\n### `EVENT_QUEUE_SIZE`\n\u003ctable\u003e\n  \u003ctr\u003e\n   \u003cth scope=\"col\"\u003e\n    Data type\n   \u003c/th\u003e\n   \u003cth scope=\"col\"\u003e\n    Default\n   \u003c/th\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n   \u003ctd\u003e\n    \u003ccode\u003eint\u003c/code\u003e\n   \u003c/td\u003e\n   \u003ctd\u003e\n    \u003ccode\u003e1000_000\u003c/code\u003e\n   \u003c/td\u003e\n  \u003c/tr\u003e\n\u003c/table\u003e\n\nOptional.\n\nThe maximum number of event objects queued in memory pending upload to Moesif. For a full queue, additional calls to `MoesifMiddleware` returns immediately without logging the event. Therefore, set this option based on the event size and memory capacity you expect.\n\n### `EVENT_WORKER_COUNT`\n\u003ctable\u003e\n  \u003ctr\u003e\n   \u003cth scope=\"col\"\u003e\n    Data type\n   \u003c/th\u003e\n   \u003cth scope=\"col\"\u003e\n    Default\n   \u003c/th\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n   \u003ctd\u003e\n    \u003ccode\u003eint\u003c/code\u003e\n   \u003c/td\u003e\n   \u003ctd\u003e\n    \u003ccode\u003e2\u003c/code\u003e\n   \u003c/td\u003e\n  \u003c/tr\u003e\n\u003c/table\u003e\n\nOptional.\n\nThe number of worker threads to use for uploading events to Moesif.\n\nIf you have a large number of events being logged, increasing this number can improve upload performance.\n\n### `BATCH_SIZE`\n\u003ctable\u003e\n  \u003ctr\u003e\n   \u003cth scope=\"col\"\u003e\n    Data type\n   \u003c/th\u003e\n   \u003cth scope=\"col\"\u003e\n    Default\n   \u003c/th\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n   \u003ctd\u003e\n    \u003ccode\u003eint\u003c/code\u003e\n   \u003c/td\u003e\n   \u003ctd\u003e\n    \u003ccode\u003e100\u003c/code\u003e\n   \u003c/td\u003e\n  \u003c/tr\u003e\n\u003c/table\u003e\n\nAn optional field name that specifies the maximum batch size when sending to Moesif.\n\n### `EVENT_BATCH_TIMEOUT`\n\u003ctable\u003e\n  \u003ctr\u003e\n   \u003cth scope=\"col\"\u003e\n    Data type\n   \u003c/th\u003e\n   \u003cth scope=\"col\"\u003e\n    Default\n   \u003c/th\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n   \u003ctd\u003e\n    \u003ccode\u003eint\u003c/code\u003e\n   \u003c/td\u003e\n   \u003ctd\u003e\n    \u003ccode\u003e1\u003c/code\u003e\n   \u003c/td\u003e\n  \u003c/tr\u003e\n\u003c/table\u003e\n\nOptional.\n\nMaximum time in seconds to wait before sending a batch of events to Moesif when reading from the queue.\n\n### `AUTHORIZATION_HEADER_NAME`\n\u003ctable\u003e\n  \u003ctr\u003e\n   \u003cth scope=\"col\"\u003e\n    Data type\n   \u003c/th\u003e\n   \u003cth scope=\"col\"\u003e\n    Default\n   \u003c/th\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n   \u003ctd\u003e\n    String\n   \u003c/td\u003e\n   \u003ctd\u003e\n    \u003ccode\u003eauthorization\u003c/code\u003e\n   \u003c/td\u003e\n  \u003c/tr\u003e\n\u003c/table\u003e\n\nOptional.\n\nA request header field name used to identify the User in Moesif. It also supports a comma separated string. Moesif checks headers in order like `\"X-Api-Key,Authorization\"`.\n\n### `AUTHORIZATION_USER_ID_FIELD`\n\u003ctable\u003e\n  \u003ctr\u003e\n   \u003cth scope=\"col\"\u003e\n    Data type\n   \u003c/th\u003e\n   \u003cth scope=\"col\"\u003e\n    Default\n   \u003c/th\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n   \u003ctd\u003e\n    String\n   \u003c/td\u003e\n   \u003ctd\u003e\n    \u003ccode\u003esub\u003c/code\u003e\n   \u003c/td\u003e\n  \u003c/tr\u003e\n\u003c/table\u003e\n\nOptional.\n\nA field name used to parse the user from authorization header in Moesif.\n\n### `BASE_URI`\n\u003ctable\u003e\n  \u003ctr\u003e\n   \u003cth scope=\"col\"\u003e\n    Data type\n   \u003c/th\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n   \u003ctd\u003e\n    String\n   \u003c/td\u003e\n  \u003c/tr\u003e\n\u003c/table\u003e\n\nOptional.\n\nA local proxy hostname when sending traffic through secure proxy. Remember to set this field when using secure proxy. For more information, see [Secure Proxy documentation.](https://www.moesif.com/docs/platform/secure-proxy/#2-configure-moesif-sdk).\n\n### Options For Outgoing API Calls\n\nThe following options apply to outgoing API calls. These are calls you initiate using the Python [Requests](http://docs.python-requests.org/en/master/) library to third parties like Stripe or to your own services.\n\nSeveral options use request and response as input arguments. These correspond to the [Requests](http://docs.python-requests.org/en/master/api/) library's request or response objects.\n\nIf you are not using WSGI, you can import [`moesifpythonrequest`](https://github.com/Moesif/moesifpythonrequest) directly.\n\n#### `CAPTURE_OUTGOING_REQUESTS`\n\u003ctable\u003e\n  \u003ctr\u003e\n   \u003cth scope=\"col\"\u003e\n    Data type\n   \u003c/th\u003e\n   \u003cth scope=\"col\"\u003e\n    Default\n   \u003c/th\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n   \u003ctd\u003e\n    Boolean\n   \u003c/td\u003e\n   \u003ctd\u003e\n    \u003ccode\u003eFalse\u003c/code\u003e\n   \u003c/td\u003e\n  \u003c/tr\u003e\n\u003c/table\u003e\n\nSet to `True` to capture all outgoing API calls.\n\n#### `GET_METADATA_OUTGOING`\n\u003ctable\u003e\n  \u003ctr\u003e\n   \u003cth scope=\"col\"\u003e\n    Data type\n   \u003c/th\u003e\n   \u003cth scope=\"col\"\u003e\n    Parameters\n   \u003c/th\u003e\n   \u003cth scope=\"col\"\u003e\n    Return type\n   \u003c/th\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n   \u003ctd\u003e\n    Function\n   \u003c/td\u003e\n   \u003ctd\u003e\n    \u003ccode\u003e(req, res)\u003c/code\u003e\n   \u003c/td\u003e\n   \u003ctd\u003e\n    Dictionary\n   \u003c/td\u003e\n  \u003c/tr\u003e\n\u003c/table\u003e\n\nOptional.\n\nA function that enables you to return custom metadata associated with the logged API calls.\n\nTakes in the [Requests](http://docs.python-requests.org/en/master/api/) request and response objects as arguments.\n\nWe recommend that you implement a function that\nreturns a dictionary containing your custom metadata. The dictionary must be a valid one that can be encoded into JSON. For example, you may want to save a virtual machine instance ID, a trace ID, or a resource ID with the request.\n\n#### `SKIP_OUTGOING`\n\u003ctable\u003e\n  \u003ctr\u003e\n   \u003cth scope=\"col\"\u003e\n    Data type\n   \u003c/th\u003e\n   \u003cth scope=\"col\"\u003e\n    Parameters\n   \u003c/th\u003e\n   \u003cth scope=\"col\"\u003e\n    Return type\n   \u003c/th\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n   \u003ctd\u003e\n    Function\n   \u003c/td\u003e\n   \u003ctd\u003e\n    \u003ccode\u003e(req, res)\u003c/code\u003e\n   \u003c/td\u003e\n   \u003ctd\u003e\n    Boolean\n   \u003c/td\u003e\n  \u003c/tr\u003e\n\u003c/table\u003e\n\nOptional.\n\nA function that takes a [Requests](http://docs.python-requests.org/en/master/api/) request and response objects,\nand returns `True` if you want to skip this particular event.\n\n#### `IDENTIFY_USER_OUTGOING`\n\u003ctable\u003e\n  \u003ctr\u003e\n   \u003cth scope=\"col\"\u003e\n    Data type\n   \u003c/th\u003e\n   \u003cth scope=\"col\"\u003e\n    Parameters\n   \u003c/th\u003e\n   \u003cth scope=\"col\"\u003e\n    Return type\n   \u003c/th\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n   \u003ctd\u003e\n    Function\n   \u003c/td\u003e\n   \u003ctd\u003e\n    \u003ccode\u003e(req, res)\u003c/code\u003e\n   \u003c/td\u003e\n   \u003ctd\u003e\n    String\n   \u003c/td\u003e\n  \u003c/tr\u003e\n\u003c/table\u003e\n\nOptional, but highly recommended.\n\nA function that takes [Requests](http://docs.python-requests.org/en/master/api/) request and response objects, and returns a string that represents the user ID used by your system.\n\nWhile Moesif tries to identify users automatically, different frameworks and your implementation might vary. So we highly recommend that you accurately provide a\nuser ID using this function.\n\n#### `IDENTIFY_COMPANY_OUTGOING`\n\u003ctable\u003e\n  \u003ctr\u003e\n   \u003cth scope=\"col\"\u003e\n    Data type\n   \u003c/th\u003e\n   \u003cth scope=\"col\"\u003e\n    Parameters\n   \u003c/th\u003e\n   \u003cth scope=\"col\"\u003e\n    Return type\n   \u003c/th\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n   \u003ctd\u003e\n    Function\n   \u003c/td\u003e\n   \u003ctd\u003e\n    \u003ccode\u003e(req, res)\u003c/code\u003e\n   \u003c/td\u003e\n   \u003ctd\u003e\n    String\n   \u003c/td\u003e\n  \u003c/tr\u003e\n\u003c/table\u003e\n\nOptional.\n\nA function that takes [Requests](http://docs.python-requests.org/en/master/api/) request and response objects, and returns a string that represents the company ID for this event.\n\n#### `GET_SESSION_TOKEN_OUTGOING`\n\u003ctable\u003e\n  \u003ctr\u003e\n   \u003cth scope=\"col\"\u003e\n    Data type\n   \u003c/th\u003e\n   \u003cth scope=\"col\"\u003e\n    Parameters\n   \u003c/th\u003e\n   \u003cth scope=\"col\"\u003e\n    Return type\n   \u003c/th\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n   \u003ctd\u003e\n    Function\n   \u003c/td\u003e\n   \u003ctd\u003e\n    \u003ccode\u003e(req, res)\u003c/code\u003e\n   \u003c/td\u003e\n   \u003ctd\u003e\n    String\n   \u003c/td\u003e\n  \u003c/tr\u003e\n\u003c/table\u003e\n\nOptional.\n\nA function that takes [Requests](http://docs.python-requests.org/en/master/api/) request and response objects, and returns a string that corresponds to the session token for this event.\n\nSimilar to [user IDs](#identify_user_outgoing), Moesif tries to get the session token automatically. However, if you setup differs from the standard, this function can help tying up events together and help you replay the events.\n\n#### `LOG_BODY_OUTGOING`\n\u003ctable\u003e\n  \u003ctr\u003e\n   \u003cth scope=\"col\"\u003e\n    Data type\n   \u003c/th\u003e\n   \u003cth scope=\"col\"\u003e\n    Default\n   \u003c/th\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n   \u003ctd\u003e\n    Boolean\n   \u003c/td\u003e\n   \u003ctd\u003e\n    \u003ccode\u003eTrue\u003c/code\u003e\n   \u003c/td\u003e\n  \u003c/tr\u003e\n\u003c/table\u003e\n\nOptional.\n\nSet to `False` to remove logging request and response body.\n\n## Examples\nSee the `examples/` directory for example applications using Flask, Falcon, and Bottle frameworks.\n\nHere's a Flask example:\n\n```python\ndef identify_user(app, environ, response_headers=dict()):\n    # Your custom code that returns a user id string\n    return \"12345\"\n\ndef identify_company(app, environ, response_headers=dict()):\n    # Your custom code that returns a company id string\n    return \"67890\"\n\ndef should_skip(app, environ):\n    # Your custom code that returns true to skip logging\n    return \"health/probe\" in environ.get('PATH_INFO', '')\n\ndef get_token(app, environ):\n    # If you don't want to use the standard WSGI session token,\n    # add your custom code that returns a string for session/API token\n    return \"XXXXXXXXXXXXXX\"\n\ndef mask_event(eventmodel):\n    # Your custom code to change or remove any sensitive fields\n    if 'password' in eventmodel.response.body:\n        eventmodel.response.body['password'] = None\n    return eventmodel\n\ndef get_metadata(app, environ):\n    return {\n        'datacenter': 'westus',\n        'deployment_version': 'v1.2.3',\n    }\n\nmoesif_settings = {\n    'APPLICATION_ID': 'YOUR_MOESIF_APPLICATION_ID',\n    'DEBUG': False,\n    'LOG_BODY': True,\n    'IDENTIFY_USER': identify_user,\n    'IDENTIFY_COMPANY': identify_company,\n    'GET_SESSION_TOKEN': get_token,\n    'SKIP': should_skip,\n    'MASK_EVENT_MODEL': mask_event,\n    'GET_METADATA': get_metadata,\n    'CAPTURE_OUTGOING_REQUESTS': False\n}\n\napp.wsgi_app = MoesifMiddleware(app.wsgi_app, moesif_settings)\n\n```\n\nThe following examples demonstrate how to add and update customer information.\n\n### Update A Single User\nTo create or update a [user](https://www.moesif.com/docs/getting-started/users/) profile in Moesif, use the `update_user()` function.\n\n```python\napi_client = MoesifAPIClient(\"Your Moesif Application Id\").api\n\n# Only user_id is required.\n# Campaign object is optional, but useful if you want to track ROI of acquisition channels\n# See https://www.moesif.com/docs/api#users for campaign schema\n# metadata can be any custom object\nuser = {\n  'user_id': '12345',\n  'company_id': '67890', # If set, associate user with a company object\n  'campaign': {\n    'utm_source': 'google',\n    'utm_medium': 'cpc',\n    'utm_campaign': 'adwords',\n    'utm_term': 'api+tooling',\n    'utm_content': 'landing'\n  },\n  'metadata': {\n    'email': 'john@acmeinc.com',\n    'first_name': 'John',\n    'last_name': 'Doe',\n    'title': 'Software Engineer',\n    'sales_info': {\n        'stage': 'Customer',\n        'lifetime_value': 24000,\n        'account_owner': 'mary@contoso.com'\n    },\n  }\n}\n\nupdate_user = api_client.update_user(user)\n```\n\nThe `metadata` field can contain any customer demographic or other info you want to store. Moesif only requires the `user_id` field.\n\nFor more information, see the function documentation in [Moesif Python API Reference](https://www.moesif.com/docs/api?python#update-a-user).\n\n\n### Update Users in Batch\nTo update a list of [users](https://www.moesif.com/docs/getting-started/users/) in one batch, use the `update_users_batch()` function.\n\n```python\napi_client = MoesifAPIClient(\"Your Moesif Application Id\").api\n\nuserA = {\n  'user_id': '12345',\n  'company_id': '67890', # If set, associate user with a company object\n  'metadata': {\n    'email': 'john@acmeinc.com',\n    'first_name': 'John',\n    'last_name': 'Doe',\n    'title': 'Software Engineer',\n    'sales_info': {\n        'stage': 'Customer',\n        'lifetime_value': 24000,\n        'account_owner': 'mary@contoso.com'\n    },\n  }\n}\n\nuserB = {\n  'user_id': '54321',\n  'company_id': '67890', # If set, associate user with a company object\n  'metadata': {\n    'email': 'mary@acmeinc.com',\n    'first_name': 'Mary',\n    'last_name': 'Jane',\n    'title': 'Software Engineer',\n    'sales_info': {\n        'stage': 'Customer',\n        'lifetime_value': 48000,\n        'account_owner': 'mary@contoso.com'\n    },\n  }\n}\nupdate_users = api_client.update_users_batch([userA, userB])\n```\n\nThe `metadata` field can contain any customer demographic or other info you want to store. Moesif only requires the `user_id` field.\n\nFor more information, see the function documentation in [Moesif Python API Reference](https://www.moesif.com/docs/api?python#update-users-in-batch).\n\n### Update A Single Company\nTo update a single [company](https://www.moesif.com/docs/getting-started/companies/), use the `update_company()` function.\n\n```python\napi_client = MoesifAPIClient(\"Your Moesif Application Id\").api\n\n# Only company_id is required.\n# Campaign object is optional, but useful if you want to track ROI of acquisition channels\n# See https://www.moesif.com/docs/api#update-a-company for campaign schema\n# metadata can be any custom object\ncompany = {\n  'company_id': '67890',\n  'company_domain': 'acmeinc.com', # If domain is set, Moesif will enrich your profiles with publicly available info\n  'campaign': {\n    'utm_source': 'google',\n    'utm_medium': 'cpc',\n    'utm_campaign': 'adwords',\n    'utm_term': 'api+tooling',\n    'utm_content': 'landing'\n  },\n  'metadata': {\n    'org_name': 'Acme, Inc',\n    'plan_name': 'Free',\n    'deal_stage': 'Lead',\n    'mrr': 24000,\n    'demographics': {\n        'alexa_ranking': 500000,\n        'employee_count': 47\n    },\n  }\n}\n\nupdate_company = api_client.update_company(company)\n```\n\nThe `metadata` field can contain any company demographic or other information you want to store. Moesif only requires the `company_id` field. For more information, see the function documentation in [Moesif Python API Reference](https://www.moesif.com/docs/api?python#update-a-company).\n\n### Update Companies in Batch\nTo update a list of [companies](https://www.moesif.com/docs/getting-started/companies/) in one batch, use the `update_companies_batch()` function.\n\n\n```python\napi_client = MoesifAPIClient(\"Your Moesif Application Id\").api\n\ncompanyA = {\n  'company_id': '67890',\n  'company_domain': 'acmeinc.com', # If domain is set, Moesif will enrich your profiles with publicly available info\n  'metadata': {\n    'org_name': 'Acme, Inc',\n    'plan_name': 'Free',\n    'deal_stage': 'Lead',\n    'mrr': 24000,\n    'demographics': {\n        'alexa_ranking': 500000,\n        'employee_count': 47\n    },\n  }\n}\n\ncompanyB = {\n  'company_id': '09876',\n  'company_domain': 'contoso.com', # If domain is set, Moesif will enrich your profiles with publicly available info\n  'metadata': {\n    'org_name': 'Contoso, Inc',\n    'plan_name': 'Free',\n    'deal_stage': 'Lead',\n    'mrr': 48000,\n    'demographics': {\n        'alexa_ranking': 500000,\n        'employee_count': 53\n    },\n  }\n}\n\nupdate_companies = api_client.update_companies_batch([companyA, companyB])\n```\nThe `metadata` field can contain any company demographic or other information you want to store. Moesif only requires the `company_id` field. For more information, see the function documentation in [Moesif Python API Reference](https://www.moesif.com/docs/api?python#update-companies-in-batch).\n\n## How to Get Help\nIf you face any issues using this middleware, try the [troubleshooting guidelines](#troubleshoot). For further assistance, reach out to our [support team](mailto:support@moesif.com).\n\n## Explore Other Integrations\n\nExplore other integration options from Moesif:\n\n- [Server integration options documentation](https://www.moesif.com/docs/server-integration//)\n- [Client integration options documentation](https://www.moesif.com/docs/client-integration/)\n\n[ico-built-for]: https://img.shields.io/badge/built%20for-python%20wsgi-blue.svg\n[ico-version]: https://img.shields.io/pypi/v/moesifwsgi.svg\n[ico-language]: https://img.shields.io/pypi/pyversions/moesifwsgi.svg\n[ico-license]: https://img.shields.io/badge/License-Apache%202.0-green.svg\n[ico-source]: https://img.shields.io/github/last-commit/moesif/moesifwsgi.svg?style=social\n\n[link-built-for]: https://en.wikipedia.org/wiki/Web_Server_Gateway_Interface\n[link-package]: https://pypi.python.org/pypi/moesifwsgi\n[link-language]: https://pypi.python.org/pypi/moesifwsgi\n[link-license]: https://raw.githubusercontent.com/Moesif/moesifwsgi/master/LICENSE\n[link-source]: https://github.com/Moesif/moesifwsgi\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmoesif%2Fmoesifwsgi","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmoesif%2Fmoesifwsgi","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmoesif%2Fmoesifwsgi/lists"}