Skip to main content

Alters and forwards incoming API requests

Project description

unit-tests e2e-tests

rest-api-proxy

Another DRF API proxy to redirect incoming requests to another API server, but with the option of altering the request and the response.

The original use case that motivated this implementation is to have a node responsible for the encryption/decryption of payloads, so that cryptographic keys are kept separated from the stored backend data. An example implementation of this use case can be found in the examples folder.

General functionality

ProxyBase is a view (inherits DRF's APIView) that allow altering incoming requests headers, data, and files via inheritance. The base implementation always creates a new request to be sent to the target API with the same data and files. The headers are specially treated: only the headers explicitly defined in the library configuration are forwarded to the target API.

The configuration can be made via settings.py or via the view constructor.

# settings.py
REST_API_PROXY = {
    'HOST': 'http://api.target.com',
    'FORWARD_HEADERS': ['Authorization'] #  only Authorization will be forwarded
}

# via constructor
ProxyBase.as_view(proxy_settings={'HOST': '...', 'FORWARD_HEADERS': [...]})

To alter headers, data and files, you can do this:

class AlterAll(ProxyBase):
    def process_headers(self, request, headers):
        return your_custom_headers

    def process_data(self, request, data):
        return your_custom_data

    def process_files(self, request, files):
        return your_custom_files

To alter the response received from the target API, you can implement process_response.

    def process_response(self, target_api_response):
        # your magic here
        return your_custom_response

Please refer to the examples folder for more practical details.

TODO

  • Add extra options to inject authentication data

Development

To install dependencies run:

uv sync --dev

Running tests

# unit tests
python runtests.py

# E2E tests
cd examples
docker compose build
docker compose up cipher -d
docker compose run tests-e2e sh test_cipher_e2e.sh

License

rest-api-proxy is offered under the BSD-2-Clause license.

Credits

This work is inspired by django-api-proxy (Simplified BSD License).

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

rest_api_proxy-0.1.2.tar.gz (56.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

rest_api_proxy-0.1.2-py3-none-any.whl (6.4 kB view details)

Uploaded Python 3

File details

Details for the file rest_api_proxy-0.1.2.tar.gz.

File metadata

  • Download URL: rest_api_proxy-0.1.2.tar.gz
  • Upload date:
  • Size: 56.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.9.8

File hashes

Hashes for rest_api_proxy-0.1.2.tar.gz
Algorithm Hash digest
SHA256 9ad84c6d36510adbd8bf2b00f06b6421bb420d9dc2770f58af72b0355e50b2ca
MD5 ca4c20280aed5135514b11c30ce8be80
BLAKE2b-256 4969540322810fbc4967d37f69f41f376e1312b328bd7f35d3e66aa65c8f1ec4

See more details on using hashes here.

File details

Details for the file rest_api_proxy-0.1.2-py3-none-any.whl.

File metadata

File hashes

Hashes for rest_api_proxy-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 86fd0640dfdbf6c75c3054ff9963cedc0462ffde8751a5c2f4e84d33033ba0a6
MD5 7c8ac1d79d514e0ec1e75db5e7cf12a2
BLAKE2b-256 d3043fe5460e738a1db9bab7147535431a5aadc0ee5d044526ead6892c7e618e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page