moesifwsgi


Namemoesifwsgi JSON
Version 1.9.4 PyPI version JSON
download
home_pagehttps://www.moesif.com/docs/server-integration/python-wsgi/
SummaryMoesif Middleware for Python WSGI based platforms (Flask, Bottle & Others)
upload_time2024-01-11 02:11:27
maintainer
docs_urlNone
authorMoesif, Inc
requires_python
licenseApache Software License
keywords log analysis restful api development debug wsgi flask bottle http middleware
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Moesif Middleware for Python WSGI based Frameworks

[![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]

WSGI middleware that automatically logs _incoming_ or _outgoing_ API calls and sends to [Moesif](https://www.moesif.com) for API analytics and monitoring.
Supports Python Frameworks built on WSGI such as Flask, Bottle, and Pyramid.

[Source Code on GitHub](https://github.com/moesif/moesifwsgi)

[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/), [Pyramid](https://trypyramid.com/) etc.
Moesif WSGI Middleware help APIs that are build on top of these Frameworks to
easily integrate with [Moesif](https://www.moesif.com).

## How to install

```shell
pip install moesifwsgi
```

## 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)

```

Your Moesif Application Id can be found in the [_Moesif Portal_](https://www.moesif.com/).
After signing up for a Moesif account, your Moesif Application Id will be displayed during the onboarding steps. 

You can always find your Moesif Application Id at any time by logging 
into the [_Moesif Portal_](https://www.moesif.com/), click on the top right menu,
and then clicking _API Keys_.

For an example with Flask, see example in the `/examples/flask` folder of this repo.

### Bottle
Wrap your bottle app 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))

```

For an example with Bottle, see example in the `/examples/bottle` folder of this repo.

### 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()

```
### 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 middleware.

## Configuration options

The app is the original WSGI app instance, and the environ is a [WSGI environ](http://wsgi.readthedocs.io/en/latest/definitions.html).
Also, Moesif adds the following to the environ variable
```
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 dict representing the response headers
```

#### __`APPLICATION_ID`__
(__required__), _string_, is obtained via your Moesif Account, this is required.

#### __`SKIP`__
(optional) _(app, environ) => boolean_, a function that takes a WSGI app and an environ,
and returns true if you want to skip this particular event.

#### __`IDENTIFY_USER`__
(optional, but highly recommended) _(app, environ, response_headers) => string_, a function that takes an app, an environ and an optional parameter response headers, and returns a string that is the user id used by your system. While Moesif tries to identify users automatically,
but different frameworks and your implementation might be very different, it would be helpful and much more accurate to provide this function.

#### __`IDENTIFY_COMPANY`__
(optional) _(app, environ, response_headers) => string_, a function that takes an app, an environ and an optional parameter response headers, and returns a string that is the company id for this event.

#### __`GET_METADATA`__
(optional) _(app, environ) => dictionary_, a function that takes an app and an environ, and
returns a dictionary (must be able to be encoded into JSON). This allows your
to associate this event with custom metadata. For example, you may want to save a VM instance_id, a trace_id, or a tenant_id with the request.

#### __`GET_SESSION_TOKEN`__
(optional) _(app, environ) => string_, a function that takes an app and an environ, and returns a string that is the session token for this event. Again, Moesif tries to get the session token automatically, but if you setup is very different from standard, this function will be very help for tying events together, and help you replay the events.

#### __`MASK_EVENT_MODEL`__
(optional) _(EventModel) => EventModel_, a function that takes an EventModel and returns an EventModel with desired data removed. The return value must be a valid EventModel required by Moesif data ingestion API. For details regarding EventModel please see the [Moesif Python API Documentation](https://www.moesif.com/docs/api?python).

#### __`DEBUG`__
(optional) _boolean_, a flag to see debugging messages.

#### __`LOG_BODY`__
(optional) _boolean_, default True, Set to False to remove logging request and response body.

#### __`EVENT_QUEUE_SIZE`__
(optional) __int__, default 1000000, the maximum number of event objects queued in memory pending upload to Moesif.  If the queue is full additional calls to `MoesifMiddleware` will return immediately without logging the event, so this number should be set based on the expected event size and memory capacity

### __`EVENT_WORKER_COUNT`__
(optional) __int__, default 2, 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`__
(optional) __int__, default 100, Maximum batch size when sending events to Moesif when reading from the queue

#### __`EVENT_BATCH_TIMEOUT`__
(optional) __int__, default 2, Maximum time in seconds to wait before sending a batch of events to Moesif when reading from the queue

#### __`AUTHORIZATION_HEADER_NAME`__
(optional) _string_, A request header field name used to identify the User in Moesif. Default value is `authorization`. Also, supports a comma separated string. We will check headers in order like `"X-Api-Key,Authorization"`.

#### __`AUTHORIZATION_USER_ID_FIELD`__
(optional) _string_, A field name used to parse the User from authorization header in Moesif. Default value is `sub`.

#### __`BASE_URI`__
(optional) _string_, A local proxy hostname when sending traffic via secure proxy. Please set this field when using secure proxy. For more details, refer [secure proxy documentation.](https://www.moesif.com/docs/platform/secure-proxy/#2-configure-moesif-sdk)

### Options specific to outgoing API calls 

The options below are applicable to outgoing API calls (calls you initiate using the Python [Requests](http://docs.python-requests.org/en/master/) lib to third parties like Stripe or to your own services.

For options that use the request and response as input arguments, these use the [Requests](http://docs.python-requests.org/en/master/api/) lib's request or response objects.

If you are not using WSGI, you can import the [moesifpythonrequest](https://github.com/Moesif/moesifpythonrequest) directly.

#### __`CAPTURE_OUTGOING_REQUESTS`__
_boolean_, Default False. Set to True to capture all outgoing API calls. False will disable this functionality. 

##### __`GET_METADATA_OUTGOING`__
(optional) _(req, res) => dictionary_, a function that enables you to return custom metadata associated with the logged API calls. 
Takes in the [Requests](http://docs.python-requests.org/en/master/api/) request and response object as arguments. You should implement a function that 
returns a dictionary containing your custom metadata. (must be able to be encoded into JSON). For example, you may want to save a VM instance_id, a trace_id, or a resource_id with the request.

##### __`SKIP_OUTGOING`__
(optional) _(req, res) => boolean_, a function that takes a [Requests](http://docs.python-requests.org/en/master/api/) request and response,
and returns true if you want to skip this particular event.

##### __`IDENTIFY_USER_OUTGOING`__
(optional, but highly recommended) _(req, res) => string_, a function that takes [Requests](http://docs.python-requests.org/en/master/api/) request and response, and returns a string that is the user id used by your system. While Moesif tries to identify users automatically,
but different frameworks and your implementation might be very different, it would be helpful and much more accurate to provide this function.

##### __`IDENTIFY_COMPANY_OUTGOING`__
(optional) _(req, res) => string_, a function that takes [Requests](http://docs.python-requests.org/en/master/api/) request and response, and returns a string that is the company id for this event.

##### __`GET_SESSION_TOKEN_OUTGOING`__
(optional) _(req, res) => string_, a function that takes [Requests](http://docs.python-requests.org/en/master/api/) request and response, and returns a string that is the session token for this event. Again, Moesif tries to get the session token automatically, but if you setup is very different from standard, this function will be very help for tying events together, and help you replay the events.

##### __`LOG_BODY_OUTGOING`__
(optional) _boolean_, default True, Set to False to remove logging request and response body.

### 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)

```

## Update User

### Update A Single User
Create or update a user profile in Moesif.
The metadata field can be any customer demographic or other info you want to store.
Only the `user_id` field is required.
For details, visit the [Python API Reference](https://www.moesif.com/docs/api?python#update-a-user).

```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)
```

### Update Users in Batch
Similar to update_user, but used to update a list of users in one batch. 
Only the `user_id` field is required.
For details, visit the [Python API Reference](https://www.moesif.com/docs/api?python#update-users-in-batch).

```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])
```

## Update Company

### Update A Single Company
Create or update a company profile in Moesif.
The metadata field can be any company demographic or other info you want to store.
Only the `company_id` field is required.
For details, visit the [Python API Reference](https://www.moesif.com/docs/api?python#update-a-company).

```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)
```

### Update Companies in Batch
Similar to update_company, but used to update a list of companies in one batch. 
Only the `company_id` field is required.
For details, visit the [Python API Reference](https://www.moesif.com/docs/api?python#update-companies-in-batch).

```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])
```
## Troubleshooting

When using Docker with Ubuntu based image, if events are not being captured, it could be possible as the image can't find any timezone configuration. 
In order to resolve that, add the following line to your Dockerfile
```
ENV TZ=UTC
```
or you could add `RUN apt-get install tzdata` in the Dockerfile.

## Other integrations

To view more documentation on integration options, please visit __[the Integration Options Documentation](https://www.moesif.com/docs/getting-started/integration-options/).__

[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": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "log analysis restful api development debug wsgi flask bottle http middleware",
    "author": "Moesif, Inc",
    "author_email": "xing@moesif.com",
    "download_url": "https://files.pythonhosted.org/packages/e9/88/07844cfcf25232dc98103ec02373ce893fc27f8151b35e4f42d2317a5383/moesifwsgi-1.9.4.tar.gz",
    "platform": null,
    "description": "# Moesif Middleware for Python WSGI based Frameworks\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\nWSGI middleware that automatically logs _incoming_ or _outgoing_ API calls and sends to [Moesif](https://www.moesif.com) for API analytics and monitoring.\nSupports Python Frameworks built on WSGI such as Flask, Bottle, and Pyramid.\n\n[Source Code on GitHub](https://github.com/moesif/moesifwsgi)\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/), [Pyramid](https://trypyramid.com/) etc.\nMoesif WSGI Middleware help APIs that are build on top of these Frameworks to\neasily integrate with [Moesif](https://www.moesif.com).\n\n## How to install\n\n```shell\npip install moesifwsgi\n```\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```\n\nYour Moesif Application Id can be found in the [_Moesif Portal_](https://www.moesif.com/).\nAfter signing up for a Moesif account, your Moesif Application Id will be displayed during the onboarding steps. \n\nYou can always find your Moesif Application Id at any time by logging \ninto the [_Moesif Portal_](https://www.moesif.com/), click on the top right menu,\nand then clicking _API Keys_.\n\nFor an example with Flask, see example in the `/examples/flask` folder of this repo.\n\n### Bottle\nWrap your bottle app 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```\n\nFor an example with Bottle, see example in the `/examples/bottle` folder of this repo.\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### 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 middleware.\n\n## Configuration options\n\nThe app is the original WSGI app instance, and the environ is a [WSGI environ](http://wsgi.readthedocs.io/en/latest/definitions.html).\nAlso, Moesif adds the following to the environ variable\n```\nenviron['moesif.request_body'] - A json object or base64 encoded string if couldn't parse the request body as json \nenviron[\"moesif.response_body_chunks\"] - A response body chunks\nenviron[\"moesif.response_headers\"] - A dict representing the response headers\n```\n\n#### __`APPLICATION_ID`__\n(__required__), _string_, is obtained via your Moesif Account, this is required.\n\n#### __`SKIP`__\n(optional) _(app, environ) => boolean_, a function that takes a WSGI app and an environ,\nand returns true if you want to skip this particular event.\n\n#### __`IDENTIFY_USER`__\n(optional, but highly recommended) _(app, environ, response_headers) => string_, a function that takes an app, an environ and an optional parameter response headers, and returns a string that is the user id used by your system. While Moesif tries to identify users automatically,\nbut different frameworks and your implementation might be very different, it would be helpful and much more accurate to provide this function.\n\n#### __`IDENTIFY_COMPANY`__\n(optional) _(app, environ, response_headers) => string_, a function that takes an app, an environ and an optional parameter response headers, and returns a string that is the company id for this event.\n\n#### __`GET_METADATA`__\n(optional) _(app, environ) => dictionary_, a function that takes an app and an environ, and\nreturns a dictionary (must be able to be encoded into JSON). This allows your\nto associate this event with custom metadata. For example, you may want to save a VM instance_id, a trace_id, or a tenant_id with the request.\n\n#### __`GET_SESSION_TOKEN`__\n(optional) _(app, environ) => string_, a function that takes an app and an environ, and returns a string that is the session token for this event. Again, Moesif tries to get the session token automatically, but if you setup is very different from standard, this function will be very help for tying events together, and help you replay the events.\n\n#### __`MASK_EVENT_MODEL`__\n(optional) _(EventModel) => EventModel_, a function that takes an EventModel and returns an EventModel with desired data removed. The return value must be a valid EventModel required by Moesif data ingestion API. For details regarding EventModel please see the [Moesif Python API Documentation](https://www.moesif.com/docs/api?python).\n\n#### __`DEBUG`__\n(optional) _boolean_, a flag to see debugging messages.\n\n#### __`LOG_BODY`__\n(optional) _boolean_, default True, Set to False to remove logging request and response body.\n\n#### __`EVENT_QUEUE_SIZE`__\n(optional) __int__, default 1000000, the maximum number of event objects queued in memory pending upload to Moesif.  If the queue is full additional calls to `MoesifMiddleware` will return immediately without logging the event, so this number should be set based on the expected event size and memory capacity\n\n### __`EVENT_WORKER_COUNT`__\n(optional) __int__, default 2, 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.\n\n#### __`BATCH_SIZE`__\n(optional) __int__, default 100, Maximum batch size when sending events to Moesif when reading from the queue\n\n#### __`EVENT_BATCH_TIMEOUT`__\n(optional) __int__, default 2, Maximum time in seconds to wait before sending a batch of events to Moesif when reading from the queue\n\n#### __`AUTHORIZATION_HEADER_NAME`__\n(optional) _string_, A request header field name used to identify the User in Moesif. Default value is `authorization`. Also, supports a comma separated string. We will check headers in order like `\"X-Api-Key,Authorization\"`.\n\n#### __`AUTHORIZATION_USER_ID_FIELD`__\n(optional) _string_, A field name used to parse the User from authorization header in Moesif. Default value is `sub`.\n\n#### __`BASE_URI`__\n(optional) _string_, A local proxy hostname when sending traffic via secure proxy. Please set this field when using secure proxy. For more details, refer [secure proxy documentation.](https://www.moesif.com/docs/platform/secure-proxy/#2-configure-moesif-sdk)\n\n### Options specific to outgoing API calls \n\nThe options below are applicable to outgoing API calls (calls you initiate using the Python [Requests](http://docs.python-requests.org/en/master/) lib to third parties like Stripe or to your own services.\n\nFor options that use the request and response as input arguments, these use the [Requests](http://docs.python-requests.org/en/master/api/) lib's request or response objects.\n\nIf you are not using WSGI, you can import the [moesifpythonrequest](https://github.com/Moesif/moesifpythonrequest) directly.\n\n#### __`CAPTURE_OUTGOING_REQUESTS`__\n_boolean_, Default False. Set to True to capture all outgoing API calls. False will disable this functionality. \n\n##### __`GET_METADATA_OUTGOING`__\n(optional) _(req, res) => dictionary_, a function that enables you to return custom metadata associated with the logged API calls. \nTakes in the [Requests](http://docs.python-requests.org/en/master/api/) request and response object as arguments. You should implement a function that \nreturns a dictionary containing your custom metadata. (must be able to be encoded into JSON). For example, you may want to save a VM instance_id, a trace_id, or a resource_id with the request.\n\n##### __`SKIP_OUTGOING`__\n(optional) _(req, res) => boolean_, a function that takes a [Requests](http://docs.python-requests.org/en/master/api/) request and response,\nand returns true if you want to skip this particular event.\n\n##### __`IDENTIFY_USER_OUTGOING`__\n(optional, but highly recommended) _(req, res) => string_, a function that takes [Requests](http://docs.python-requests.org/en/master/api/) request and response, and returns a string that is the user id used by your system. While Moesif tries to identify users automatically,\nbut different frameworks and your implementation might be very different, it would be helpful and much more accurate to provide this function.\n\n##### __`IDENTIFY_COMPANY_OUTGOING`__\n(optional) _(req, res) => string_, a function that takes [Requests](http://docs.python-requests.org/en/master/api/) request and response, and returns a string that is the company id for this event.\n\n##### __`GET_SESSION_TOKEN_OUTGOING`__\n(optional) _(req, res) => string_, a function that takes [Requests](http://docs.python-requests.org/en/master/api/) request and response, and returns a string that is the session token for this event. Again, Moesif tries to get the session token automatically, but if you setup is very different from standard, this function will be very help for tying events together, and help you replay the events.\n\n##### __`LOG_BODY_OUTGOING`__\n(optional) _boolean_, default True, Set to False to remove logging request and response body.\n\n### 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\n## Update User\n\n### Update A Single User\nCreate or update a user profile in Moesif.\nThe metadata field can be any customer demographic or other info you want to store.\nOnly the `user_id` field is required.\nFor details, visit the [Python API Reference](https://www.moesif.com/docs/api?python#update-a-user).\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\n### Update Users in Batch\nSimilar to update_user, but used to update a list of users in one batch. \nOnly the `user_id` field is required.\nFor details, visit the [Python API Reference](https://www.moesif.com/docs/api?python#update-users-in-batch).\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\n## Update Company\n\n### Update A Single Company\nCreate or update a company profile in Moesif.\nThe metadata field can be any company demographic or other info you want to store.\nOnly the `company_id` field is required.\nFor details, visit the [Python API Reference](https://www.moesif.com/docs/api?python#update-a-company).\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\n### Update Companies in Batch\nSimilar to update_company, but used to update a list of companies in one batch. \nOnly the `company_id` field is required.\nFor details, visit the [Python API Reference](https://www.moesif.com/docs/api?python#update-companies-in-batch).\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```\n## Troubleshooting\n\nWhen using Docker with Ubuntu based image, if events are not being captured, it could be possible as the image can't find any timezone configuration. \nIn order to resolve that, add the following line to your Dockerfile\n```\nENV TZ=UTC\n```\nor you could add `RUN apt-get install tzdata` in the Dockerfile.\n\n## Other integrations\n\nTo view more documentation on integration options, please visit __[the Integration Options Documentation](https://www.moesif.com/docs/getting-started/integration-options/).__\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.4",
    "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": "8214938ff2e7050661e1d0417b360d8259f079da634853841876b3ffedc19fb5",
                "md5": "c02e0672fabd5ca53802be5de7023357",
                "sha256": "321c9b9686e7ba5d26e4d6688a9511e5a181eb708bd40291545d924915b35379"
            },
            "downloads": -1,
            "filename": "moesifwsgi-1.9.4-py2.py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "c02e0672fabd5ca53802be5de7023357",
            "packagetype": "bdist_wheel",
            "python_version": "py2.py3",
            "requires_python": null,
            "size": 33229,
            "upload_time": "2024-01-11T02:11:25",
            "upload_time_iso_8601": "2024-01-11T02:11:25.054930Z",
            "url": "https://files.pythonhosted.org/packages/82/14/938ff2e7050661e1d0417b360d8259f079da634853841876b3ffedc19fb5/moesifwsgi-1.9.4-py2.py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "e98807844cfcf25232dc98103ec02373ce893fc27f8151b35e4f42d2317a5383",
                "md5": "633711977bdcd76007eb79525eda4980",
                "sha256": "cd07b68fbe9a1f18305a0b9bcdc34e62d72476e29a41e355c28afd777655b35c"
            },
            "downloads": -1,
            "filename": "moesifwsgi-1.9.4.tar.gz",
            "has_sig": false,
            "md5_digest": "633711977bdcd76007eb79525eda4980",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 34608,
            "upload_time": "2024-01-11T02:11:27",
            "upload_time_iso_8601": "2024-01-11T02:11:27.363464Z",
            "url": "https://files.pythonhosted.org/packages/e9/88/07844cfcf25232dc98103ec02373ce893fc27f8151b35e4f42d2317a5383/moesifwsgi-1.9.4.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-01-11 02:11:27",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "lcname": "moesifwsgi"
}
        
Elapsed time: 0.17531s