REST
CLASS | DESCRIPTION |
---|---|
ALBResolver |
Amazon Application Load Balancer (ALB) resolver |
APIGatewayHttpResolver |
Amazon API Gateway HTTP API v2 payload resolver |
APIGatewayRestResolver |
Amazon API Gateway REST and HTTP API v1 payload resolver |
ApiGatewayResolver |
API Gateway, VPC Laticce, Bedrock and ALB proxy resolver |
BaseRouter |
Base class for Routing |
BedrockResponse |
Contains the response body, status code, content type, and optional attributes |
CORSConfig |
CORS Config |
MiddlewareFrame |
Creates a Middle Stack Wrapper instance to be used as a "Frame" in the overall stack of |
ProxyEventType |
An enumerations of the supported proxy event types. |
Response |
Response data class that provides greater control over what is returned from the proxy event |
ResponseBuilder |
Internally used Response builder |
Route |
Internally used Route Configuration |
Router |
Router helper class to allow splitting ApiGatewayResolver into multiple files |
ALBResolver ¶
ALBResolver(
cors: CORSConfig | None = None,
debug: bool | None = None,
serializer: Callable[[dict], str] | None = None,
strip_prefixes: list[str | Pattern] | None = None,
enable_validation: bool = False,
response_validation_error_http_code: HTTPStatus
| int
| None = None,
)
Bases: ApiGatewayResolver
Amazon Application Load Balancer (ALB) resolver
Source code in aws_lambda_powertools/event_handler/api_gateway.py
2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 |
|
APIGatewayHttpResolver ¶
APIGatewayHttpResolver(
cors: CORSConfig | None = None,
debug: bool | None = None,
serializer: Callable[[dict], str] | None = None,
strip_prefixes: list[str | Pattern] | None = None,
enable_validation: bool = False,
response_validation_error_http_code: HTTPStatus
| int
| None = None,
)
Bases: ApiGatewayResolver
Amazon API Gateway HTTP API v2 payload resolver
Source code in aws_lambda_powertools/event_handler/api_gateway.py
2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 |
|
APIGatewayRestResolver ¶
APIGatewayRestResolver(
cors: CORSConfig | None = None,
debug: bool | None = None,
serializer: Callable[[dict], str] | None = None,
strip_prefixes: list[str | Pattern] | None = None,
enable_validation: bool = False,
response_validation_error_http_code: HTTPStatus
| int
| None = None,
)
Bases: ApiGatewayResolver
Amazon API Gateway REST and HTTP API v1 payload resolver
Source code in aws_lambda_powertools/event_handler/api_gateway.py
2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 |
|
ApiGatewayResolver ¶
ApiGatewayResolver(
proxy_type: Enum = ProxyEventType.APIGatewayProxyEvent,
cors: CORSConfig | None = None,
debug: bool | None = None,
serializer: Callable[[dict], str] | None = None,
strip_prefixes: list[str | Pattern] | None = None,
enable_validation: bool = False,
response_validation_error_http_code: HTTPStatus
| int
| None = None,
)
Bases: BaseRouter
API Gateway, VPC Laticce, Bedrock and ALB proxy resolver
Examples:
Simple example with a custom lambda handler using the Tracer capture_lambda_handler decorator
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
response_validation_error_http_code Sets the returned status code if response is not validated. enable_validation must be True.
METHOD | DESCRIPTION |
---|---|
configure_openapi |
Configure OpenAPI specification settings for the API. |
enable_swagger |
Returns the OpenAPI schema as a JSON serializable dict |
get_openapi_json_schema |
Returns the OpenAPI schema as a JSON serializable dict |
get_openapi_schema |
Returns the OpenAPI schema as a pydantic model. |
include_router |
Adds all routes and context defined in a router |
resolve |
Resolves the response based on the provide event and decorator routes |
route |
Route decorator includes parameter |
Source code in aws_lambda_powertools/event_handler/api_gateway.py
1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 |
|
configure_openapi ¶
configure_openapi(
title: str = DEFAULT_OPENAPI_TITLE,
version: str = DEFAULT_API_VERSION,
openapi_version: str = DEFAULT_OPENAPI_VERSION,
summary: str | None = None,
description: str | None = None,
tags: list[Tag | str] | None = None,
servers: list[Server] | None = None,
terms_of_service: str | None = None,
contact: Contact | None = None,
license_info: License | None = None,
security_schemes: dict[str, SecurityScheme]
| None = None,
security: list[dict[str, list[str]]] | None = None,
openapi_extensions: dict[str, Any] | None = None,
)
Configure OpenAPI specification settings for the API.
Sets up the OpenAPI documentation configuration that can be later used when enabling Swagger UI or generating OpenAPI specifications.
PARAMETER | DESCRIPTION |
---|---|
title
|
The title of the application.
TYPE:
|
version
|
The version of the OpenAPI document (which is distinct from the OpenAPI Specification version or the API
TYPE:
|
openapi_version
|
The version of the OpenAPI Specification (which the document uses).
TYPE:
|
summary
|
A short summary of what the application does.
TYPE:
|
description
|
A verbose explanation of the application behavior.
TYPE:
|
tags
|
A list of tags used by the specification with additional metadata. |
servers
|
An array of Server Objects, which provide connectivity information to a target server.
TYPE:
|
terms_of_service
|
A URL to the Terms of Service for the API. MUST be in the format of a URL.
TYPE:
|
contact
|
The contact information for the exposed API.
TYPE:
|
license_info
|
The license information for the exposed API.
TYPE:
|
security_schemes
|
A declaration of the security schemes available to be used in the specification. |
security
|
A declaration of which security mechanisms are applied globally across the API. |
openapi_extensions
|
Additional OpenAPI extensions as a dictionary. |
Example
api.configure_openapi( ... title="My API", ... version="1.0.0", ... description="API for managing resources", ... contact=Contact( ... name="API Support", ... email="support@example.com" ... ) ... )
See Also
enable_swagger : Method to enable Swagger UI using these configurations OpenAPIConfig : Data class containing all OpenAPI configuration options
Source code in aws_lambda_powertools/event_handler/api_gateway.py
1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 |
|
enable_swagger ¶
enable_swagger(
*,
path: str = "/swagger",
title: str = DEFAULT_OPENAPI_TITLE,
version: str = DEFAULT_API_VERSION,
openapi_version: str = DEFAULT_OPENAPI_VERSION,
summary: str | None = None,
description: str | None = None,
tags: list[Tag | str] | None = None,
servers: list[Server] | None = None,
terms_of_service: str | None = None,
contact: Contact | None = None,
license_info: License | None = None,
swagger_base_url: str | None = None,
middlewares: list[Callable[..., Response]]
| None = None,
compress: bool = False,
security_schemes: dict[str, SecurityScheme]
| None = None,
security: list[dict[str, list[str]]] | None = None,
oauth2_config: OAuth2Config | None = None,
persist_authorization: bool = False,
openapi_extensions: dict[str, Any] | None = None,
)
Returns the OpenAPI schema as a JSON serializable dict
PARAMETER | DESCRIPTION |
---|---|
path
|
The path to the swagger UI.
TYPE:
|
title
|
The title of the application.
TYPE:
|
version
|
The version of the OpenAPI document (which is distinct from the OpenAPI Specification version or the API
TYPE:
|
openapi_version
|
The version of the OpenAPI Specification (which the document uses).
TYPE:
|
summary
|
A short summary of what the application does.
TYPE:
|
description
|
A verbose explanation of the application behavior.
TYPE:
|
tags
|
A list of tags used by the specification with additional metadata. |
servers
|
An array of Server Objects, which provide connectivity information to a target server.
TYPE:
|
terms_of_service
|
A URL to the Terms of Service for the API. MUST be in the format of a URL.
TYPE:
|
contact
|
The contact information for the exposed API.
TYPE:
|
license_info
|
The license information for the exposed API.
TYPE:
|
swagger_base_url
|
The base url for the swagger UI. If not provided, we will serve a recent version of the Swagger UI.
TYPE:
|
middlewares
|
List of middlewares to be used for the swagger route. |
compress
|
Whether or not to enable gzip compression swagger route.
TYPE:
|
security_schemes
|
A declaration of the security schemes available to be used in the specification. |
security
|
A declaration of which security mechanisms are applied globally across the API. |
oauth2_config
|
The OAuth2 configuration for the Swagger UI.
TYPE:
|
persist_authorization
|
Whether to persist authorization data on browser close/refresh.
TYPE:
|
openapi_extensions
|
Additional OpenAPI extensions as a dictionary. |
Source code in aws_lambda_powertools/event_handler/api_gateway.py
2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 |
|
get_openapi_json_schema ¶
get_openapi_json_schema(
*,
title: str = DEFAULT_OPENAPI_TITLE,
version: str = DEFAULT_API_VERSION,
openapi_version: str = DEFAULT_OPENAPI_VERSION,
summary: str | None = None,
description: str | None = None,
tags: list[Tag | str] | None = None,
servers: list[Server] | None = None,
terms_of_service: str | None = None,
contact: Contact | None = None,
license_info: License | None = None,
security_schemes: dict[str, SecurityScheme]
| None = None,
security: list[dict[str, list[str]]] | None = None,
openapi_extensions: dict[str, Any] | None = None,
) -> str
Returns the OpenAPI schema as a JSON serializable dict
PARAMETER | DESCRIPTION |
---|---|
title
|
The title of the application.
TYPE:
|
version
|
The version of the OpenAPI document (which is distinct from the OpenAPI Specification version or the API
TYPE:
|
openapi_version
|
The version of the OpenAPI Specification (which the document uses).
TYPE:
|
summary
|
A short summary of what the application does.
TYPE:
|
description
|
A verbose explanation of the application behavior.
TYPE:
|
tags
|
A list of tags used by the specification with additional metadata. |
servers
|
An array of Server Objects, which provide connectivity information to a target server.
TYPE:
|
terms_of_service
|
A URL to the Terms of Service for the API. MUST be in the format of a URL.
TYPE:
|
contact
|
The contact information for the exposed API.
TYPE:
|
license_info
|
The license information for the exposed API.
TYPE:
|
security_schemes
|
A declaration of the security schemes available to be used in the specification. |
security
|
A declaration of which security mechanisms are applied globally across the API. |
openapi_extensions
|
Additional OpenAPI extensions as a dictionary. |
RETURNS | DESCRIPTION |
---|---|
str
|
The OpenAPI schema as a JSON serializable dict. |
Source code in aws_lambda_powertools/event_handler/api_gateway.py
1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 |
|
get_openapi_schema ¶
get_openapi_schema(
*,
title: str = DEFAULT_OPENAPI_TITLE,
version: str = DEFAULT_API_VERSION,
openapi_version: str = DEFAULT_OPENAPI_VERSION,
summary: str | None = None,
description: str | None = None,
tags: list[Tag | str] | None = None,
servers: list[Server] | None = None,
terms_of_service: str | None = None,
contact: Contact | None = None,
license_info: License | None = None,
security_schemes: dict[str, SecurityScheme]
| None = None,
security: list[dict[str, list[str]]] | None = None,
openapi_extensions: dict[str, Any] | None = None,
) -> OpenAPI
Returns the OpenAPI schema as a pydantic model.
PARAMETER | DESCRIPTION |
---|---|
title
|
The title of the application.
TYPE:
|
version
|
The version of the OpenAPI document (which is distinct from the OpenAPI Specification version or the API
TYPE:
|
openapi_version
|
The version of the OpenAPI Specification (which the document uses).
TYPE:
|
summary
|
A short summary of what the application does.
TYPE:
|
description
|
A verbose explanation of the application behavior.
TYPE:
|
tags
|
A list of tags used by the specification with additional metadata. |
servers
|
An array of Server Objects, which provide connectivity information to a target server.
TYPE:
|
terms_of_service
|
A URL to the Terms of Service for the API. MUST be in the format of a URL.
TYPE:
|
contact
|
The contact information for the exposed API.
TYPE:
|
license_info
|
The license information for the exposed API.
TYPE:
|
security_schemes
|
A declaration of the security schemes available to be used in the specification. |
security
|
A declaration of which security mechanisms are applied globally across the API. |
openapi_extensions
|
Additional OpenAPI extensions as a dictionary. |
RETURNS | DESCRIPTION |
---|---|
OpenAPI
|
The OpenAPI schema as a pydantic model.
TYPE:
|
Source code in aws_lambda_powertools/event_handler/api_gateway.py
1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 |
|
include_router ¶
include_router(
router: Router, prefix: str | None = None
) -> None
Adds all routes and context defined in a router
PARAMETER | DESCRIPTION |
---|---|
router
|
The Router containing a list of routes to be registered after the existing routes
TYPE:
|
prefix
|
An optional prefix to be added to the originally defined rule
TYPE:
|
Source code in aws_lambda_powertools/event_handler/api_gateway.py
2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 |
|
resolve ¶
resolve(
event: dict[str, Any], context: LambdaContext
) -> dict[str, Any]
Resolves the response based on the provide event and decorator routes
Internals¶
Request processing chain is triggered by a Route object being called (_call_route
-> __call__
):
- When a route is matched 1.1. Exception handlers (if any exception bubbled up and caught) 1.2. Global middlewares (before, and after on the way back) 1.3. Path level middleware (before, and after on the way back) 1.4. Middleware adapter to ensure Response is homogenous (_registered_api_adapter) 1.5. Run actual route
- When a route is NOT matched 2.1. Exception handlers (if any exception bubbled up and caught) 2.2. Global middlewares (before, and after on the way back) 2.3. Path level middleware (before, and after on the way back) 2.4. Middleware adapter to ensure Response is homogenous (_registered_api_adapter) 2.5. Run 404 route handler
- When a route is a pre-flight CORS (often not matched) 3.1. Exception handlers (if any exception bubbled up and caught) 3.2. Global middlewares (before, and after on the way back) 3.3. Path level middleware (before, and after on the way back) 3.4. Middleware adapter to ensure Response is homogenous (_registered_api_adapter) 3.5. Return 204 with appropriate CORS headers
- When a route is matched with Data Validation enabled 4.1. Exception handlers (if any exception bubbled up and caught) 4.2. Data Validation middleware (before, and after on the way back) 4.3. Global middlewares (before, and after on the way back) 4.4. Path level middleware (before, and after on the way back) 4.5. Middleware adapter to ensure Response is homogenous (_registered_api_adapter) 4.6. Run actual route
PARAMETER | DESCRIPTION |
---|---|
event
|
Event |
context
|
Lambda context
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
dict
|
Returns the dict response |
Source code in aws_lambda_powertools/event_handler/api_gateway.py
2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 |
|
route ¶
route(
rule: str,
method: str | list[str] | tuple[str],
cors: bool | None = None,
compress: bool = False,
cache_control: str | None = None,
summary: str | None = None,
description: str | None = None,
responses: dict[int, OpenAPIResponse] | None = None,
response_description: str = _DEFAULT_OPENAPI_RESPONSE_DESCRIPTION,
tags: list[str] | None = None,
operation_id: str | None = None,
include_in_schema: bool = True,
security: list[dict[str, list[str]]] | None = None,
openapi_extensions: dict[str, Any] | None = None,
deprecated: bool = False,
custom_response_validation_http_code: int
| HTTPStatus
| None = None,
middlewares: list[Callable[..., Any]] | None = None,
) -> Callable[[AnyCallableT], AnyCallableT]
Route decorator includes parameter method
Source code in aws_lambda_powertools/event_handler/api_gateway.py
2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 |
|
BaseRouter ¶
Bases: ABC
Base class for Routing
METHOD | DESCRIPTION |
---|---|
append_context |
Append key=value data as routing context |
clear_context |
Resets routing context |
delete |
Delete route decorator with DELETE |
get |
Get route decorator with GET |
head |
Head route decorator with HEAD |
patch |
Patch route decorator with PATCH |
post |
Post route decorator with POST |
put |
Put route decorator with PUT |
use |
Add one or more global middlewares that run before/after route specific middleware. |
append_context ¶
append_context(**additional_context)
Append key=value data as routing context
Source code in aws_lambda_powertools/event_handler/api_gateway.py
1429 1430 1431 |
|
clear_context ¶
clear_context()
Resets routing context
Source code in aws_lambda_powertools/event_handler/api_gateway.py
1433 1434 1435 |
|
delete ¶
delete(
rule: str,
cors: bool | None = None,
compress: bool = False,
cache_control: str | None = None,
summary: str | None = None,
description: str | None = None,
responses: dict[int, OpenAPIResponse] | None = None,
response_description: str = _DEFAULT_OPENAPI_RESPONSE_DESCRIPTION,
tags: list[str] | None = None,
operation_id: str | None = None,
include_in_schema: bool = True,
security: list[dict[str, list[str]]] | None = None,
openapi_extensions: dict[str, Any] | None = None,
deprecated: bool = False,
custom_response_validation_http_code: int
| HTTPStatus
| None = None,
middlewares: list[Callable[..., Any]] | None = None,
) -> Callable[[AnyCallableT], AnyCallableT]
Delete route decorator with DELETE method
Examples:
Simple example with a custom lambda handler using the Tracer capture_lambda_handler decorator
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
Source code in aws_lambda_powertools/event_handler/api_gateway.py
1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 |
|
get ¶
get(
rule: str,
cors: bool | None = None,
compress: bool = False,
cache_control: str | None = None,
summary: str | None = None,
description: str | None = None,
responses: dict[int, OpenAPIResponse] | None = None,
response_description: str = _DEFAULT_OPENAPI_RESPONSE_DESCRIPTION,
tags: list[str] | None = None,
operation_id: str | None = None,
include_in_schema: bool = True,
security: list[dict[str, list[str]]] | None = None,
openapi_extensions: dict[str, Any] | None = None,
deprecated: bool = False,
custom_response_validation_http_code: int
| HTTPStatus
| None = None,
middlewares: list[Callable[..., Any]] | None = None,
) -> Callable[[AnyCallableT], AnyCallableT]
Get route decorator with GET method
Examples:
Simple example with a custom lambda handler using the Tracer capture_lambda_handler decorator
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
Source code in aws_lambda_powertools/event_handler/api_gateway.py
1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 |
|
head ¶
head(
rule: str,
cors: bool | None = None,
compress: bool = False,
cache_control: str | None = None,
summary: str | None = None,
description: str | None = None,
responses: dict[int, OpenAPIResponse] | None = None,
response_description: str = _DEFAULT_OPENAPI_RESPONSE_DESCRIPTION,
tags: list[str] | None = None,
operation_id: str | None = None,
include_in_schema: bool = True,
security: list[dict[str, list[str]]] | None = None,
openapi_extensions: dict[str, Any] | None = None,
deprecated: bool = False,
custom_response_validation_http_code: int
| HTTPStatus
| None = None,
middlewares: list[Callable] | None = None,
) -> Callable[[AnyCallableT], AnyCallableT]
Head route decorator with HEAD method
Examples:
Simple example with a custom lambda handler using the Tracer capture_lambda_handler decorator
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
Source code in aws_lambda_powertools/event_handler/api_gateway.py
1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 |
|
patch ¶
patch(
rule: str,
cors: bool | None = None,
compress: bool = False,
cache_control: str | None = None,
summary: str | None = None,
description: str | None = None,
responses: dict[int, OpenAPIResponse] | None = None,
response_description: str = _DEFAULT_OPENAPI_RESPONSE_DESCRIPTION,
tags: list[str] | None = None,
operation_id: str | None = None,
include_in_schema: bool = True,
security: list[dict[str, list[str]]] | None = None,
openapi_extensions: dict[str, Any] | None = None,
deprecated: bool = False,
custom_response_validation_http_code: int
| HTTPStatus
| None = None,
middlewares: list[Callable] | None = None,
) -> Callable[[AnyCallableT], AnyCallableT]
Patch route decorator with PATCH method
Examples:
Simple example with a custom lambda handler using the Tracer capture_lambda_handler decorator
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
Source code in aws_lambda_powertools/event_handler/api_gateway.py
1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 |
|
post ¶
post(
rule: str,
cors: bool | None = None,
compress: bool = False,
cache_control: str | None = None,
summary: str | None = None,
description: str | None = None,
responses: dict[int, OpenAPIResponse] | None = None,
response_description: str = _DEFAULT_OPENAPI_RESPONSE_DESCRIPTION,
tags: list[str] | None = None,
operation_id: str | None = None,
include_in_schema: bool = True,
security: list[dict[str, list[str]]] | None = None,
openapi_extensions: dict[str, Any] | None = None,
deprecated: bool = False,
custom_response_validation_http_code: int
| HTTPStatus
| None = None,
middlewares: list[Callable[..., Any]] | None = None,
) -> Callable[[AnyCallableT], AnyCallableT]
Post route decorator with POST method
Examples:
Simple example with a custom lambda handler using the Tracer capture_lambda_handler decorator
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
Source code in aws_lambda_powertools/event_handler/api_gateway.py
1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 |
|
put ¶
put(
rule: str,
cors: bool | None = None,
compress: bool = False,
cache_control: str | None = None,
summary: str | None = None,
description: str | None = None,
responses: dict[int, OpenAPIResponse] | None = None,
response_description: str = _DEFAULT_OPENAPI_RESPONSE_DESCRIPTION,
tags: list[str] | None = None,
operation_id: str | None = None,
include_in_schema: bool = True,
security: list[dict[str, list[str]]] | None = None,
openapi_extensions: dict[str, Any] | None = None,
deprecated: bool = False,
custom_response_validation_http_code: int
| HTTPStatus
| None = None,
middlewares: list[Callable[..., Any]] | None = None,
) -> Callable[[AnyCallableT], AnyCallableT]
Put route decorator with PUT method
Examples:
Simple example with a custom lambda handler using the Tracer capture_lambda_handler decorator
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
Source code in aws_lambda_powertools/event_handler/api_gateway.py
1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 |
|
use ¶
use(middlewares: list[Callable[..., Response]]) -> None
Add one or more global middlewares that run before/after route specific middleware.
NOTE: Middlewares are called in insertion order.
PARAMETER | DESCRIPTION |
---|---|
middlewares
|
List of global middlewares to be used |
Examples:
Add middlewares to be used for every request processed by the Router.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
|
Source code in aws_lambda_powertools/event_handler/api_gateway.py
1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 |
|
BedrockResponse ¶
BedrockResponse(
body: Any = None,
status_code: int = 200,
content_type: str = _DEFAULT_CONTENT_TYPE,
session_attributes: dict[str, Any] | None = None,
prompt_session_attributes: dict[str, Any] | None = None,
knowledge_bases_configuration: list[dict[str, Any]]
| None = None,
)
Bases: Generic[ResponseT]
Contains the response body, status code, content type, and optional attributes for session management and knowledge base configuration.
METHOD | DESCRIPTION |
---|---|
is_json |
Returns True if the response is JSON, based on the Content-Type. |
Source code in aws_lambda_powertools/event_handler/api_gateway.py
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 |
|
is_json ¶
is_json() -> bool
Returns True if the response is JSON, based on the Content-Type.
Source code in aws_lambda_powertools/event_handler/api_gateway.py
281 282 283 284 285 |
|
CORSConfig ¶
CORSConfig(
allow_origin: str = "*",
extra_origins: list[str] | None = None,
allow_headers: list[str] | None = None,
expose_headers: list[str] | None = None,
max_age: int | None = None,
allow_credentials: bool = False,
)
CORS Config
Examples:
Simple CORS example using the default permissive CORS, note that this should only be used during early prototyping.
1 2 3 4 5 6 7 8 9 |
|
Using a custom CORSConfig where with_cors
used the custom provided CORSConfig and without_cors
do not include any CORS headers.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
|
METHOD | DESCRIPTION |
---|---|
build_allow_methods |
Build sorted comma delimited methods for Access-Control-Allow-Methods header |
to_dict |
Builds the configured Access-Control http headers |
Source code in aws_lambda_powertools/event_handler/api_gateway.py
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
|
build_allow_methods
staticmethod
¶
build_allow_methods(methods: set[str]) -> str
Build sorted comma delimited methods for Access-Control-Allow-Methods header
PARAMETER | DESCRIPTION |
---|---|
methods
|
Set of HTTP Methods |
RETURNS | DESCRIPTION |
---|---|
set[str]
|
Formatted string with all HTTP Methods allowed for CORS e.g., |
Source code in aws_lambda_powertools/event_handler/api_gateway.py
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 |
|
to_dict ¶
to_dict(origin: str | None) -> dict[str, str]
Builds the configured Access-Control http headers
Source code in aws_lambda_powertools/event_handler/api_gateway.py
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 |
|
MiddlewareFrame ¶
MiddlewareFrame(
current_middleware: Callable[..., Any],
next_middleware: Callable[..., Any],
)
Creates a Middle Stack Wrapper instance to be used as a "Frame" in the overall stack of middleware functions. Each instance contains the current middleware and the next middleware function to be called in the stack.
In this way the middleware stack is constructed in a recursive fashion, with each middleware calling the next as a simple function call. The actual Python call-stack will contain each MiddlewareStackWrapper "Frame", meaning any Middleware function can cause the entire Middleware call chain to be exited early (short-circuited) by raising an exception or by simply returning early with a custom Response. The decision to short-circuit the middleware chain is at the user's discretion but instantly available due to the Wrapped nature of the callable constructs in the Middleware stack and each Middleware function having complete control over whether the "Next" handler in the stack is called or not.
PARAMETER | DESCRIPTION |
---|---|
current_middleware
|
The current middleware function to be called as a request is processed.
TYPE:
|
next_middleware
|
The next middleware in the middleware stack.
TYPE:
|
Source code in aws_lambda_powertools/event_handler/api_gateway.py
1461 1462 1463 1464 1465 1466 1467 1468 |
|
Response ¶
Response(
status_code: int,
content_type: str | None = None,
body: ResponseT | None = None,
headers: Mapping[str, str | list[str]] | None = None,
cookies: list[Cookie] | None = None,
compress: bool | None = None,
)
Bases: Generic[ResponseT]
Response data class that provides greater control over what is returned from the proxy event
METHOD | DESCRIPTION |
---|---|
is_json |
Returns True if the response is JSON, based on the Content-Type. |
Source code in aws_lambda_powertools/event_handler/api_gateway.py
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 |
|
is_json ¶
is_json() -> bool
Returns True if the response is JSON, based on the Content-Type.
Source code in aws_lambda_powertools/event_handler/api_gateway.py
326 327 328 329 330 331 332 333 |
|
ResponseBuilder ¶
ResponseBuilder(
response: Response,
serializer: Callable[[Any], str] = _JSON_DUMP_CALL,
route: Route | None = None,
)
Bases: Generic[ResponseEventT]
Internally used Response builder
METHOD | DESCRIPTION |
---|---|
build |
Build the full response dict to be returned by the lambda |
Source code in aws_lambda_powertools/event_handler/api_gateway.py
867 868 869 870 871 872 873 874 875 |
|
build ¶
build(
event: ResponseEventT, cors: CORSConfig | None = None
) -> dict[str, Any]
Build the full response dict to be returned by the lambda
Source code in aws_lambda_powertools/event_handler/api_gateway.py
948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 |
|
Route ¶
Route(
method: str,
path: str,
rule: Pattern,
func: Callable,
cors: bool,
compress: bool,
cache_control: str | None = None,
summary: str | None = None,
description: str | None = None,
responses: dict[int, OpenAPIResponse] | None = None,
response_description: str | None = None,
tags: list[str] | None = None,
operation_id: str | None = None,
include_in_schema: bool = True,
security: list[dict[str, list[str]]] | None = None,
openapi_extensions: dict[str, Any] | None = None,
deprecated: bool = False,
custom_response_validation_http_code: HTTPStatus
| None = None,
middlewares: list[Callable[..., Response]]
| None = None,
)
Internally used Route Configuration
PARAMETER | DESCRIPTION |
---|---|
method
|
The HTTP method, example "GET"
TYPE:
|
path
|
The path of the route
TYPE:
|
rule
|
The route rule, example "/my/path"
TYPE:
|
func
|
The route handler function
TYPE:
|
cors
|
Whether or not to enable CORS for this route
TYPE:
|
compress
|
Whether or not to enable gzip compression for this route
TYPE:
|
cache_control
|
The cache control header value, example "max-age=3600"
TYPE:
|
summary
|
The OpenAPI summary for this route
TYPE:
|
description
|
The OpenAPI description for this route
TYPE:
|
responses
|
The OpenAPI responses for this route |
response_description
|
The OpenAPI response description for this route
TYPE:
|
tags
|
The list of OpenAPI tags to be used for this route |
operation_id
|
The OpenAPI operationId for this route
TYPE:
|
include_in_schema
|
Whether or not to include this route in the OpenAPI schema
TYPE:
|
security
|
The OpenAPI security for this route |
openapi_extensions
|
Additional OpenAPI extensions as a dictionary. |
deprecated
|
Whether or not to mark this route as deprecated in the OpenAPI schema
TYPE:
|
custom_response_validation_http_code
|
Whether to have custom http status code for this route if response validation fails
TYPE:
|
middlewares
|
The list of route middlewares to be called in order. |
Source code in aws_lambda_powertools/event_handler/api_gateway.py
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 |
|
Router ¶
Router()
Bases: BaseRouter
Router helper class to allow splitting ApiGatewayResolver into multiple files
Source code in aws_lambda_powertools/event_handler/api_gateway.py
2773 2774 2775 2776 2777 2778 |
|