|
| 1 | +# External API Functionality in `InputFilter` |
| 2 | + |
| 3 | +This documentation provides a comprehensive overview of the external API functionality available in the `InputFilter` class. It covers the configuration, core methods, and examples of usage for interacting with external APIs. |
| 4 | + |
| 5 | +--- |
| 6 | + |
| 7 | +## 1. Overview |
| 8 | + |
| 9 | +The `InputFilter` class includes a mechanism for fetching data from external APIs during the input validation process. |
| 10 | +This feature allows dynamic data retrieval based on user inputs, such as validating fields or fetching related data from an external service. |
| 11 | + |
| 12 | +Important to know, the external api functionality runs after all other filters and validators have been executed. |
| 13 | +This means that the data fetched from the external API will not be validated or filtered. |
| 14 | + |
| 15 | +--- |
| 16 | + |
| 17 | +## 2. Configuration |
| 18 | + |
| 19 | +The external API functionality is configured via the `external_api` parameter in the `add` method. This parameter accepts a dictionary with the following structure: |
| 20 | + |
| 21 | +### `ExternalApiConfig` Fields |
| 22 | + |
| 23 | +| Field | Type | Description | |
| 24 | +|------------|--------------------------|-----------------------------------------------------------------------------| |
| 25 | +| `url` | `str` | The URL of the external API, with optional placeholders in `{{}}` format. | |
| 26 | +| `method` | `str` | The HTTP method to use (e.g., `GET`, `POST`). | |
| 27 | +| `params` | `Optional[Dict[str, str]]` | Query parameters for the API, with placeholders allowed. | |
| 28 | +| `data_key` | `Optional[str]` | Key in the JSON response to extract the required data. | |
| 29 | +| `api_key` | `Optional[str]` | API key for authorization, sent in the `Authorization` header. | |
| 30 | + |
| 31 | +--- |
| 32 | + |
| 33 | +## 3. Examples |
| 34 | + |
| 35 | +### 3.1 Basic External API Integration |
| 36 | + |
| 37 | +```python |
| 38 | +from flask_inputfilter.InputFilter import InputFilter |
| 39 | + |
| 40 | +class MyInputFilter(InputFilter): |
| 41 | + def __init__(self): |
| 42 | + super().__init__() |
| 43 | + |
| 44 | + self.add( |
| 45 | + "user_id", required=True |
| 46 | + ) |
| 47 | + self.add( |
| 48 | + "is_active", |
| 49 | + required=True, |
| 50 | + external_api={ |
| 51 | + "url": "https://api.example.com/users/{{user_id}}/status", |
| 52 | + "method": "GET", |
| 53 | + "data_key": "is_active", |
| 54 | + }, |
| 55 | + ) |
| 56 | + |
| 57 | +# Example usage |
| 58 | +filter_instance = MyInputFilter() |
| 59 | +validated_data = filter_instance.validateData({"user_id": 123}) |
| 60 | +print(validated_data["is_active"]) # True or False based on API response |
| 61 | +``` |
| 62 | + |
| 63 | +### 3.2 Using Query Parameters |
| 64 | + |
| 65 | +```python |
| 66 | +self.add( |
| 67 | + "is_valid", |
| 68 | + required=True, |
| 69 | + external_api={ |
| 70 | + "url": "https://api.example.com/validate", |
| 71 | + "method": "GET", |
| 72 | + "params": {"user": "{{user_id}}", "hash": "{{hash}}"}, |
| 73 | + "data_key": "is_valid", |
| 74 | + }, |
| 75 | +) |
| 76 | +``` |
| 77 | + |
| 78 | +This configuration sends the `user_id` and `hash` as query parameters, replacing the placeholders with validated data. |
| 79 | + |
| 80 | +--- |
| 81 | + |
| 82 | +### 3.3 Handling Fallback Values |
| 83 | + |
| 84 | +If the external API call fails, a fallback value can be specified: |
| 85 | + |
| 86 | +```python |
| 87 | +self.add( |
| 88 | + "user_info", |
| 89 | + required=True, |
| 90 | + fallback={"name": "unknown", "age": 0}, |
| 91 | + external_api={ |
| 92 | + "url": "https://api.example.com/user/{{user_id}}", |
| 93 | + "method": "GET", |
| 94 | + "data_key": "user", |
| 95 | + }, |
| 96 | +) |
| 97 | +``` |
| 98 | + |
| 99 | +--- |
| 100 | + |
| 101 | +## 4. Error Handling |
| 102 | + |
| 103 | +- `ValidationError` is raised when: |
| 104 | + - The API call returns a non-200 status code. |
| 105 | + - A required field is missing and no fallback/default is provided. |
| 106 | + - Validation of the field value fails. |
| 107 | + |
| 108 | +--- |
| 109 | + |
| 110 | +## 7. Best Practices |
| 111 | + |
| 112 | +- **Required Fields:** Clearly define required fields and provide fallback values where necessary. |
| 113 | +- **Placeholders:** Ensure placeholders in URLs and parameters match the keys in `validated_data`. |
| 114 | +- **Fallbacks:** Always provide fallback values for critical fields to avoid disruptions in case of API failure. |
| 115 | +- **Security:** Use HTTPS for API calls and secure sensitive data like API keys. |
| 116 | +- **Testing:** Mock external API calls during unit testing to avoid dependencies on external systems. |
| 117 | + |
| 118 | +--- |
0 commit comments