apitoolkit-pyramid


Nameapitoolkit-pyramid JSON
Version 0.1.6 PyPI version JSON
download
home_pageNone
SummaryA Python Pyramid SDK for Apitoolkit integration
upload_time2024-06-11 15:33:56
maintainerNone
docs_urlNone
authorAPIToolkit
requires_pythonNone
licenseNone
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # API Toolkit Python Pyramid SDK

The API Toolkit Pyramid client is an sdk used to integrate Pyramid web applications with APIToolkit.
It monitors incoming traffic, gathers the requests and sends the request to the apitoolkit servers.

## How to Integrate:

First install the apitoolkit pyramid sdk:
`pip install apitoolkit-pyramid`

Add your APIToolkit API key `APITOOLKIT_KEY` to your `development.ini` or `production.ini` files or in your settings:

```python
APITOOLKIT_KEY = "<YOUR_API_KEY>"
```

When using ini files separate mulitple values with comma.

```ini
APITOOLKIT_REDACT_HEADERS = X-Secret1,X-Secret2
```

Then add apitoolkit pyramid tween in your server's config:

```python
from wsgiref.simple_server import make_server
from pyramid.config import Configurator
from pyramid.response import Response
from pyramid.view import view_config


@view_config(
    route_name='home'
)
def home(request):
    return Response('Welcome!')

if __name__ == '__main__':
    setting = {"APITOOLKIT_KEY": "<YOUR_API_KEY>"}
    with Configurator(settings=setting) as config:
        # add aptoolkit tween
        config.add_tween("apitoolkit_pyramid.APIToolkit")
        config.add_route('home', '/')
        config.scan()
        app = config.make_wsgi_app()
    server = make_server('0.0.0.0', 6543, app)
    server.serve_forever()
```

This will monitor all requests and send them to the APIToolkit's servers.

## Configuration

You can add more configurations in your settings to customize behavior, such as redacting senstive fields, printing values to help with debugging and so on.

### Configuration Parameters

`APITOOLKIT_KEY`: `required` API key for accessing the APIToolkit service

`APITOOLKIT_REDACT_HEADERS`: `optional` List of headers to redact in captured requests.

`APITOOLKIT_DEBUG`: `optional` Flag to enable debug mode.

`APITOOLKIT_REDACT_REQ_BODY`: `optional` List of fields to redact in request bodies.

`APITOOLKIT_REDACT_RES_BODY`: `optional` List of fields to redact in response bodies.

`APITOOLKIT_ROUTES_WHITELIST`: `optional` List of routes prefixes that should be captured.

`APITOOLKIT_IGNORE_HTTP_CODES`: `optional` List of HTTP status codes that should NOT be captured.

`APITOOLKIT_SERVICE_VERSION`: `optional` Version of the service (helps in monitoring different versions of your deployments).

`APITOOLKIT_TAGS`: `optional` Tags associated with the service.

## Client Redacting fields

The SDK provides a way for customers to redact senstive fields from data it sends to APIToolkit servers, redacting means that those fields would never leave your servers at all. So you feel safer that your sensitive data only stays on your servers.

To mark fields that should be redacted, add them to your application's settings.
Eg:

```python
settings = {
"APITOOLKIT_KEY": "<YOUR_API_KEY>",
"APITOOLKIT_REDACT_HEADERS": ["Authorization", "Cookie","Content-Length", "Content-Type"],
"APITOOLKIT_REDACT_REQ_BODY": ["$.password", "$.credit_card"],
"APITOOLKIT_REDACT_RES_BODY": ["$.credentials", "$.social_security_number"]
}

```

It is important to note that while the `APITOOLKIT_REDACT_HEADERS` config field accepts a list of headers(case insensitive),
the `APITOOLKIT_REDACT_REQ_BODY` and `APITOOLKIT_REDACT_RES_BODY` expects a list of JSONPath strings as arguments.

The choice of JSONPath was selected to allow you have great flexibility in descibing which fields within your responses are sensitive.
Also note that these list of items to be redacted will be aplied to all endpoint requests and responses on your server.
To learn more about jsonpath to help form your queries, please take a look at this cheatsheet:
[https://lzone.de/cheat-sheet/JSONPath](https://lzone.de/cheat-sheet/JSONPath)

## Routes Whitelist

If you only want to capture specific app routes you can configure prefixes that need be matched.

```python
settings = {
"APITOOLKIT_ROUTES_WHITELIST": ["/api/first", "/api/second"],
}
```

This will only capture requests that are incoming to your app with these prefixes, e.a. `/api/first/customer/1` but not `/api/health`.

## Ignore HTTP status codes

You can exclude HTTP response status codes that you're not interested in or that are spamming your log.

```python
settings = {
"APITOOLKIT_IGNORE_HTTP_CODES": [404, 429],
}
```

## Debugging

You can add `APITOOLKIT_DEBUG` to your app's settings and set it to `True` to enable debug logging from the SDK. This will print out logs for each request/response captured by the tween. APITOOLKIT_DEBUG defaults to `False`.

# Outgoing Requests

To monitor outgoing HTTP requests from your Pyramid application, you can use the `observe_request` function from the `apitoolkit_pyramid` module. This allows you to capture and send copies of all outgoing requests to an apitoolkit server for monitoring and analysis. All outgoing request are associated with the incoming request that trigger them.

### Example

```python
from pyramid.response import Response
from pyramid.view import view_config
from apitoolkit_pyramid import observe_request

@view_config(route_name='home')
def home(request):
    resp = observe_request(request).get(
        "https://jsonplaceholder.typicode.com/todos/2")
    return Response(resp.read())
```

The `observe_request` function wraps an httpx client and you can use it just like you would normally use httpx for any request you need.

# Error Reporting

If you’ve used sentry, or bugsnag, or rollbar, then you’re already familiar with this usecase.
But you can report an error to apitoolkit. A difference, is that errors are always associated with a parent request, and helps you query and associate the errors which occured while serving a given customer request. Unhandled errors are reported automatically but you can also report errors to APIToolkit by using the `report_error` function from the `apitoolkit_pyramid` module to report an error you can report as many errors you want during a request

### Example

```python
from pyramid.response import Response
from pyramid.view import view_config
from apitoolkit_pyramid import observe_request, report_error


@view_config(route_name='home')
def home(request):
  try:
    resp = observe_request(request).get(
        "https://jsonplaceholder.typicode.com/todos/2")
    return Response(resp.read())
  except Exception as e:
    report_error(request, e)
    return Response("something went wrong")
```



            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "apitoolkit-pyramid",
    "maintainer": null,
    "docs_url": null,
    "requires_python": null,
    "maintainer_email": null,
    "keywords": null,
    "author": "APIToolkit",
    "author_email": "hello@apitoolkit.io",
    "download_url": "https://files.pythonhosted.org/packages/ea/1b/5881f727247314d980fd847eec296a9047609289c80b595cb0dbffe4accb/apitoolkit-pyramid-0.1.6.tar.gz",
    "platform": null,
    "description": "# API Toolkit Python Pyramid SDK\n\nThe API Toolkit Pyramid client is an sdk used to integrate Pyramid web applications with APIToolkit.\nIt monitors incoming traffic, gathers the requests and sends the request to the apitoolkit servers.\n\n## How to Integrate:\n\nFirst install the apitoolkit pyramid sdk:\n`pip install apitoolkit-pyramid`\n\nAdd your APIToolkit API key `APITOOLKIT_KEY` to your `development.ini` or `production.ini` files or in your settings:\n\n```python\nAPITOOLKIT_KEY = \"<YOUR_API_KEY>\"\n```\n\nWhen using ini files separate mulitple values with comma.\n\n```ini\nAPITOOLKIT_REDACT_HEADERS = X-Secret1,X-Secret2\n```\n\nThen add apitoolkit pyramid tween in your server's config:\n\n```python\nfrom wsgiref.simple_server import make_server\nfrom pyramid.config import Configurator\nfrom pyramid.response import Response\nfrom pyramid.view import view_config\n\n\n@view_config(\n    route_name='home'\n)\ndef home(request):\n    return Response('Welcome!')\n\nif __name__ == '__main__':\n    setting = {\"APITOOLKIT_KEY\": \"<YOUR_API_KEY>\"}\n    with Configurator(settings=setting) as config:\n        # add aptoolkit tween\n        config.add_tween(\"apitoolkit_pyramid.APIToolkit\")\n        config.add_route('home', '/')\n        config.scan()\n        app = config.make_wsgi_app()\n    server = make_server('0.0.0.0', 6543, app)\n    server.serve_forever()\n```\n\nThis will monitor all requests and send them to the APIToolkit's servers.\n\n## Configuration\n\nYou can add more configurations in your settings to customize behavior, such as redacting senstive fields, printing values to help with debugging and so on.\n\n### Configuration Parameters\n\n`APITOOLKIT_KEY`: `required` API key for accessing the APIToolkit service\n\n`APITOOLKIT_REDACT_HEADERS`: `optional` List of headers to redact in captured requests.\n\n`APITOOLKIT_DEBUG`: `optional` Flag to enable debug mode.\n\n`APITOOLKIT_REDACT_REQ_BODY`: `optional` List of fields to redact in request bodies.\n\n`APITOOLKIT_REDACT_RES_BODY`: `optional` List of fields to redact in response bodies.\n\n`APITOOLKIT_ROUTES_WHITELIST`: `optional` List of routes prefixes that should be captured.\n\n`APITOOLKIT_IGNORE_HTTP_CODES`: `optional` List of HTTP status codes that should NOT be captured.\n\n`APITOOLKIT_SERVICE_VERSION`: `optional` Version of the service (helps in monitoring different versions of your deployments).\n\n`APITOOLKIT_TAGS`: `optional` Tags associated with the service.\n\n## Client Redacting fields\n\nThe SDK provides a way for customers to redact senstive fields from data it sends to APIToolkit servers, redacting means that those fields would never leave your servers at all. So you feel safer that your sensitive data only stays on your servers.\n\nTo mark fields that should be redacted, add them to your application's settings.\nEg:\n\n```python\nsettings = {\n\"APITOOLKIT_KEY\": \"<YOUR_API_KEY>\",\n\"APITOOLKIT_REDACT_HEADERS\": [\"Authorization\", \"Cookie\",\"Content-Length\", \"Content-Type\"],\n\"APITOOLKIT_REDACT_REQ_BODY\": [\"$.password\", \"$.credit_card\"],\n\"APITOOLKIT_REDACT_RES_BODY\": [\"$.credentials\", \"$.social_security_number\"]\n}\n\n```\n\nIt is important to note that while the `APITOOLKIT_REDACT_HEADERS` config field accepts a list of headers(case insensitive),\nthe `APITOOLKIT_REDACT_REQ_BODY` and `APITOOLKIT_REDACT_RES_BODY` expects a list of JSONPath strings as arguments.\n\nThe choice of JSONPath was selected to allow you have great flexibility in descibing which fields within your responses are sensitive.\nAlso note that these list of items to be redacted will be aplied to all endpoint requests and responses on your server.\nTo learn more about jsonpath to help form your queries, please take a look at this cheatsheet:\n[https://lzone.de/cheat-sheet/JSONPath](https://lzone.de/cheat-sheet/JSONPath)\n\n## Routes Whitelist\n\nIf you only want to capture specific app routes you can configure prefixes that need be matched.\n\n```python\nsettings = {\n\"APITOOLKIT_ROUTES_WHITELIST\": [\"/api/first\", \"/api/second\"],\n}\n```\n\nThis will only capture requests that are incoming to your app with these prefixes, e.a. `/api/first/customer/1` but not `/api/health`.\n\n## Ignore HTTP status codes\n\nYou can exclude HTTP response status codes that you're not interested in or that are spamming your log.\n\n```python\nsettings = {\n\"APITOOLKIT_IGNORE_HTTP_CODES\": [404, 429],\n}\n```\n\n## Debugging\n\nYou can add `APITOOLKIT_DEBUG` to your app's settings and set it to `True` to enable debug logging from the SDK. This will print out logs for each request/response captured by the tween. APITOOLKIT_DEBUG defaults to `False`.\n\n# Outgoing Requests\n\nTo monitor outgoing HTTP requests from your Pyramid application, you can use the `observe_request` function from the `apitoolkit_pyramid` module. This allows you to capture and send copies of all outgoing requests to an apitoolkit server for monitoring and analysis. All outgoing request are associated with the incoming request that trigger them.\n\n### Example\n\n```python\nfrom pyramid.response import Response\nfrom pyramid.view import view_config\nfrom apitoolkit_pyramid import observe_request\n\n@view_config(route_name='home')\ndef home(request):\n    resp = observe_request(request).get(\n        \"https://jsonplaceholder.typicode.com/todos/2\")\n    return Response(resp.read())\n```\n\nThe `observe_request` function wraps an httpx client and you can use it just like you would normally use httpx for any request you need.\n\n# Error Reporting\n\nIf you\u2019ve used sentry, or bugsnag, or rollbar, then you\u2019re already familiar with this usecase.\nBut you can report an error to apitoolkit. A difference, is that errors are always associated with a parent request, and helps you query and associate the errors which occured while serving a given customer request. Unhandled errors are reported automatically but you can also report errors to APIToolkit by using the `report_error` function from the `apitoolkit_pyramid` module to report an error you can report as many errors you want during a request\n\n### Example\n\n```python\nfrom pyramid.response import Response\nfrom pyramid.view import view_config\nfrom apitoolkit_pyramid import observe_request, report_error\n\n\n@view_config(route_name='home')\ndef home(request):\n  try:\n    resp = observe_request(request).get(\n        \"https://jsonplaceholder.typicode.com/todos/2\")\n    return Response(resp.read())\n  except Exception as e:\n    report_error(request, e)\n    return Response(\"something went wrong\")\n```\n\n\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "A Python Pyramid SDK for Apitoolkit integration",
    "version": "0.1.6",
    "project_urls": null,
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "67691fa2daa1fd15ad2589b6a9cc15d5d0ce6e420d8437920b7dfba957bc5cda",
                "md5": "fff4347024d48274dc0733978f6316bf",
                "sha256": "906c1faf4b6a3530a6a34aec8a3599357acc623dc9c183f889bd1525a14d4ca1"
            },
            "downloads": -1,
            "filename": "apitoolkit_pyramid-0.1.6-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "fff4347024d48274dc0733978f6316bf",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 6923,
            "upload_time": "2024-06-11T15:33:54",
            "upload_time_iso_8601": "2024-06-11T15:33:54.137850Z",
            "url": "https://files.pythonhosted.org/packages/67/69/1fa2daa1fd15ad2589b6a9cc15d5d0ce6e420d8437920b7dfba957bc5cda/apitoolkit_pyramid-0.1.6-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "ea1b5881f727247314d980fd847eec296a9047609289c80b595cb0dbffe4accb",
                "md5": "996c8e1e10acc308f9142079c2b13e9e",
                "sha256": "4a541e0a7a82ab1430a06e2ef00fd70e4fb340004ac47a3ac161f89c747a44e2"
            },
            "downloads": -1,
            "filename": "apitoolkit-pyramid-0.1.6.tar.gz",
            "has_sig": false,
            "md5_digest": "996c8e1e10acc308f9142079c2b13e9e",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 6497,
            "upload_time": "2024-06-11T15:33:56",
            "upload_time_iso_8601": "2024-06-11T15:33:56.432224Z",
            "url": "https://files.pythonhosted.org/packages/ea/1b/5881f727247314d980fd847eec296a9047609289c80b595cb0dbffe4accb/apitoolkit-pyramid-0.1.6.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-06-11 15:33:56",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "lcname": "apitoolkit-pyramid"
}
        
Elapsed time: 0.46837s