mosparo-api-client


Namemosparo-api-client JSON
Version 1.1.1 PyPI version JSON
download
home_page
SummaryPython API Client to communicate with mosparo.
upload_time2024-01-18 18:29:28
maintainer
docs_urlNone
author
requires_python>=3.5
licenseThe MIT License (MIT) Copyright 2023-2024 mosparo Core Developers and contributors Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
keywords mosparo api-client spam-protection accessibility captcha
VCS
bugtrack_url
requirements certifi charset-normalizer idna requests urllib3
Travis-CI No Travis.
coveralls test coverage No coveralls.
             
<p align="center">
    <img src="https://github.com/mosparo/mosparo/blob/master/assets/images/mosparo-logo.svg?raw=true" alt="mosparo logo contains a bird with the name Mo and the mosparo text"/>
</p>

<h1 align="center">
    Python API Client
</h1>
<p align="center">
    This library offers the API client to communicate with mosparo to verify a submission.
</p>

-----

## 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:

```text
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
```commandline
python -m build
```
3. Install the package
```commandline
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
```html
<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>
```
3. Include the library in your project
```text
pip install mosparo-api-client
```
4. After the form is submitted, verify the data before processing it

```python
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.

```python
from mosparo_api_client import Client

api_client = Client(host, public_key, private_key, verify_ssl)
```

| 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. |

#### 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.

```python
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](#get_verified_fields-list-see-constants)).

See [Bypass protection](https://documentation.mosparo.io/docs/integration/bypass_protection) in the mosparo documentation.

##### Example

```python
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](#constants))

Returns an array with all verified field keys.

#### `get_verified_field(key)`: string (see [Constants](#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.

```python
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](https://opensource.org/licenses/MIT).
Please see the [LICENSE](LICENSE) file for the full license.

            

Raw data

            {
    "_id": null,
    "home_page": "",
    "name": "mosparo-api-client",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.5",
    "maintainer_email": "",
    "keywords": "mosparo,api-client,spam-protection,accessibility,captcha",
    "author": "",
    "author_email": "mosparo Core Developers <info@mosparo.io>",
    "download_url": "https://files.pythonhosted.org/packages/95/2e/52c8c1d0cbe8edd7aad28a5163f8f65df1c16d71b1094b28e7e8ef53507c/mosparo_api_client-1.1.1.tar.gz",
    "platform": null,
    "description": "&nbsp;\n<p align=\"center\">\n    <img src=\"https://github.com/mosparo/mosparo/blob/master/assets/images/mosparo-logo.svg?raw=true\" alt=\"mosparo logo contains a bird with the name Mo and the mosparo text\"/>\n</p>\n\n<h1 align=\"center\">\n    Python API Client\n</h1>\n<p align=\"center\">\n    This library offers the API client to communicate with mosparo to verify a submission.\n</p>\n\n-----\n\n## Description\nThis Python library lets you connect to a mosparo installation and verify the submitted data.\n\n## Installation\n\n### Install using pip\n\nInstall this library by using pip:\n\n```text\npip install mosparo-api-client\n```\n\n### Build from source\n\nYou need the module `build` to build the module from source.\n\n1. Clone the repository\n2. Build the package\n```commandline\npython -m build\n```\n3. Install the package\n```commandline\npip install dist/mosparo_api_client-1.0.0-py3-none-any.whl\n```\n\n## Usage\n1. Create a project in your mosparo installation\n2. Include the mosparo script in your form\n```html\n<div id=\"mosparo-box\"></div>\n\n<script src=\"https://[URL]/build/mosparo-frontend.js\" defer></script>\n<script>\n    var m;\n    window.onload = function(){\n        m = new mosparo('mosparo-box', 'https://[URL]', '[UUID]', '[PUBLIC_KEY]', {loadCssResource: true});\n    };\n</script>\n```\n3. Include the library in your project\n```text\npip install mosparo-api-client\n```\n4. After the form is submitted, verify the data before processing it\n\n```python\nfrom mosparo_api_client import Client\n\napi_client = Client(host, public_key, private_key)\n\nyour_post_data = {}  # This needs to be filled with the post data\n\nmosparo_submit_token = your_post_data['_mosparo_submitToken']\nmosparo_validation_token = your_post_data['_mosparo_validationToken']\n\nresult = api_client.verify_submission(your_post_data, mosparo_submit_token, mosparo_validation_token)\n\nif result.is_submittable():\n    # Send the email or process the data\n    pass\nelse:\n    # Show error message\n    pass\n```\n\n## API Documentation\n\n### Client\n\n#### Client initialization\n\nCreate a new client object to use the API client.\n\n```python\nfrom mosparo_api_client import Client\n\napi_client = Client(host, public_key, private_key, verify_ssl)\n```\n\n| Parameter   | Type | Description                                                 |\n|-------------|------|-------------------------------------------------------------|\n| host        | str  | The host of the mosparo installation                        |\n| public_key  | str  | The public key of the mosparo project                       |\n| private_key | str  | The private key of the mosparo project                      |\n| verify_ssl  | bool | Set to False if the SSL certificate should not be verified. |\n\n#### Verify form data\n\nTo 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.\n\n```python\nresult = api_client.verify_submission(form_data, mosparo_submit_token, mosparo_validation_token)\n```\n\n| Parameter                | Type  | Description                                                                                                  |\n|--------------------------|-------|--------------------------------------------------------------------------------------------------------------|\n| form_data                | dict  | The dictionary with all the submitted form data.                                                             |\n| mosparo_submit_token     | str   | The submit token which was generated by mosparo and submitted with the form data                             |\n| mosparo_validation_token | str   | The validation token which mosparo generated after the validation and which was submitted with the form data |\n\n#### Bypass protection\n\nAfter 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](#get_verified_fields-list-see-constants)).\n\nSee [Bypass protection](https://documentation.mosparo.io/docs/integration/bypass_protection) in the mosparo documentation.\n\n##### Example\n\n```python\nverified_fields = result.get_verified_fields()\nrequired_field_difference = set(list_of_required_field_names) - set(verified_fields.keys())\nverifiable_field_difference = set(list_of_verifiable_field_names) - set(verified_fields.keys())\n\n# The submission is only valid if all required and verifiable fields got verified\nif result.is_submittable() and not required_field_difference and not verifiable_field_difference:\n    print('All good, send email')\nelse:\n    raise Exception('mosparo did not verify all required fields. This submission looks like spam.')\n```\n\n`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. \n\n### VerificationResult\n\n#### Constants\n\n- `FIELD_NOT_VERIFIED`: 'not-verified'\n- `FIELD_VALID`: 'valid'\n- `FIELD_INVALID`: 'invalid'\n\n#### `is_submittable()`: bool\n\nReturns `True` if the form is submittable. This means that the verification was successful and the \nform data are valid.\n\n#### `is_valid()`: bool\n\nReturns `True` if mosparo determined the form as valid. The difference to `is_submittable()` is, that this\nis the original result from mosparo, while `is_submittable()` also checks if the verification was done correctly.\n\n#### `get_verified_fields()`: list (see [Constants](#constants))\n\nReturns an array with all verified field keys.\n\n#### `get_verified_field(key)`: string (see [Constants](#constants))\n\nReturns the verification status of one field.\n\n#### `has_issues()`: bool\n\nReturns `True` if there were verification issues.\n\n#### `get_issues()`: list\n\nReturns an array with all verification issues.\n\n#### Get the statistic data by date\n\nTo 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.\n\n```python\nresult = api_client.get_statistic_by_date(range, start_date)\n```\n\n| Parameter  | Type          | Description                                                                         |\n|------------|---------------|-------------------------------------------------------------------------------------|\n| range      | int           | Time range in seconds (will be rounded up to a full day since mosparo v1.1)         |\n| start_date | datetime.date | The start date from which the statistics are to be returned (requires mosparo v1.1) |\n\n### StatisticResult\n\n#### `get_number_of_valid_submissions()`: int\n\nReturn the number of valid submissions in the requested time range.\n\n#### `get_number_of_spam_submissions()`: int\n\nReturn the number of spam submissions in the requested time range.\n\n#### `get_numbers_by_date()`: dict\n\nReturn the numbers grouped by date.\n\n## License\n\nmosparo Python API Client is open-sourced software licensed under the [MIT License](https://opensource.org/licenses/MIT).\nPlease see the [LICENSE](LICENSE) file for the full license.\n",
    "bugtrack_url": null,
    "license": "The MIT License (MIT)  Copyright 2023-2024 mosparo Core Developers and contributors  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.  THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ",
    "summary": "Python API Client to communicate with mosparo.",
    "version": "1.1.1",
    "project_urls": {
        "GitHub": "https://github.com/mosparo/python-api-client",
        "Website": "https://mosparo.io"
    },
    "split_keywords": [
        "mosparo",
        "api-client",
        "spam-protection",
        "accessibility",
        "captcha"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "1d879c2118912bf2d77fe837489e87afe86d8e107bcd73fc76699191f13bddde",
                "md5": "4befb5e9d397a9c68c310b203064c982",
                "sha256": "576c8be6c33f27155649b90084041c5036ad794f6b86192f82d674a2d784a592"
            },
            "downloads": -1,
            "filename": "mosparo_api_client-1.1.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "4befb5e9d397a9c68c310b203064c982",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.5",
            "size": 10631,
            "upload_time": "2024-01-18T18:29:26",
            "upload_time_iso_8601": "2024-01-18T18:29:26.224942Z",
            "url": "https://files.pythonhosted.org/packages/1d/87/9c2118912bf2d77fe837489e87afe86d8e107bcd73fc76699191f13bddde/mosparo_api_client-1.1.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "952e52c8c1d0cbe8edd7aad28a5163f8f65df1c16d71b1094b28e7e8ef53507c",
                "md5": "9ac23fedf2222fdc91b60aa14e319f91",
                "sha256": "ff8c8ae4b47e95f9441adc664fedadfa0bde2d9cf5dcaddfc1a7d538732c98ef"
            },
            "downloads": -1,
            "filename": "mosparo_api_client-1.1.1.tar.gz",
            "has_sig": false,
            "md5_digest": "9ac23fedf2222fdc91b60aa14e319f91",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.5",
            "size": 13637,
            "upload_time": "2024-01-18T18:29:28",
            "upload_time_iso_8601": "2024-01-18T18:29:28.715057Z",
            "url": "https://files.pythonhosted.org/packages/95/2e/52c8c1d0cbe8edd7aad28a5163f8f65df1c16d71b1094b28e7e8ef53507c/mosparo_api_client-1.1.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-01-18 18:29:28",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "mosparo",
    "github_project": "python-api-client",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "requirements": [
        {
            "name": "certifi",
            "specs": [
                [
                    "==",
                    "2023.7.22"
                ]
            ]
        },
        {
            "name": "charset-normalizer",
            "specs": [
                [
                    "==",
                    "3.1.0"
                ]
            ]
        },
        {
            "name": "idna",
            "specs": [
                [
                    "==",
                    "3.4"
                ]
            ]
        },
        {
            "name": "requests",
            "specs": [
                [
                    "==",
                    "2.31.0"
                ]
            ]
        },
        {
            "name": "urllib3",
            "specs": [
                [
                    "==",
                    "1.26.18"
                ]
            ]
        }
    ],
    "lcname": "mosparo-api-client"
}
        
Elapsed time: 0.17444s