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๏
Functions๏
Abort the request with a 403 error for invalid API keys. |
|
Abort the request with a 401 error for missing API keys. |
|
Abort the request with a 415 Unsupported Media Type error for missing JSON body. |
|
|
Abort the request with a 400 Bad Request error for missing keys in JSON body. |
Abort the request with a 402 Payment Required error for insufficient credits. |
|
|
Validate that the request has JSON content type and a JSON body. |
|
Fetch the user associated with the provided API key. |
|
Return a standardized successful validation response. |
|
A test API endpoint that requires an API key. |
The API endpoint to get the user's credit balance. |
|
The API endpoint to validate a single email address. |
Module Contents๏
- 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:
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