{"id":19294655,"url":"https://github.com/moesif/moesifdjango","last_synced_at":"2025-11-11T20:08:23.951Z","repository":{"id":43879432,"uuid":"71720267","full_name":"Moesif/moesifdjango","owner":"Moesif","description":" Moesif Middleware for Python Django, for API Monitoring, Analytics, and Monetization.","archived":false,"fork":false,"pushed_at":"2024-12-16T21:48:55.000Z","size":362,"stargazers_count":12,"open_issues_count":1,"forks_count":10,"subscribers_count":7,"default_branch":"master","last_synced_at":"2025-04-01T20:51:27.541Z","etag":null,"topics":["analytics","api-analytics","api-logs","api-management","api-monetization","api-monitoring","django-logging","django-middleware","django-rest-framework","interceptor","interceptors","logger","logging","monitoring","observability","python","python-django","usage-based-billing"],"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":"2016-10-23T18:08:33.000Z","updated_at":"2024-12-16T21:39:02.000Z","dependencies_parsed_at":"2023-11-11T02:27:05.040Z","dependency_job_id":"f4ad2ee5-121a-41c7-b4b1-51e0dc22cc68","html_url":"https://github.com/Moesif/moesifdjango","commit_stats":null,"previous_names":[],"tags_count":54,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Moesif%2Fmoesifdjango","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Moesif%2Fmoesifdjango/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Moesif%2Fmoesifdjango/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Moesif%2Fmoesifdjango/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Moesif","download_url":"https://codeload.github.com/Moesif/moesifdjango/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-analytics","api-logs","api-management","api-monetization","api-monitoring","django-logging","django-middleware","django-rest-framework","interceptor","interceptors","logger","logging","monitoring","observability","python","python-django","usage-based-billing"],"created_at":"2024-11-09T22:39:07.536Z","updated_at":"2025-11-11T20:08:18.915Z","avatar_url":"https://github.com/Moesif.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Moesif Middleware for Python Django\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\nMoesif middleware for Django automatically logs incoming and outgoing API calls\nand sends them to [Moesif](https://www.moesif.com) for API analytics and monitoring.\n\nThis SDK uses the Requests library and works for Python versions 2.7 up to 3.10.4.\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## 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\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\n\nInstall with pip:\n\n```shell\npip install moesifdjango\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\nIn your [Django settings file](https://docs.djangoproject.com/en/4.0/topics/settings/#default-settings), add `moesifdjango.middleware.moesif_middleware`\nto the [`MIDDLEWARE` array](https://docs.djangoproject.com/en/4.0/topics/http/middleware/#activating-middleware):\n\n```python\nMIDDLEWARE = [\n    'django.middleware.security.SecurityMiddleware',\n    'django.contrib.sessions.middleware.SessionMiddleware',\n    'django.middleware.common.CommonMiddleware',\n    'django.middleware.csrf.CsrfViewMiddleware',\n    'django.contrib.auth.middleware.AuthenticationMiddleware',\n    'moesifdjango.middleware.moesif_middleware',\n    'django.contrib.messages.middleware.MessageMiddleware',\n    'django.middleware.clickjacking.XFrameOptionsMiddleware',\n]\n```\n\nBecause of [middleware execution order](https://docs.djangoproject.com/en/4.0/ref/middleware/#middleware-ordering), we highly recommend that you add `moesif_middleweare` _after_ `SessionMiddleware` and `AuthenticationMiddleware`. These Django middleware add useful session data that enables deeper error analysis. On the other hand, if you have other middleware that modifies responses before going out, you may choose to place Moesif middleware before the middleware that modifies responses. This allows Moesif to see the modifications to the response data and see closer to what goes over the wire.\n\n### Changes in Django 1.10\n\nDjango middleware style and setup was refactored in version 1.10. You need need to import the correct version of Moesif middleware depending on your Django version:\n\n- For Django 1.10 or greater, use `moesifdjango.middleware.moesif_middleware`.\n- For Django 1.9 or older, you need to follow the legacy style for importing middleware and use `moesifdjango.middleware_pre19.MoesifMiddlewarePre19` instead.\n\nTo find your current Django version, you can execute the following command in your terminal:\n\n```sh\npython -c \"import django; print(django.get_version())\"\n```\n\n### Django 1.10 or Newer\n\nDjango 1.10 renamed `MIDDLEWARE_CLASSES` to `MIDDLEWARE.` Therefore, for Django 1.10 or newer, you must [activate your middleware](https://docs.djangoproject.com/en/4.0/topics/http/middleware/#activating-middleware) by adding it to the `MIDDLEWARE` list:\n\n```python\nMIDDLEWARE = [\n    ...,\n    'django.contrib.sessions.middleware.SessionMiddleware',\n    'django.middleware.common.CommonMiddleware',\n    'django.contrib.auth.middleware.AuthenticationMiddleware',\n    'moesifdjango.middleware.moesif_middleware'\n    ...\n]\n```\n\nThen add a dictionary `MOESIF_MIDDLEWARE` to your Django settings file:\n\n```\nMOESIF_MIDDLEWARE = {\n    'APPLICATION_ID': 'YOUR_MOESIF_APPLICATION_ID',\n    'LOG_BODY': True,\n}\n```\n\n### Django 1.9 or Older\n\n[Activate the middleware](https://docs.djangoproject.com/en/1.8/topics/http/middleware/#activating-middleware) by adding it to the `MIDDLEWARE_CLASSES` list in your Django settings file:\n\n```\nMIDDLEWARE_CLASSES = [\n    ...,\n    'moesifdjango.middleware_pre19.MoesifMiddlewarePre19',\n    # other middlewares\n]\n```\n\nThen add a dictionary `MOESIF_MIDDLEWARE` to your Django settings file:\n```\n\nMOESIF_MIDDLEWARE = {\n    'APPLICATION_ID': 'YOUR_MOESIF_APPLICATION_ID',\n    'LOG_BODY': True,\n}\n```\n\nThe dictionary contains the [configuration options](#configuration-options) for\nthe middleware, including your [Moesif Application ID](#get-your-moesif-application-id) that you must specify.\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](#capture_outgoing_requests).\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### 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├── images/\n├── LICENSE\n├── MANIFEST.in\n├── moesifdjango/\n├── pull_request_template.md\n├── README.md\n├── requirements.txt\n├── setup.cfg\n├── setup.py\n└── tests/\n```\n\n## Configuration options\nThe following sections describe the available configuration options for this middleware. You have to set these options in a Python dictionary `MOESIF_MIDDLEWARE`. See the [examples](#examples) for better understanding.\n\n#### `APPLICATION_ID` (Required)\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### Options specific to Incoming API Calls\nThe following options document the configuration options to use for incoming API calls.\n\nSeveral options use request and response as input arguments. These correspond to [Django's request and response objects](https://docs.djangoproject.com/en/1.10/ref/request-response/).\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(request, response)\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\nThis function takes Requests [`Request` and `Response` objects](https://requests.readthedocs.io/en/latest/user/advanced/#request-and-response-objects) and 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(request, response)\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\nThis function takes a request and a response and returns a string that represents\nthe user ID used by your system.\n\nMoesif tries to identify users automatically and fetch the username from [`django.http.HttpRequest.user`](https://docs.djangoproject.com/en/1.8/ref/request-response/#django.http.HttpRequest.user). However, if implementation differs, we highly recommend that you accurately provide a\nuser ID using this function.\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(request, response)\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 a request and a response and returns a string that represents the company ID for this event.\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(request, response)\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 a request and a response, and returns a string that represents the session token for this event.\n\nSimilar to [user IDs](#identify_user), 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#### `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(request, response)\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 request and a response and returns a Python dictionary.\n\nThe dictionary must be such that it can be converted into valid JSON. This allows you to associate this event with custom metadata. For example, you may want to save a virtual machine instance ID, a trace ID, or a tenant ID with the request.\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    \u003ccode\u003eBoolean\u003c/code\u003e\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\nSet to `False` to remove the HTTP body before sending to Moesif.\n\nIf you want more control over which fields the middleware includes or not, see the next option `MASK_EVENT_MODEL`.\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 `EventModel` object with desired data removed.\n\nUse this if you prefer to write your own mask function than uses the string based filter options:\n\n- `REQUEST_BODY_MASKS`\n- `REQUEST_HEADER_MASKS`\n- `RESPONSE_BODY_MASKS`\n- `RESPONSE_HEADER_MASKS`\n\nThe return value must be a valid `EventModel` required by Moesif data ingestion API. For more information about Moesif event model, see [Moesif Python API documentation](https://www.moesif.com/docs/api?python).\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\u003e25\u003c/code\u003e\n   \u003c/td\u003e\n  \u003c/tr\u003e\n\u003c/table\u003e\n\nOptional.\n\nSpecifies the maximum batch size when sending 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 events to hold in queue before sending to Moesif.\n\nIn case of network issues, the middleware may fail to connect or send event to Moesif. In those cases, the middleware skips adding new to event to queue to prevent memory overflow.\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 Django, 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### General options\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#### `LOCAL_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 internal log messages for debugging SDK integration issues.\n\n## Examples\nSee the [Moesif Django Example](https://github.com/Moesif/moesifdjangoexample) for a complete Django application that uses this middleware.\n\nThe following shows a sample Django settings file with different configuration options.\n\n```python\ndef identify_user(req, res):\n    # Your custom code that returns a user id string\n    if req.user and req.user.is_authenticated:\n        return req.user.username\n    else:\n        return None\n\ndef identify_company(req, res):\n    # Your custom code that returns a company id string\n    return \"67890\"\n\ndef should_skip(req, res):\n    # Your custom code that returns true to skip logging\n    return \"health/probe\" in req.path\n\ndef get_token(req, res):\n    # If you don't want to use the standard Django 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(req, res):\n    return {\n        'datacenter': 'westus',\n        'deployment_version': 'v1.2.3',\n    }\n\n\nMOESIF_MIDDLEWARE = {\n    'APPLICATION_ID': 'Your application id',\n    'LOCAL_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}\n```\n\nThe following examples demonstrate how to add and update customer information using the middleware.\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()` method.\n\n```python\nmiddleware = MoesifMiddleware(None)\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 = middleware.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. This method is a convenient helper that calls the Moesif API library.\n\nFor more information, see [Moesif Python API reference](https://www.moesif.com/docs/api?python#update-a-user).\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()` method.\n\n```python\nmiddleware = MoesifMiddleware(None)\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 = middleware.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. This method is a convenient helper that calls the Moesif API library.\n\nFor more information, see [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()` method.\n\n```python\nmiddleware = MoesifMiddleware(None)\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 = middleware.update_company(company)\n```\n\nThe `metadata` field can contain any company demographic or other info you want to store. Moesif only requires the `company_id` field. This method is a convenient helper that calls the Moesif API library.\n\nFor more information, see [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()` method.\n\n```python\nmiddleware = MoesifMiddleware(None)\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 = middleware.update_companies_batch([userA, userB])\n```\n\nThe `metadata` field can contain any company demographic or other info you want to store. Moesif only requires the `company_id` field. This method is a convenient helper that calls the Moesif API library.\n\nFor more information, see [Moesif Python API reference](https://www.moesif.com/docs/api?python#update-companies-in-batch).\n\n### Update Subscription\n\n#### Update A Single Subscription\nTo create or update a subscription in Moesif, use the `update_subscription` method.\n\n```python\nmiddleware = MoesifMiddleware(None)\n\n# Only subscription_id is required.\n# metadata can be any custom object\nsubscription = {\n  'subscription_id': 'sub_67890',\n  'company_id': '3456',\n  'status': 'active'\n  'metadata': {\n    'plan_name': 'Pro',\n    'signup_date': '2020-09-09',\n    'expiration_date': '2021-09-09',\n    'payment_method': 'credit_card',\n    'mrr': 12000,\n    'currency': 'USD'\n  }\n}\n\nupdate_subscription = middleware.update_subscription(subscription)\n```\n\nThe `metadata` field can store any subscription-related information. Moesif only requires the the following fields:\n\n- `subscription_id`\n- `company_id`\n- `status`\n\nThis method is a convenient helper that calls the Moesif API library.\n\nFor more information, see [Moesif Python API reference](https://www.moesif.com/docs/api?python#update-a-subscription).\n\n### Update Subscriptions in Batch\nTo update a list of subscriptions in one batch, use the `update_subscriptions_batch` method.\n\n```python\nmiddleware = MoesifMiddleware(None)\n\nsubscriptionA = {\n  'subscription_id': 'sub_67890',\n  'company_id': '3456',\n  'status': 'active'\n  'metadata': {\n    'plan_name': 'Pro',\n    'signup_date': '2020-09-09',\n    'expiration_date': '2021-09-09',\n    'payment_method': 'credit_card',\n    'mrr': 12000,\n    'currency': 'USD'\n  }\n}\n\nsubscriptionB = {\n  'subscription_id': 'sub_54321',\n  'company_id': '6789',\n  'status': 'active'\n  'metadata': {\n    'plan_name': 'Enterprise',\n    'signup_date': '2020-10-01',\n    'expiration_date': '2021-10-01',\n    'payment_method': 'paypal',\n    'mrr': 24000,\n    'currency': 'USD'\n  }\n}\n\nupdate_subscriptions = middleware.update_subscriptions_batch([subscriptionA, subscriptionB])\n```\n\nThe `metadata` field can store any subscription-related information. Moesif only requires the the following fields:\n\n- `subscription_id`\n- `company_id`\n- `status`\n\nThis method is a convenient helper that calls the Moesif API library.\n\nFor more information, see [Moesif Python API reference](https://www.moesif.com/docs/api?python#update-subscriptions-in-batch).\n\n## Tested Python and Django Versions\n\nMoesif has validated this middleware against the following combinations of Python and Django versions:\n\n| Python        | Django     |\n| ------------  | -------    |\n| Python 2.7    | 1.11.22    |\n| Python 2.7    | 1.11.22    |\n| Python 2.7    | 1.9        |\n| Python 3.4.5  | 1.11.22    |\n| Python 3.4.5  | 1.11.22    |\n| Python 3.4.5  | 1.9        |\n| Python 3.6.4  | 1.11.22    |\n| Python 3.6.4  | 1.11.22    |\n| Python 3.6.4  | 1.9        |\n| Python 3.10.4 | 3.2.13 LTS |\n| Python 3.10.4 | 4.0.5      |\n\n## How to Test\n1. Manually clone this repository.\n2. From your terminal, navigate to the root directory of the middleware.\n3. Run `pip install Django` and then run `pip install moesifdjango`.\n4. Add your [Moesif Application ID](#get-your-moesif-application-id) to `tests/settings.py`.\n5. From terminal, navigate to the root directory of the middleware tests `tests/`.\n\n   a. Run `python manage.py test` if you are using Django 1.10 or newer.\n\n   b. Run `python manage.py test middleware_pre19_tests` if you are using Django 1.9 or older.\n\n## Troubleshoot\n\nOn MacOS, with `gunicorn`, it is possible to encounter this error: \"in progress in another thread when fork() was called\". Solution is to add `--preload` option when launching `gunicorn` like below.\n\n```bash\ngunicorn myapp.wsgi:application --bind 127.0.0.1:8000 --preload\n```\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-django-blue.svg\n[ico-version]: https://img.shields.io/pypi/v/moesifdjango.svg\n[ico-language]: https://img.shields.io/pypi/pyversions/moesifdjango.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/moesifdjango.svg?style=social\n\n[link-built-for]: https://www.djangoproject.com/\n[link-package]: https://pypi.python.org/pypi/moesifdjango\n[link-language]: https://pypi.python.org/pypi/moesifdjango\n[link-license]: https://raw.githubusercontent.com/Moesif/moesifdjango/master/LICENSE\n[link-source]: https://github.com/Moesif/moesifdjango\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmoesif%2Fmoesifdjango","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmoesif%2Fmoesifdjango","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmoesif%2Fmoesifdjango/lists"}