|
| 1 | +API Parameters |
| 2 | +=================== |
| 3 | + |
| 4 | +dequest provides `PathParameter`, `QueryParameter`, `FormParameter` and `JsonBody` to handle API parameters. |
| 5 | + |
| 6 | +Path Parameters |
| 7 | +--------------- |
| 8 | + |
| 9 | +Use `PathParameter` to include values in the URL: |
| 10 | + |
| 11 | +.. code-block:: python |
| 12 | +
|
| 13 | + from dequest import sync_client, PathParameter |
| 14 | +
|
| 15 | + @sync_client(url="https://api.example.com/users/{user_id}") |
| 16 | + def get_user(user_id: PathParameter[int]): |
| 17 | + pass |
| 18 | +
|
| 19 | + user = get_user(user_id=42) |
| 20 | + print(user) |
| 21 | +
|
| 22 | +Query Parameters |
| 23 | +---------------- |
| 24 | + |
| 25 | +Use `QueryParameter` to pass values as query parameters: |
| 26 | + |
| 27 | +.. code-block:: python |
| 28 | +
|
| 29 | + from dequest import sync_client, QueryParameter |
| 30 | +
|
| 31 | + @sync_client(url="https://api.example.com/search") |
| 32 | + def search(keyword: QueryParameter[str, "q"]): |
| 33 | + pass |
| 34 | +
|
| 35 | + results = search(keyword="python") |
| 36 | + print(results) |
| 37 | +
|
| 38 | +
|
| 39 | +In endpoint function definitions, `QueryParameter`, `PathParameter`, `FormParameter`, and `JsonBody` parameters support two optional arguments: |
| 40 | + |
| 41 | +1. **Type Hint (First Argument)** |
| 42 | + |
| 43 | + You can provide a type hint (e.g., `str`, `int`, `bool`, etc.) as the first argument. This enables automatic type checking and validation before making the API call. |
| 44 | + |
| 45 | + **Example:** |
| 46 | + |
| 47 | +.. code-block:: python |
| 48 | +
|
| 49 | + keyword: QueryParameter[str] |
| 50 | +
|
| 51 | +This ensures that `keyword` must be a string when passed to the function. |
| 52 | + |
| 53 | +2. **Mapping Name (Second Argument)** |
| 54 | + The second argument is an optional string that lets you map the Python parameter name to the actual API parameter name. |
| 55 | + |
| 56 | + **Example:** |
| 57 | + |
| 58 | +.. code-block:: python |
| 59 | +
|
| 60 | + keyword: QueryParameter[str, "q"] |
| 61 | +
|
| 62 | +This maps the `keyword` parameter in your function to the `q` query parameter in the API request. |
| 63 | + |
| 64 | +These features help keep your code type-safe and aligned with external API schemas. Note that each of these arguments can be used alone. |
0 commit comments