# Moesif Middleware for Python WSGI-based Frameworks
by [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.
[![Built For][ico-built-for]][link-built-for]
[![Latest Version][ico-version]][link-package]
[![Language Versions][ico-language]][link-language]
[![Software License][ico-license]][link-license]
[![Source Code][ico-source]][link-source]
With Moesif middleware for Python WSGI-based frameworks, you can automatically log API calls
and send them to [Moesif](https://www.moesif.com) for API analytics and monitoring.
> If you're new to Moesif, see [our Getting Started](https://www.moesif.com/docs/) resources to quickly get up and running.
## Overview
This middleware allows you to integrate Moesif's API analytics and
API monetization features with minimal configuration into APIs that are built on Python WSGI-based (Web Server Gateway Interface) frameworks.
[WSGI (Web Server Gateway Interface)](https://wsgi.readthedocs.io/en/latest/)
is a standard (PEP 3333) that describes
how a web server communicates with web applications. Many Python Frameworks
are build on top of WSGI, such as [Flask](http://flask.pocoo.org/),
[Bottle](https://bottlepy.org/docs/dev/), and [Pyramid](https://trypyramid.com/).
## Prerequisites
Before using this middleware, make sure you have the following:
- [An active Moesif account](https://moesif.com/wrap)
- [A Moesif Application ID](#get-your-moesif-application-id)
### Get Your Moesif Application ID
After 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:
1. Select the account icon to bring up the settings menu.
2. Select **Installation** or **API Keys**.
3. Copy your Moesif Application ID from the **Collector Application ID** field.
<img class="lazyload blur-up" src="images/app_id.png" width="700" alt="Accessing the settings menu in Moesif Portal">
## Install the Middleware
Install with `pip` using the following command:
```shell
pip install moesifwsgi
```
## Configure the Middleware
See the available [configuration options](#configuration-options) to learn how to configure the middleware for your use case.
## How to Use
### Flask
Wrap your `wsgi_app` with the Moesif middleware.
```python
from moesifwsgi import MoesifMiddleware
moesif_settings = {
'APPLICATION_ID': 'YOUR_MOESIF_APPLICATION_ID',
'LOG_BODY': True,
# ... For other options see below.
}
app.wsgi_app = MoesifMiddleware(app.wsgi_app, moesif_settings)
```
Replace *`YOUR_MOESIF_APPLICATION_ID`* with your [Moesif Application ID](#get-your-moesif-application-id).
For an example with Flask, see the `/examples/flask` folder of this repository.
### Bottle
Wrap your Bottle application with the Moesif middleware:
```python
from moesifwsgi import MoesifMiddleware
app = bottle.Bottle()
moesif_settings = {
'APPLICATION_ID': 'YOUR_MOESIF_APPLICATION_ID',
'LOG_BODY': True,
# ... For other options see below.
}
bottle.run(app=MoesifMiddleware(app, moesif_settings))
```
Replace *`YOUR_MOESIF_APPLICATION_ID`* with your [Moesif Application ID](#get-your-moesif-application-id).
For an example with Bottle, see the `/examples/bottle` folder of this repository.
### Pyramid
```python
from pyramid.config import Configurator
from moesifwsgi import MoesifMiddleware
if __name__ == '__main__':
config = Configurator()
config.add_route('hello', '/')
config.scan()
app = config.make_wsgi_app()
# configure your moesif settings
moesif_settings = {
'APPLICATION_ID': 'YOUR_MOESIF_APPLICATION_ID',
'LOG_BODY': True,
# ... For other options see below.
}
# Put middleware
app = MoesifMiddleware(app, moesif_settings)
server = make_server('0.0.0.0', 8080, app)
server.serve_forever()
```
Replace *`YOUR_MOESIF_APPLICATION_ID`* with your [Moesif Application ID](#get-your-moesif-application-id).
### Other WSGI Frameworks
If you are using a framework that is built on top of WSGI, it should work just by adding the Moesif middleware.
Please read the documentation for your specific framework on how to add middlewares.
### Optional: Capturing Outgoing API Calls
In addition to your own APIs, you can also start capturing calls out to third party services through by setting the `CAPTURE_OUTGOING_REQUESTS` option:
```python
from moesifwsgi import MoesifMiddleware
from flask import Flask
moesif_settings = {
'APPLICATION_ID': 'YOUR_MOESIF_APPLICATION_ID',
'LOG_BODY': True,
'CAPTURE_OUTGOING_REQUESTS': False
}
app = Flask(__name__)
app.wsgi_app = MoesifMiddleware(app.wsgi_app, moesif_settings)
```
For configuration options specific to capturing outgoing API calls, see [Options For Outgoing API Calls](#options-for-outgoing-api-calls).
## Troubleshoot
For a general troubleshooting guide that can help you solve common problems, see [Server Troubleshooting Guide](https://www.moesif.com/docs/troubleshooting/server-troubleshooting-guide/).
Other troubleshooting supports:
- [FAQ](https://www.moesif.com/docs/faq/)
- [Moesif support email](mailto:support@moesif.com)
### Solve Timezone Issue with Docker
When 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:
```
ENV TZ=UTC
```
Otherwise, you can add `RUN apt-get install tzdata` in the Dockerfile.
## Repository Structure
```
.
├── BUILDING.md
├── examples/
├── images/
├── LICENSE
├── MANIFEST.in
├── moesifwsgi/
├── README.md
├── requirements.txt
├── setup.cfg
└── setup.py
```
## Configuration options
The 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.
Notice the following about the configuration options:
- The `app` is the original WSGI app instance.
- The `environ` is a [WSGI `environ`](http://wsgi.readthedocs.io/en/latest/definitions.html).
Moesif also adds the following keys to `environ`:
- __`environ['moesif.request_body']`__: a JSON object or base64 encoded string if couldn't parse the request body as JSON
- __`environ["moesif.response_body_chunks"]`__: a response body chunks
- __`environ["moesif.response_headers"]`__: a dictionary representing the response headers
### `APPLICATION_ID`
<table>
<tr>
<th scope="col">
Data type
</th>
</tr>
<tr>
<td>
String
</td>
</tr>
</table>
A string that [identifies your application in Moesif](#get-your-moesif-application-id).
### `SKIP`
<table>
<tr>
<th scope="col">
Data type
</th>
<th scope="col">
Parameters
</th>
<th scope="col">
Return type
</th>
</tr>
<tr>
<td>
Function
</td>
<td>
<code>(app, environ)</code>
</td>
<td>
Boolean
</td>
</tr>
</table>
Optional.
A function that takes a WSGI application and an `environ` object,
and returns `True` if you want to skip this particular event.
### `IDENTIFY_USER`
<table>
<tr>
<th scope="col">
Data type
</th>
<th scope="col">
Parameters
</th>
<th scope="col">
Return type
</th>
</tr>
<tr>
<td>
Function
</td>
<td>
<code>(app, environ, response_headers)</code>
</td>
<td>
String
</td>
</tr>
</table>
Optional, but highly recommended.
A function with the following arguments:
- A WSGI application
- An `environ` object
- An optional parameter for response headers
Returns returns a string that represents the user ID used by your system.
Moesif identifies users automatically. However, due to the differences arising from different frameworks and implementations, provide this function to ensure user identification properly.
### `IDENTIFY_COMPANY`
<table>
<tr>
<th scope="col">
Data type
</th>
<th scope="col">
Parameters
</th>
<th scope="col">
Return type
</th>
</tr>
<tr>
<td>
Function
</td>
<td>
<code>(app, environ, response_headers)</code>
</td>
<td>
String
</td>
</tr>
</table>
Optional.
A function with the following arguments:
- A WSGI application
- An `environ` object
- An optional parameter for response headers
Returns a string that represents the company ID for this event.
### `GET_METADATA`
<table>
<tr>
<th scope="col">
Data type
</th>
<th scope="col">
Parameters
</th>
<th scope="col">
Return type
</th>
</tr>
<tr>
<td>
Function
</td>
<td>
<code>(app, environ)</code>
</td>
<td>
Dictionary
</td>
</tr>
</table>
Optional.
A function that takes a WSGI application and an `environ` object, and
returns a dictionary.
This function allows you
to add custom metadata that Moesif can associate with the event. The metadata must be a simple Python dictionary that can be converted to JSON.
For example, you may want to save a virtual machine instance ID, a trace ID, or a resource ID with the request.
### `GET_SESSION_TOKEN`
<table>
<tr>
<th scope="col">
Data type
</th>
<th scope="col">
Parameters
</th>
<th scope="col">
Return type
</th>
</tr>
<tr>
<td>
Function
</td>
<td>
<code>(app, environ)</code>
</td>
<td>
String
</td>
</tr>
</table>
Optional.
A function that takes a WSGI application and an `environ`, and returns a string that represents the session token for this event.
Similar 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.
### `MASK_EVENT_MODEL`
<table>
<tr>
<th scope="col">
Data type
</th>
<th scope="col">
Parameters
</th>
<th scope="col">
Return type
</th>
</tr>
<tr>
<td>
Function
</td>
<td>
<code>(EventModel)</code>
</td>
<td>
<code>EventModel</code>
</td>
</tr>
</table>
Optional.
A function that takes the final Moesif event model and returns an event model with desired data removed.
The 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).
### `DEBUG`
<table>
<tr>
<th scope="col">
Data type
</th>
</tr>
<tr>
<td>
Boolean
</td>
</tr>
</table>
Optional.
Set to `True` to print debug logs if you're having integration issues.
### `LOG_BODY`
<table>
<tr>
<th scope="col">
Data type
</th>
<th scope="col">
Default
</th>
</tr>
<tr>
<td>
Boolean
</td>
<td>
<code>True</code>
</td>
</tr>
</table>
Optional.
Whether to log request and response body to Moesif.
### `EVENT_QUEUE_SIZE`
<table>
<tr>
<th scope="col">
Data type
</th>
<th scope="col">
Default
</th>
</tr>
<tr>
<td>
<code>int</code>
</td>
<td>
<code>1000_000</code>
</td>
</tr>
</table>
Optional.
The 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.
### `EVENT_WORKER_COUNT`
<table>
<tr>
<th scope="col">
Data type
</th>
<th scope="col">
Default
</th>
</tr>
<tr>
<td>
<code>int</code>
</td>
<td>
<code>2</code>
</td>
</tr>
</table>
Optional.
The number of worker threads to use for uploading events to Moesif.
If you have a large number of events being logged, increasing this number can improve upload performance.
### `BATCH_SIZE`
<table>
<tr>
<th scope="col">
Data type
</th>
<th scope="col">
Default
</th>
</tr>
<tr>
<td>
<code>int</code>
</td>
<td>
<code>100</code>
</td>
</tr>
</table>
An optional field name that specifies the maximum batch size when sending to Moesif.
### `EVENT_BATCH_TIMEOUT`
<table>
<tr>
<th scope="col">
Data type
</th>
<th scope="col">
Default
</th>
</tr>
<tr>
<td>
<code>int</code>
</td>
<td>
<code>1</code>
</td>
</tr>
</table>
Optional.
Maximum time in seconds to wait before sending a batch of events to Moesif when reading from the queue.
### `AUTHORIZATION_HEADER_NAME`
<table>
<tr>
<th scope="col">
Data type
</th>
<th scope="col">
Default
</th>
</tr>
<tr>
<td>
String
</td>
<td>
<code>authorization</code>
</td>
</tr>
</table>
Optional.
A 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"`.
### `AUTHORIZATION_USER_ID_FIELD`
<table>
<tr>
<th scope="col">
Data type
</th>
<th scope="col">
Default
</th>
</tr>
<tr>
<td>
String
</td>
<td>
<code>sub</code>
</td>
</tr>
</table>
Optional.
A field name used to parse the user from authorization header in Moesif.
### `BASE_URI`
<table>
<tr>
<th scope="col">
Data type
</th>
</tr>
<tr>
<td>
String
</td>
</tr>
</table>
Optional.
A 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).
### Options For Outgoing API Calls
The 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.
Several 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.
If you are not using WSGI, you can import [`moesifpythonrequest`](https://github.com/Moesif/moesifpythonrequest) directly.
#### `CAPTURE_OUTGOING_REQUESTS`
<table>
<tr>
<th scope="col">
Data type
</th>
<th scope="col">
Default
</th>
</tr>
<tr>
<td>
Boolean
</td>
<td>
<code>False</code>
</td>
</tr>
</table>
Set to `True` to capture all outgoing API calls.
#### `GET_METADATA_OUTGOING`
<table>
<tr>
<th scope="col">
Data type
</th>
<th scope="col">
Parameters
</th>
<th scope="col">
Return type
</th>
</tr>
<tr>
<td>
Function
</td>
<td>
<code>(req, res)</code>
</td>
<td>
Dictionary
</td>
</tr>
</table>
Optional.
A function that enables you to return custom metadata associated with the logged API calls.
Takes in the [Requests](http://docs.python-requests.org/en/master/api/) request and response objects as arguments.
We recommend that you implement a function that
returns 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.
#### `SKIP_OUTGOING`
<table>
<tr>
<th scope="col">
Data type
</th>
<th scope="col">
Parameters
</th>
<th scope="col">
Return type
</th>
</tr>
<tr>
<td>
Function
</td>
<td>
<code>(req, res)</code>
</td>
<td>
Boolean
</td>
</tr>
</table>
Optional.
A function that takes a [Requests](http://docs.python-requests.org/en/master/api/) request and response objects,
and returns `True` if you want to skip this particular event.
#### `IDENTIFY_USER_OUTGOING`
<table>
<tr>
<th scope="col">
Data type
</th>
<th scope="col">
Parameters
</th>
<th scope="col">
Return type
</th>
</tr>
<tr>
<td>
Function
</td>
<td>
<code>(req, res)</code>
</td>
<td>
String
</td>
</tr>
</table>
Optional, but highly recommended.
A 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.
While Moesif tries to identify users automatically, different frameworks and your implementation might vary. So we highly recommend that you accurately provide a
user ID using this function.
#### `IDENTIFY_COMPANY_OUTGOING`
<table>
<tr>
<th scope="col">
Data type
</th>
<th scope="col">
Parameters
</th>
<th scope="col">
Return type
</th>
</tr>
<tr>
<td>
Function
</td>
<td>
<code>(req, res)</code>
</td>
<td>
String
</td>
</tr>
</table>
Optional.
A 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.
#### `GET_SESSION_TOKEN_OUTGOING`
<table>
<tr>
<th scope="col">
Data type
</th>
<th scope="col">
Parameters
</th>
<th scope="col">
Return type
</th>
</tr>
<tr>
<td>
Function
</td>
<td>
<code>(req, res)</code>
</td>
<td>
String
</td>
</tr>
</table>
Optional.
A 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.
Similar 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.
#### `LOG_BODY_OUTGOING`
<table>
<tr>
<th scope="col">
Data type
</th>
<th scope="col">
Default
</th>
</tr>
<tr>
<td>
Boolean
</td>
<td>
<code>True</code>
</td>
</tr>
</table>
Optional.
Set to `False` to remove logging request and response body.
## Examples
See the `examples/` directory for example applications using Flask, Falcon, and Bottle frameworks.
Here's a Flask example:
```python
def identify_user(app, environ, response_headers=dict()):
# Your custom code that returns a user id string
return "12345"
def identify_company(app, environ, response_headers=dict()):
# Your custom code that returns a company id string
return "67890"
def should_skip(app, environ):
# Your custom code that returns true to skip logging
return "health/probe" in environ.get('PATH_INFO', '')
def get_token(app, environ):
# If you don't want to use the standard WSGI session token,
# add your custom code that returns a string for session/API token
return "XXXXXXXXXXXXXX"
def mask_event(eventmodel):
# Your custom code to change or remove any sensitive fields
if 'password' in eventmodel.response.body:
eventmodel.response.body['password'] = None
return eventmodel
def get_metadata(app, environ):
return {
'datacenter': 'westus',
'deployment_version': 'v1.2.3',
}
moesif_settings = {
'APPLICATION_ID': 'YOUR_MOESIF_APPLICATION_ID',
'DEBUG': False,
'LOG_BODY': True,
'IDENTIFY_USER': identify_user,
'IDENTIFY_COMPANY': identify_company,
'GET_SESSION_TOKEN': get_token,
'SKIP': should_skip,
'MASK_EVENT_MODEL': mask_event,
'GET_METADATA': get_metadata,
'CAPTURE_OUTGOING_REQUESTS': False
}
app.wsgi_app = MoesifMiddleware(app.wsgi_app, moesif_settings)
```
The following examples demonstrate how to add and update customer information.
### Update A Single User
To create or update a [user](https://www.moesif.com/docs/getting-started/users/) profile in Moesif, use the `update_user()` function.
```python
api_client = MoesifAPIClient("Your Moesif Application Id").api
# Only user_id is required.
# Campaign object is optional, but useful if you want to track ROI of acquisition channels
# See https://www.moesif.com/docs/api#users for campaign schema
# metadata can be any custom object
user = {
'user_id': '12345',
'company_id': '67890', # If set, associate user with a company object
'campaign': {
'utm_source': 'google',
'utm_medium': 'cpc',
'utm_campaign': 'adwords',
'utm_term': 'api+tooling',
'utm_content': 'landing'
},
'metadata': {
'email': 'john@acmeinc.com',
'first_name': 'John',
'last_name': 'Doe',
'title': 'Software Engineer',
'sales_info': {
'stage': 'Customer',
'lifetime_value': 24000,
'account_owner': 'mary@contoso.com'
},
}
}
update_user = api_client.update_user(user)
```
The `metadata` field can contain any customer demographic or other info you want to store. Moesif only requires the `user_id` field.
For more information, see the function documentation in [Moesif Python API Reference](https://www.moesif.com/docs/api?python#update-a-user).
### Update Users in Batch
To update a list of [users](https://www.moesif.com/docs/getting-started/users/) in one batch, use the `update_users_batch()` function.
```python
api_client = MoesifAPIClient("Your Moesif Application Id").api
userA = {
'user_id': '12345',
'company_id': '67890', # If set, associate user with a company object
'metadata': {
'email': 'john@acmeinc.com',
'first_name': 'John',
'last_name': 'Doe',
'title': 'Software Engineer',
'sales_info': {
'stage': 'Customer',
'lifetime_value': 24000,
'account_owner': 'mary@contoso.com'
},
}
}
userB = {
'user_id': '54321',
'company_id': '67890', # If set, associate user with a company object
'metadata': {
'email': 'mary@acmeinc.com',
'first_name': 'Mary',
'last_name': 'Jane',
'title': 'Software Engineer',
'sales_info': {
'stage': 'Customer',
'lifetime_value': 48000,
'account_owner': 'mary@contoso.com'
},
}
}
update_users = api_client.update_users_batch([userA, userB])
```
The `metadata` field can contain any customer demographic or other info you want to store. Moesif only requires the `user_id` field.
For more information, see the function documentation in [Moesif Python API Reference](https://www.moesif.com/docs/api?python#update-users-in-batch).
### Update A Single Company
To update a single [company](https://www.moesif.com/docs/getting-started/companies/), use the `update_company()` function.
```python
api_client = MoesifAPIClient("Your Moesif Application Id").api
# Only company_id is required.
# Campaign object is optional, but useful if you want to track ROI of acquisition channels
# See https://www.moesif.com/docs/api#update-a-company for campaign schema
# metadata can be any custom object
company = {
'company_id': '67890',
'company_domain': 'acmeinc.com', # If domain is set, Moesif will enrich your profiles with publicly available info
'campaign': {
'utm_source': 'google',
'utm_medium': 'cpc',
'utm_campaign': 'adwords',
'utm_term': 'api+tooling',
'utm_content': 'landing'
},
'metadata': {
'org_name': 'Acme, Inc',
'plan_name': 'Free',
'deal_stage': 'Lead',
'mrr': 24000,
'demographics': {
'alexa_ranking': 500000,
'employee_count': 47
},
}
}
update_company = api_client.update_company(company)
```
The `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).
### Update Companies in Batch
To update a list of [companies](https://www.moesif.com/docs/getting-started/companies/) in one batch, use the `update_companies_batch()` function.
```python
api_client = MoesifAPIClient("Your Moesif Application Id").api
companyA = {
'company_id': '67890',
'company_domain': 'acmeinc.com', # If domain is set, Moesif will enrich your profiles with publicly available info
'metadata': {
'org_name': 'Acme, Inc',
'plan_name': 'Free',
'deal_stage': 'Lead',
'mrr': 24000,
'demographics': {
'alexa_ranking': 500000,
'employee_count': 47
},
}
}
companyB = {
'company_id': '09876',
'company_domain': 'contoso.com', # If domain is set, Moesif will enrich your profiles with publicly available info
'metadata': {
'org_name': 'Contoso, Inc',
'plan_name': 'Free',
'deal_stage': 'Lead',
'mrr': 48000,
'demographics': {
'alexa_ranking': 500000,
'employee_count': 53
},
}
}
update_companies = api_client.update_companies_batch([companyA, companyB])
```
The `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).
## How to Get Help
If you face any issues using this middleware, try the [troubheshooting guidelines](#troubleshoot). For further assistance, reach out to our [support team](mailto:support@moesif.com).
## Explore Other Integrations
Explore other integration options from Moesif:
- [Server integration options documentation](https://www.moesif.com/docs/server-integration//)
- [Client integration options documentation](https://www.moesif.com/docs/client-integration/)
[ico-built-for]: https://img.shields.io/badge/built%20for-python%20wsgi-blue.svg
[ico-version]: https://img.shields.io/pypi/v/moesifwsgi.svg
[ico-language]: https://img.shields.io/pypi/pyversions/moesifwsgi.svg
[ico-license]: https://img.shields.io/badge/License-Apache%202.0-green.svg
[ico-source]: https://img.shields.io/github/last-commit/moesif/moesifwsgi.svg?style=social
[link-built-for]: https://en.wikipedia.org/wiki/Web_Server_Gateway_Interface
[link-package]: https://pypi.python.org/pypi/moesifwsgi
[link-language]: https://pypi.python.org/pypi/moesifwsgi
[link-license]: https://raw.githubusercontent.com/Moesif/moesifwsgi/master/LICENSE
[link-source]: https://github.com/Moesif/moesifwsgi
Raw data
{
"_id": null,
"home_page": "https://www.moesif.com/docs/server-integration/python-wsgi/",
"name": "moesifwsgi",
"maintainer": null,
"docs_url": null,
"requires_python": null,
"maintainer_email": null,
"keywords": "log analysis restful api development debug wsgi flask bottle http middleware",
"author": "Moesif, Inc",
"author_email": "team@moesif.com",
"download_url": "https://files.pythonhosted.org/packages/51/a0/b73c7ecca205c3094bb1c9414a9e6c06314ff4ae66a1bfa698e953fbe9a0/moesifwsgi-1.9.8.tar.gz",
"platform": null,
"description": "# 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> 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<img class=\"lazyload blur-up\" src=\"images/app_id.png\" width=\"700\" alt=\"Accessing the settings menu in Moesif Portal\">\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### 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\u251c\u2500\u2500 BUILDING.md\n\u251c\u2500\u2500 examples/\n\u251c\u2500\u2500 images/\n\u251c\u2500\u2500 LICENSE\n\u251c\u2500\u2500 MANIFEST.in\n\u251c\u2500\u2500 moesifwsgi/\n\u251c\u2500\u2500 README.md\n\u251c\u2500\u2500 requirements.txt\n\u251c\u2500\u2500 setup.cfg\n\u2514\u2500\u2500 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<table>\n <tr>\n <th scope=\"col\">\n Data type\n </th>\n </tr>\n <tr>\n <td>\n String\n </td>\n </tr>\n</table>\n\nA string that [identifies your application in Moesif](#get-your-moesif-application-id).\n\n### `SKIP`\n<table>\n <tr>\n <th scope=\"col\">\n Data type\n </th>\n <th scope=\"col\">\n Parameters\n </th>\n <th scope=\"col\">\n Return type\n </th>\n </tr>\n <tr>\n <td>\n Function\n </td>\n <td>\n <code>(app, environ)</code>\n </td>\n <td>\n Boolean\n </td>\n </tr>\n</table>\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<table>\n <tr>\n <th scope=\"col\">\n Data type\n </th>\n <th scope=\"col\">\n Parameters\n </th>\n <th scope=\"col\">\n Return type\n </th>\n </tr>\n <tr>\n <td>\n Function\n </td>\n <td>\n <code>(app, environ, response_headers)</code>\n </td>\n <td>\n String\n </td>\n </tr>\n</table>\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<table>\n <tr>\n <th scope=\"col\">\n Data type\n </th>\n <th scope=\"col\">\n Parameters\n </th>\n <th scope=\"col\">\n Return type\n </th>\n </tr>\n <tr>\n <td>\n Function\n </td>\n <td>\n <code>(app, environ, response_headers)</code>\n </td>\n <td>\n String\n </td>\n </tr>\n</table>\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<table>\n <tr>\n <th scope=\"col\">\n Data type\n </th>\n <th scope=\"col\">\n Parameters\n </th>\n <th scope=\"col\">\n Return type\n </th>\n </tr>\n <tr>\n <td>\n Function\n </td>\n <td>\n <code>(app, environ)</code>\n </td>\n <td>\n Dictionary\n </td>\n </tr>\n</table>\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<table>\n <tr>\n <th scope=\"col\">\n Data type\n </th>\n <th scope=\"col\">\n Parameters\n </th>\n <th scope=\"col\">\n Return type\n </th>\n </tr>\n <tr>\n <td>\n Function\n </td>\n <td>\n <code>(app, environ)</code>\n </td>\n <td>\n String\n </td>\n </tr>\n</table>\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<table>\n <tr>\n <th scope=\"col\">\n Data type\n </th>\n <th scope=\"col\">\n Parameters\n </th>\n <th scope=\"col\">\n Return type\n </th>\n </tr>\n <tr>\n <td>\n Function\n </td>\n <td>\n <code>(EventModel)</code>\n </td>\n <td>\n <code>EventModel</code>\n </td>\n </tr>\n</table>\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<table>\n <tr>\n <th scope=\"col\">\n Data type\n </th>\n </tr>\n <tr>\n <td>\n Boolean\n </td>\n </tr>\n</table>\n\nOptional.\n\nSet to `True` to print debug logs if you're having integration issues.\n\n### `LOG_BODY`\n<table>\n <tr>\n <th scope=\"col\">\n Data type\n </th>\n <th scope=\"col\">\n Default\n </th>\n </tr>\n <tr>\n <td>\n Boolean\n </td>\n <td>\n <code>True</code>\n </td>\n </tr>\n</table>\n\nOptional.\n\nWhether to log request and response body to Moesif.\n\n### `EVENT_QUEUE_SIZE`\n<table>\n <tr>\n <th scope=\"col\">\n Data type\n </th>\n <th scope=\"col\">\n Default\n </th>\n </tr>\n <tr>\n <td>\n <code>int</code>\n </td>\n <td>\n <code>1000_000</code>\n </td>\n </tr>\n</table>\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<table>\n <tr>\n <th scope=\"col\">\n Data type\n </th>\n <th scope=\"col\">\n Default\n </th>\n </tr>\n <tr>\n <td>\n <code>int</code>\n </td>\n <td>\n <code>2</code>\n </td>\n </tr>\n</table>\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<table>\n <tr>\n <th scope=\"col\">\n Data type\n </th>\n <th scope=\"col\">\n Default\n </th>\n </tr>\n <tr>\n <td>\n <code>int</code>\n </td>\n <td>\n <code>100</code>\n </td>\n </tr>\n</table>\n\nAn optional field name that specifies the maximum batch size when sending to Moesif.\n\n### `EVENT_BATCH_TIMEOUT`\n<table>\n <tr>\n <th scope=\"col\">\n Data type\n </th>\n <th scope=\"col\">\n Default\n </th>\n </tr>\n <tr>\n <td>\n <code>int</code>\n </td>\n <td>\n <code>1</code>\n </td>\n </tr>\n</table>\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<table>\n <tr>\n <th scope=\"col\">\n Data type\n </th>\n <th scope=\"col\">\n Default \n </th>\n </tr>\n <tr>\n <td>\n String\n </td>\n <td>\n <code>authorization</code>\n </td>\n </tr>\n</table>\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<table>\n <tr>\n <th scope=\"col\">\n Data type\n </th>\n <th scope=\"col\">\n Default \n </th>\n </tr>\n <tr>\n <td>\n String\n </td>\n <td>\n <code>sub</code>\n </td>\n </tr>\n</table>\n\nOptional.\n\nA field name used to parse the user from authorization header in Moesif.\n\n### `BASE_URI`\n<table>\n <tr>\n <th scope=\"col\">\n Data type\n </th>\n </tr>\n <tr>\n <td>\n String\n </td>\n </tr>\n</table>\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<table>\n <tr>\n <th scope=\"col\">\n Data type\n </th>\n <th scope=\"col\">\n Default \n </th>\n </tr>\n <tr>\n <td>\n Boolean\n </td>\n <td>\n <code>False</code>\n </td>\n </tr>\n</table>\n\nSet to `True` to capture all outgoing API calls.\n\n#### `GET_METADATA_OUTGOING`\n<table>\n <tr>\n <th scope=\"col\">\n Data type\n </th>\n <th scope=\"col\">\n Parameters\n </th>\n <th scope=\"col\">\n Return type\n </th>\n </tr>\n <tr>\n <td>\n Function\n </td>\n <td>\n <code>(req, res)</code>\n </td>\n <td>\n Dictionary\n </td>\n </tr>\n</table>\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<table>\n <tr>\n <th scope=\"col\">\n Data type\n </th>\n <th scope=\"col\">\n Parameters\n </th>\n <th scope=\"col\">\n Return type\n </th>\n </tr>\n <tr>\n <td>\n Function\n </td>\n <td>\n <code>(req, res)</code>\n </td>\n <td>\n Boolean\n </td>\n </tr>\n</table>\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<table>\n <tr>\n <th scope=\"col\">\n Data type\n </th>\n <th scope=\"col\">\n Parameters\n </th>\n <th scope=\"col\">\n Return type\n </th>\n </tr>\n <tr>\n <td>\n Function\n </td>\n <td>\n <code>(req, res)</code>\n </td>\n <td>\n String\n </td>\n </tr>\n</table>\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<table>\n <tr>\n <th scope=\"col\">\n Data type\n </th>\n <th scope=\"col\">\n Parameters\n </th>\n <th scope=\"col\">\n Return type\n </th>\n </tr>\n <tr>\n <td>\n Function\n </td>\n <td>\n <code>(req, res)</code>\n </td>\n <td>\n String\n </td>\n </tr>\n</table>\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<table>\n <tr>\n <th scope=\"col\">\n Data type\n </th>\n <th scope=\"col\">\n Parameters\n </th>\n <th scope=\"col\">\n Return type\n </th>\n </tr>\n <tr>\n <td>\n Function\n </td>\n <td>\n <code>(req, res)</code>\n </td>\n <td>\n String\n </td>\n </tr>\n</table>\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<table>\n <tr>\n <th scope=\"col\">\n Data type\n </th>\n <th scope=\"col\">\n Default\n </th>\n </tr>\n <tr>\n <td>\n Boolean\n </td>\n <td>\n <code>True</code>\n </td>\n </tr>\n</table>\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 [troubheshooting 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",
"bugtrack_url": null,
"license": "Apache Software License",
"summary": "Moesif Middleware for Python WSGI based platforms (Flask, Bottle & Others)",
"version": "1.9.8",
"project_urls": {
"Homepage": "https://www.moesif.com/docs/server-integration/python-wsgi/"
},
"split_keywords": [
"log",
"analysis",
"restful",
"api",
"development",
"debug",
"wsgi",
"flask",
"bottle",
"http",
"middleware"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "fef273d859b8ef1f58c249118bb832ca9af7fc45f990324daa067b78f735acd9",
"md5": "1f8ab0820278ff6c206255103550e20d",
"sha256": "970315f28886c11b2cd403de9bde24da3b58d668734371906c4bba42b8894c3b"
},
"downloads": -1,
"filename": "moesifwsgi-1.9.8-py2.py3-none-any.whl",
"has_sig": false,
"md5_digest": "1f8ab0820278ff6c206255103550e20d",
"packagetype": "bdist_wheel",
"python_version": "py2.py3",
"requires_python": null,
"size": 34924,
"upload_time": "2024-12-04T21:08:52",
"upload_time_iso_8601": "2024-12-04T21:08:52.631429Z",
"url": "https://files.pythonhosted.org/packages/fe/f2/73d859b8ef1f58c249118bb832ca9af7fc45f990324daa067b78f735acd9/moesifwsgi-1.9.8-py2.py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "51a0b73c7ecca205c3094bb1c9414a9e6c06314ff4ae66a1bfa698e953fbe9a0",
"md5": "8c3104b1c022229d11f0ef3da6327367",
"sha256": "56ccb771fd5b011f0dc6787cd31b9e279ead7a445c5a937a70d6ce2700edb578"
},
"downloads": -1,
"filename": "moesifwsgi-1.9.8.tar.gz",
"has_sig": false,
"md5_digest": "8c3104b1c022229d11f0ef3da6327367",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 37768,
"upload_time": "2024-12-04T21:08:54",
"upload_time_iso_8601": "2024-12-04T21:08:54.570389Z",
"url": "https://files.pythonhosted.org/packages/51/a0/b73c7ecca205c3094bb1c9414a9e6c06314ff4ae66a1bfa698e953fbe9a0/moesifwsgi-1.9.8.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-12-04 21:08:54",
"github": false,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"lcname": "moesifwsgi"
}