Skip to main content

RequestMap

RequestMap is a Python3 microframework designed with compatibility and portability in mind. It utilises a plugin-based system to allow your application to easily integrate with other frameworks such as Flask without the need of changing the code. Website.

Installing RequestMap

RequestMap is not published on pip yet as it's currently under development. With that in mind, you can add it to your project by following the instructions below.

In your project folder, cloning the repository

git clone https://github.com/yyjlincoln/RequestMap

Then, install the dependencies

python3 -m pip install -r requirements.txt

Finally, add requestmap as a submodule

git submodule add https://github.com/yyjlincoln/RequestMap RequestMap

Learn more about submodules

You might wish to learn more about git submodules here.

The Concepts of RequestMap

RequestMap uses a plugin-based system. It consists of three main components:

  • Protocol
  • ResponseHandler
  • Validator

Protocol

A Protocol is the part of the plugin that handles incoming requests and pass it down to RequestMap's internal handlers. An instance of RequestMap (aka EndpointMap) can have multiple protocols registered. A protocol must inherit from RequestMap.Protocols.ProtocolBase.StandardProtocolHandler.

You can use a Protocol through:

from RequestMap import Map
# Initialise an instance of RequestMap (aka EndpointMap)
API = Map()

# Initialise the protocol instance
SomeProtocolInstance = SomeProtocol(someConfiguration=1, anotherConfiguration=True)

# Use the protocol
API.useProtocol(SomeProtocolInstance)

For more on Protocol, check out RequestMap.Protocols.ProtocolBase.StandardProtocolHandler.

ResponseHandler

A ResponseHandler is an optional function that standardises the response from your view function. An instance of RequestMap (aka EndpointMap) can only have ONE ResponseHandler. It must inherit from RequestMap.Response.ResponseBase.StandardResponseHandler.

The ResponseHandler can be obtained in a view function by specifying a makeResponse keyword-only argument. For example:

@API.endpoint('addition', {
    'authrequired': False,
    'httproute': '/addition',
    'httpmethods': ['GET']
}, a=float, b=float)
def addition(a, b, makeResponse):
    return makeResponse(code=0, message="succeeded", result=a+b)

Validator

A Validator validates the incoming request. This can be useful for authentication purposes (for example, by validating userId and token and rejecting the request by throwing RequestMap.Exceptions.ValidationError if the credentials are invalid). It must inherit from RequestMap.Validators.ValidatorBase.StandardValidator.

For more on Validator, check out RequestMap.Validators.ValidatorBase.StandardValidator

Using RequestMap

Setting up an endpoint

You can set up an endpoint using the decorator, RequestMap.EndpointMap.Map().endpoint(). For simplicity, we'll call RequestMap.EndpointMap.Map(), which is an instance, "API (initialised above)".

@API.endpoint(<endpointIdentifier>, metadata = {}, **TypeConversionFunctions) [Decorator]

  • endpointIdentifier: The identifier of the endpoint. It must be unique.

  • metadata: A dictionary of metadata about the endpoint. This is available to Validators so you can configure the Validator on a per-route basis.

  • **TypeConversionFunctions: Keyword arguments that specify the type conversion functions for the data of the endpoint. It follows a format of <dataName>=<callable>, for example, aNumber=float. If dataName does not exist in the data then the conversion function will not be called; otherwise it will be called and the data of that key will be replaced by the return value of the type conversion function.

def theViewFunction(<nonOptionalArgs>, <optionalArgsWithDefaultValue> = <defaultValue>):

Following the decorator, the view function can specify which data is required and which are optional. RequestMap will automatically retrieve the values from the request, convert it using the type conversion functions, and pass it to the view function. If the data does not exist and it's nonOptional, then an Exceptions.MissingParameter exception will be raised which can be captured by the responseHandler function.

Lifecycle & Internal Logic

Alternatively, you can view the PNG version of this flowchart here

Example

from utils.RequestMap.Protocols.Flask import HTTPViaFlask
from utils.RequestMap.Response.JSON import JSONStandardizer

API.useProtocol(FlaskProtocol(port=5000, ALLOW_DEV_SERVER=True)) # Launches the dev server. For production, use FlaskProtocol().app with programs such as Gunicorn. 
API.useResponseHandler(JSONStandardizer({
    0: "succeeded",
    -1: "failed",
    -2: "unauthorised",
})

@API.endpoint('addition', {
    'authrequired': False,
    'httproute': '/addition',
    'httpmethods': ['GET']
}, a=float, b=float)
def addition(a, b, makeResponse):
    return makeResponse(code=0, result=a+b)

See it in action

The RequestMap framework is used by a few projects. Check them out here:

License

RequestMap is licensed under the Apache License 2.0 .

Copyright @yyjlincoln

Metadata

Release files for RequestMap 1.0.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for RequestMap 1.0.3
File Size Uploaded
RequestMap-1.0.3.tar.gz 15.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for RequestMap 1.0.3
File Interpreter ABI Platform
RequestMap-1.0.3-py3-none-any.whl Python 3 none any Details

Total release size: 32.1 kB

Release files / RequestMap-1.0.3.tar.gz

Download URL RequestMap-1.0.3.tar.gz
Size 15.7 kB
Tags Source
SHA-256 checksum
How to use checksums
2f7a2c89846133c9eee5dd8b3e1eac5fb9f8b64c50d837347b448f29662db8f4
BLAKE2b-256 checksum
How to use checksums
d392b9d43607626831de5ceee3cf69caa2048bbc84ec42e670feb6a61daa0130
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.0

Release files / RequestMap-1.0.3-py3-none-any.whl

Download URL RequestMap-1.0.3-py3-none-any.whl
Size 16.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e9ecc1544584c701929e6ac986fac1797a0346fabb725e4e279328f362d23ca0
BLAKE2b-256 checksum
How to use checksums
e06606ee2e448258b25567f7d3acb41c7abc5ad3aa4a70c72d9666c6dde76b54
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.0

Release history Release notifications | RSS feed

This release

1.0.3 This release

2 release files

1.0.2

2 release files

1.0.1

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page