app.views_api๏ƒ

API views and routes for the Mail List Shield application.

This module defines the REST API endpoints for email validation, credit balance retrieval, and API key testing.

Attributes๏ƒ

api_bp

Functions๏ƒ

invalid_api_key_response()

Abort the request with a 403 error for invalid API keys.

no_api_key_response()

Abort the request with a 401 error for missing API keys.

no_json_body_response()

Abort the request with a 415 Unsupported Media Type error for missing JSON body.

missing_key_in_json_response(key_name)

Abort the request with a 400 Bad Request error for missing keys in JSON body.

insufficient_credit_response()

Abort the request with a 402 Payment Required error for insufficient credits.

validate_request_json(request)

Validate that the request has JSON content type and a JSON body.

get_user_from_api_key(request)

Fetch the user associated with the provided API key.

successful_validation_response(result)

Return a standardized successful validation response.

api_test()

A test API endpoint that requires an API key.

get_credit_balance()

The API endpoint to get the user's credit balance.

validate_single()

The API endpoint to validate a single email address.

Module Contents๏ƒ

app.views_api.api_bp[source]๏ƒ
app.views_api.invalid_api_key_response()[source]๏ƒ

Abort the request with a 403 error for invalid API keys.

We standardize the error response to avoid leaking information about why the API key is invalid.

app.views_api.no_api_key_response()[source]๏ƒ

Abort the request with a 401 error for missing API keys.

app.views_api.no_json_body_response()[source]๏ƒ

Abort the request with a 415 Unsupported Media Type error for missing JSON body.

app.views_api.missing_key_in_json_response(key_name)[source]๏ƒ

Abort the request with a 400 Bad Request error for missing keys in JSON body.

Parameters:

key_name โ€“ The name of the missing key in the JSON payload.

app.views_api.insufficient_credit_response()[source]๏ƒ

Abort the request with a 402 Payment Required error for insufficient credits.

app.views_api.validate_request_json(request)[source]๏ƒ

Validate that the request has JSON content type and a JSON body.

Parameters:

request โ€“ The Flask request object.

Note

Aborts the request with appropriate error responses if validation fails.

app.views_api.get_user_from_api_key(request)[source]๏ƒ

Fetch the user associated with the provided API key.

Parameters:

request โ€“ The Flask request object containing the x-api-key header.

Returns:

The user object associated with the API key.

Return type:

Users

Note

Aborts with appropriate error responses if the API key is missing, invalid, or not associated with a user.

app.views_api.successful_validation_response(result)[source]๏ƒ

Return a standardized successful validation response.

Parameters:

result โ€“ The validation result dictionary.

Returns:

JSON response with validation results. If single_level_response

is requested, returns just the result dict. Otherwise, wraps it with status and message.

Return type:

Response

app.views_api.api_test()[source]๏ƒ

A test API endpoint that requires an API key.

This endpoint forgives requests without their content-type set to application/json, because it doesnโ€™t read your request body.

Returns:

For GET requests, returns a 405 error message.

For POST requests, returns a success message with the userโ€™s name.

Return type:

Response

app.views_api.get_credit_balance()[source]๏ƒ

The API endpoint to get the userโ€™s credit balance.

This endpoint forgives requests without their content-type set to application/json, because it doesnโ€™t read your request body.

Returns:

JSON response containing status, message, and credit balance.

Return type:

dict

app.views_api.validate_single()[source]๏ƒ

The API endpoint to validate a single email address.

This endpoint requires the request content-type to be application/json and a JSON body with an โ€œemailโ€ key.

Optionally, the request JSON can include a boolean key โ€œsingle_level_responseโ€. If set to true, the API will return the validation result in a single-level dictionary. Otherwise, and by default, the response includes status and message keys.

Returns:

JSON response with validation results or error message.
  • 200: Successful validation with result.

  • 400: Missing email key in request.

  • 402: Insufficient credits.

  • 500: Internal server error.

  • 503: Validation service unavailable.

Return type:

Response