Skip to main content

 

mosparo logo contains a bird with the name Mo and the mosparo text

Python API Client

This library offers the API client to communicate with mosparo to verify a submission.


Description

This Python library lets you connect to a mosparo installation and verify the submitted data.

Installation

Install using pip

Install this library by using pip:

pip install mosparo-api-client

Build from source

You need the module build to build the module from source.

  1. Clone the repository
  2. Build the package
python -m build
  1. Install the package
pip install dist/mosparo_api_client-1.0.0-py3-none-any.whl

Usage

  1. Create a project in your mosparo installation
  2. Include the mosparo script in your form
<div id="mosparo-box"></div>

<script src="https://[URL]/build/mosparo-frontend.js" defer></script>
<script>
    var m;
    window.onload = function(){
        m = new mosparo('mosparo-box', 'https://[URL]', '[UUID]', '[PUBLIC_KEY]', {loadCssResource: true});
    };
</script>
  1. Include the library in your project
pip install mosparo-api-client
  1. After the form is submitted, verify the data before processing it
from mosparo_api_client import Client

api_client = Client(host, public_key, private_key)

your_post_data = {}  # This needs to be filled with the post data

mosparo_submit_token = your_post_data["_mosparo_submitToken"]
mosparo_validation_token = your_post_data["_mosparo_validationToken"]

result = api_client.verify_submission(
    your_post_data, mosparo_submit_token, mosparo_validation_token
)

if result.is_submittable():
    # Send the email or process the data
    pass
else:
    # Show error message
    pass

API Documentation

Client

Client initialization

Create a new client object to use the API client.

from mosparo_api_client import Client

api_client = Client(host, public_key, private_key, verify_ssl, timeout)
Parameter Type Description
host str The host of the mosparo installation
public_key str The public key of the mosparo project
private_key str The private key of the mosparo project
verify_ssl bool Set to False if the SSL certificate should not be verified.
timeout float, tuple or None The timeout in seconds for the requests to mosparo, as a number or a (connect, read) tuple. Default: 10. None waits forever.

Verify form data

To verify the form data, call verify_submission with the form data in an array and the submit and validation tokens, which mosparo generated on the form initialization and the form data validation. The method will return a VerificationResult object.

result = api_client.verify_submission(
    form_data, mosparo_submit_token, mosparo_validation_token
)
Parameter Type Description
form_data dict The dictionary with all the submitted form data.
mosparo_submit_token str The submit token which was generated by mosparo and submitted with the form data
mosparo_validation_token str The validation token which mosparo generated after the validation and which was submitted with the form data

Bypass protection

After the verification of the submission by mosparo, you have to verify that all required fields and all possible fields were verified correctly. For this you have to check that all your required fields are set in the result (get_verified_fields).

See Bypass protection in the mosparo documentation.

Example
verified_fields = result.get_verified_fields()
required_field_difference = set(list_of_required_field_names) - set(
    verified_fields.keys()
)
verifiable_field_difference = set(list_of_verifiable_field_names) - set(
    verified_fields.keys()
)

# The submission is only valid if all required and verifiable fields got verified
if (
    result.is_submittable()
    and not required_field_difference
    and not verifiable_field_difference
):
    print("All good, send email")
else:
    raise Exception(
        "mosparo did not verify all required fields. This submission looks like spam."
    )

list_of_required_field_names and list_of_verifiable_field_names contain the names of all required or verifiable fields. Verifiable fields are fields that mosparo can validate and verify (for example, text fields). A checkbox field, for example, will not be validated and verified by mosparo.

VerificationResult

Constants

  • FIELD_NOT_VERIFIED: "not-verified"
  • FIELD_VALID: "valid"
  • FIELD_INVALID: "invalid"

is_submittable(): bool

Returns True if the form is submittable. This means that the verification was successful and the form data are valid.

is_valid(): bool

Returns True if mosparo determined the form as valid. The difference to is_submittable() is, that this is the original result from mosparo, while is_submittable() also checks if the verification was done correctly.

get_verified_fields(): list (see Constants)

Returns an array with all verified field keys.

get_verified_field(key): string (see Constants)

Returns the verification status of one field.

has_issues(): bool

Returns True if there were verification issues.

get_issues(): list

Returns an array with all verification issues.

Get the statistic data by date

To get the statistic data grouped by date, call get_statistic_by_date. The method accepts a time range in seconds for which the data should be returned (last x seconds) or the start date from which the data should be returned. The method will return a StatisticResult object.

result = api_client.get_statistic_by_date(range, start_date)
Parameter Type Description
range int Time range in seconds (will be rounded up to a full day since mosparo v1.1)
start_date datetime.date The start date from which the statistics are to be returned (requires mosparo v1.1)

StatisticResult

get_number_of_valid_submissions(): int

Return the number of valid submissions in the requested time range.

get_number_of_spam_submissions(): int

Return the number of spam submissions in the requested time range.

get_numbers_by_date(): dict

Return the numbers grouped by date.

License

mosparo Python API Client is open-sourced software licensed under the MIT License. Please see the LICENSE file for the full license.

Metadata

Release files for mosparo-api-client 1.2.0

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

Source distribution (sdist)

Source distribution for mosparo-api-client 1.2.0
File Size Uploaded Provenance Actions
mosparo_api_client-1.2.0.tar.gz 13.8 kB Without provenance Details for mosparo_api_client-1.2.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for mosparo-api-client 1.2.0
File Interpreter ABI Platform Actions
mosparo_api_client-1.2.0-py3-none-any.whl Python 3 none any Details for mosparo_api_client-1.2.0-py3-none-any.whl

Total release size: 24.1 kB

Release files / mosparo_api_client-1.2.0.tar.gz

Download URL mosparo_api_client-1.2.0.tar.gz
Size 13.8 kB
Tags Source
SHA-256 checksum
How to use checksums
a65cc4daeba7dae8ff8063bca759a6bbbd5537c77abc26bf4a8cc5d428a99850
BLAKE2b-256 checksum
How to use checksums
fe86d985215a5b8a49e15639b7b504d4df3cd19a71eb1f369b5dde44e345ea2a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.11.3

Release files / mosparo_api_client-1.2.0-py3-none-any.whl

Download URL mosparo_api_client-1.2.0-py3-none-any.whl
Size 10.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d70420b441b425933a4fbb14e491ce742037af6ef4688b848b232a6f91b071f7
BLAKE2b-256 checksum
How to use checksums
551af31bc31f77d86fedbd25a1fee6fd2c5255e742865e4787b170d0d4f0278d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.11.3

Security for mosparo-api-client 1.2.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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