|
1 | | -## Moesif Patch Python Requests |
| 1 | +## Moesif Python Requests |
2 | 2 |
|
3 | 3 | [![Built For][ico-built-for]][link-built-for] |
4 | 4 | [![Latest Version][ico-version]][link-package] |
5 | 5 | [![Language Versions][ico-language]][link-language] |
6 | 6 | [![Software License][ico-license]][link-license] |
7 | 7 | [![Source Code][ico-source]][link-source] |
8 | 8 |
|
9 | | -This is a library to patch the Requests lib for capturing outgoing API calls to send to Moesif for API Analytics. |
| 9 | +Interceptor for Python Requests lib to capture _outgoing_ API calls and sends to [Moesif](https://www.moesif.com) for API analytics and monitoring. |
10 | 10 |
|
11 | | -It is imported as a dependency in higher level middleware like [Moesif Django Middleware](https://github.com/Moesif/moesifdjango) and [Moesif WSGI Middleware](https://github.com/moesif/moesifwsgi) which we recommend checking out first. |
| 11 | +_If you are using Moesif's API monitoring SDKs like [Moesif Django](https://github.com/Moesif/moesifdjango) or [Moesif WSGI](https://github.com/moesif/moesifwsgi) to log _incoming_ API calls, this library is already included._ |
| 12 | + |
| 13 | +## How to install |
| 14 | + |
| 15 | +```shell |
| 16 | +pip install moesifpythonrequest |
| 17 | +``` |
| 18 | + |
| 19 | +Import the Moesif lib and call start_capture_outgoing. |
| 20 | +Moesif will start logging all API calls made from the requests lib. |
| 21 | + |
| 22 | +```python |
| 23 | +from moesifpythonrequest.start_capture.start_capture import StartCapture |
| 24 | +import requests |
| 25 | + |
| 26 | +moesif_settings = { |
| 27 | + 'APPLICATION_ID': 'Your Moesif Application Id' |
| 28 | +} |
| 29 | + |
| 30 | +def main(): |
| 31 | + # Outgoing API call to third party like Github / Stripe or to your own dependencies |
| 32 | + response = requests.get("http://httpbin.org/uuid") |
| 33 | + print(response.json()) |
| 34 | + |
| 35 | +StartCapture().start_capture_outgoing(moesif_settings) |
| 36 | +main() |
| 37 | +``` |
| 38 | + |
| 39 | +Your Moesif Application Id can be found in the [_Moesif Portal_](https://www.moesif.com/). |
| 40 | +After signing up for a Moesif account, your Moesif Application Id will be displayed during the onboarding steps. |
| 41 | + |
| 42 | +## Configuration options |
| 43 | + |
| 44 | +#### _`APPLICATION_ID`_ |
| 45 | +(required) _string_, Your Moesif Application Id which can be found by logging |
| 46 | +into the [_Moesif Portal_](https://www.moesif.com/), click on the top right menu, |
| 47 | +and then clicking _Installation_. |
| 48 | + |
| 49 | +##### __`GET_METADATA_OUTGOING`__ |
| 50 | +(optional) _(req, res) => dictionary_, a function that enables you to return custom metadata associated with the logged API calls. |
| 51 | +Takes in the [Requests](http://docs.python-requests.org/en/master/api/) request and response object as arguments. You should implement a function that |
| 52 | +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. |
| 53 | + |
| 54 | +##### __`IDENTIFY_USER_OUTGOING`__ |
| 55 | +(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, |
| 56 | +but different frameworks and your implementation might be very different, it would be helpful and much more accurate to provide this function. |
| 57 | + |
| 58 | +##### __`IDENTIFY_COMPANY_OUTGOING`__ |
| 59 | +(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. |
| 60 | + |
| 61 | +##### __`GET_SESSION_TOKEN_OUTGOING`__ |
| 62 | +(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. |
| 63 | + |
| 64 | +##### __`LOG_BODY_OUTGOING`__ |
| 65 | +(optional) _boolean_, default True, Set to False to remove logging request and response body. |
| 66 | + |
| 67 | +##### __`SKIP_OUTGOING`__ |
| 68 | +(optional) _(req, res) => boolean_, a function that takes a [Requests](http://docs.python-requests.org/en/master/api/) request and response, |
| 69 | +and returns true if you want to skip this particular event. |
| 70 | + |
| 71 | +## Example |
| 72 | + |
| 73 | +An example Moesif integration is [available on GitHub](https://github.com/Moesif/moesif-python-outgoing-example) |
| 74 | + |
| 75 | +## Other integrations |
| 76 | + |
| 77 | +To view more documentation on integration options, please visit __[the Integration Options Documentation](https://www.moesif.com/docs/getting-started/integration-options/).__ |
12 | 78 |
|
13 | 79 | [ico-built-for]: https://img.shields.io/badge/built%20for-python%20requests-blue.svg |
14 | 80 | [ico-version]: https://img.shields.io/pypi/v/moesifpythonrequest.svg |
|
0 commit comments