5
5
import re
6
6
from copy import deepcopy
7
7
from datetime import datetime
8
+ from operator import itemgetter
8
9
from typing import IO , Any
9
10
10
11
from moto .apigateway import models as apigw_models
99
100
OpenAPIExt ,
100
101
apply_json_patch_safe ,
101
102
get_apigateway_store ,
103
+ get_moto_backend ,
102
104
get_moto_rest_api ,
103
105
get_regional_domain_name ,
104
106
get_rest_api_container ,
@@ -725,7 +727,7 @@ def put_method(
725
727
** kwargs ,
726
728
) -> Method :
727
729
# TODO: add missing validation? check order of validation as well
728
- moto_backend = apigw_models . apigateway_backends [ context .account_id ][ context .region ]
730
+ moto_backend = get_moto_backend ( context .account_id , context .region )
729
731
moto_rest_api : MotoRestAPI = moto_backend .apis .get (rest_api_id )
730
732
if not moto_rest_api or not (moto_resource := moto_rest_api .resources .get (resource_id )):
731
733
raise NotFoundException ("Invalid Resource identifier specified" )
@@ -792,7 +794,7 @@ def update_method(
792
794
) -> Method :
793
795
# see https://www.linkedin.com/pulse/updating-aws-cli-patch-operations-rest-api-yitzchak-meirovich/
794
796
# for path construction
795
- moto_backend = apigw_models . apigateway_backends [ context .account_id ][ context .region ]
797
+ moto_backend = get_moto_backend ( context .account_id , context .region )
796
798
moto_rest_api : MotoRestAPI = moto_backend .apis .get (rest_api_id )
797
799
if not moto_rest_api or not (moto_resource := moto_rest_api .resources .get (resource_id )):
798
800
raise NotFoundException ("Invalid Resource identifier specified" )
@@ -907,7 +909,7 @@ def delete_method(
907
909
http_method : String ,
908
910
** kwargs ,
909
911
) -> None :
910
- moto_backend = apigw_models . apigateway_backends [ context .account_id ][ context .region ]
912
+ moto_backend = get_moto_backend ( context .account_id , context .region )
911
913
moto_rest_api : MotoRestAPI = moto_backend .apis .get (rest_api_id )
912
914
if not moto_rest_api or not (moto_resource := moto_rest_api .resources .get (resource_id )):
913
915
raise NotFoundException ("Invalid Resource identifier specified" )
@@ -929,7 +931,7 @@ def get_method_response(
929
931
** kwargs ,
930
932
) -> MethodResponse :
931
933
# this could probably be easier in a patch?
932
- moto_backend = apigw_models . apigateway_backends [ context .account_id ][ context .region ]
934
+ moto_backend = get_moto_backend ( context .account_id , context .region )
933
935
moto_rest_api : MotoRestAPI = moto_backend .apis .get (rest_api_id )
934
936
# TODO: snapshot test different possibilities
935
937
if not moto_rest_api or not (moto_resource := moto_rest_api .resources .get (resource_id )):
@@ -1002,7 +1004,7 @@ def update_stage(
1002
1004
) -> Stage :
1003
1005
call_moto (context )
1004
1006
1005
- moto_backend = apigw_models . apigateway_backends [ context .account_id ][ context .region ]
1007
+ moto_backend = get_moto_backend ( context .account_id , context .region )
1006
1008
moto_rest_api : MotoRestAPI = moto_backend .apis .get (rest_api_id )
1007
1009
if not (moto_stage := moto_rest_api .stages .get (stage_name )):
1008
1010
raise NotFoundException ("Invalid Stage identifier specified" )
@@ -2076,25 +2078,26 @@ def get_api_keys(
2076
2078
include_values : NullableBoolean = None ,
2077
2079
** kwargs ,
2078
2080
) -> ApiKeys :
2079
- moto_response : ApiKeys = call_moto (context = context )
2080
- item_list = PaginatedList (moto_response ["items" ])
2081
+ # TODO: migrate API keys in our store
2082
+ moto_backend = get_moto_backend (context .account_id , context .region )
2083
+ api_keys = [api_key .to_json () for api_key in moto_backend .keys .values ()]
2084
+ if not include_values :
2085
+ for api_key in api_keys :
2086
+ api_key .pop ("value" )
2081
2087
2082
- def token_generator (item ):
2083
- return item ["id" ]
2088
+ item_list = PaginatedList (api_keys )
2084
2089
2085
2090
def filter_function (item ):
2086
2091
return item ["name" ].startswith (name_query )
2087
2092
2088
2093
paginated_list , next_token = item_list .get_page (
2089
- token_generator = token_generator ,
2094
+ token_generator = itemgetter ( "id" ) ,
2090
2095
next_token = position ,
2091
2096
page_size = limit ,
2092
2097
filter_function = filter_function if name_query else None ,
2093
2098
)
2094
2099
2095
- return ApiKeys (
2096
- items = paginated_list , warnings = moto_response .get ("warnings" ), position = next_token
2097
- )
2100
+ return ApiKeys (items = paginated_list , position = next_token )
2098
2101
2099
2102
def update_api_key (
2100
2103
self ,
0 commit comments