Linting
This commit is contained in:
parent
1c96a05314
commit
69f963a3c2
@ -39,7 +39,7 @@ from .exceptions import (
|
|||||||
InvalidRestApiId,
|
InvalidRestApiId,
|
||||||
InvalidModelName,
|
InvalidModelName,
|
||||||
RestAPINotFound,
|
RestAPINotFound,
|
||||||
ModelNotFound
|
ModelNotFound,
|
||||||
)
|
)
|
||||||
|
|
||||||
STAGE_URL = "https://{api_id}.execute-api.{region_name}.amazonaws.com/{stage_name}"
|
STAGE_URL = "https://{api_id}.execute-api.{region_name}.amazonaws.com/{stage_name}"
|
||||||
@ -499,13 +499,15 @@ class RestAPI(BaseModel):
|
|||||||
self.resources[child_id] = child
|
self.resources[child_id] = child
|
||||||
return child
|
return child
|
||||||
|
|
||||||
def add_model(self,
|
def add_model(
|
||||||
|
self,
|
||||||
name,
|
name,
|
||||||
description=None,
|
description=None,
|
||||||
schema=None,
|
schema=None,
|
||||||
content_type=None,
|
content_type=None,
|
||||||
cli_input_json=None,
|
cli_input_json=None,
|
||||||
generate_cli_skeleton=None):
|
generate_cli_skeleton=None,
|
||||||
|
):
|
||||||
model_id = create_id()
|
model_id = create_id()
|
||||||
new_model = Model(
|
new_model = Model(
|
||||||
id=model_id,
|
id=model_id,
|
||||||
@ -514,7 +516,8 @@ class RestAPI(BaseModel):
|
|||||||
schema=schema,
|
schema=schema,
|
||||||
content_type=content_type,
|
content_type=content_type,
|
||||||
cli_input_json=cli_input_json,
|
cli_input_json=cli_input_json,
|
||||||
generate_cli_skeleton=generate_cli_skeleton)
|
generate_cli_skeleton=generate_cli_skeleton,
|
||||||
|
)
|
||||||
|
|
||||||
self.models[name] = new_model
|
self.models[name] = new_model
|
||||||
return new_model
|
return new_model
|
||||||
@ -670,7 +673,7 @@ class DomainName(BaseModel, dict):
|
|||||||
self["generateCliSkeleton"] = kwargs.get("generate_cli_skeleton")
|
self["generateCliSkeleton"] = kwargs.get("generate_cli_skeleton")
|
||||||
|
|
||||||
|
|
||||||
class Model(BaseModel,dict):
|
class Model(BaseModel, dict):
|
||||||
def __init__(self, id, name, **kwargs):
|
def __init__(self, id, name, **kwargs):
|
||||||
super(Model, self).__init__()
|
super(Model, self).__init__()
|
||||||
self["id"] = id
|
self["id"] = id
|
||||||
@ -1130,14 +1133,16 @@ class APIGatewayBackend(BaseBackend):
|
|||||||
else:
|
else:
|
||||||
return self.domain_names[domain_name]
|
return self.domain_names[domain_name]
|
||||||
|
|
||||||
def create_model(self,
|
def create_model(
|
||||||
|
self,
|
||||||
rest_api_id,
|
rest_api_id,
|
||||||
name,
|
name,
|
||||||
content_type,
|
content_type,
|
||||||
description=None,
|
description=None,
|
||||||
schema=None,
|
schema=None,
|
||||||
cli_input_json=None,
|
cli_input_json=None,
|
||||||
generate_cli_skeleton=None):
|
generate_cli_skeleton=None,
|
||||||
|
):
|
||||||
|
|
||||||
if not rest_api_id:
|
if not rest_api_id:
|
||||||
raise InvalidRestApiId
|
raise InvalidRestApiId
|
||||||
@ -1151,7 +1156,8 @@ class APIGatewayBackend(BaseBackend):
|
|||||||
schema=schema,
|
schema=schema,
|
||||||
content_type=content_type,
|
content_type=content_type,
|
||||||
cli_input_json=cli_input_json,
|
cli_input_json=cli_input_json,
|
||||||
generate_cli_skeleton=generate_cli_skeleton)
|
generate_cli_skeleton=generate_cli_skeleton,
|
||||||
|
)
|
||||||
|
|
||||||
return new_model
|
return new_model
|
||||||
|
|
||||||
|
@ -16,7 +16,7 @@ from .exceptions import (
|
|||||||
InvalidRestApiId,
|
InvalidRestApiId,
|
||||||
InvalidModelName,
|
InvalidModelName,
|
||||||
RestAPINotFound,
|
RestAPINotFound,
|
||||||
ModelNotFound
|
ModelNotFound,
|
||||||
)
|
)
|
||||||
|
|
||||||
API_KEY_SOURCES = ["AUTHORIZER", "HEADER"]
|
API_KEY_SOURCES = ["AUTHORIZER", "HEADER"]
|
||||||
@ -600,15 +600,13 @@ class APIGatewayResponse(BaseResponse):
|
|||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
def models(self,request, full_url, headers):
|
def models(self, request, full_url, headers):
|
||||||
self.setup_class(request, full_url, headers)
|
self.setup_class(request, full_url, headers)
|
||||||
rest_api_id = self.path.replace("/restapis/", "", 1).split("/")[0]
|
rest_api_id = self.path.replace("/restapis/", "", 1).split("/")[0]
|
||||||
|
|
||||||
try:
|
try:
|
||||||
if self.method == "GET":
|
if self.method == "GET":
|
||||||
models = self.backend.get_models(
|
models = self.backend.get_models(rest_api_id)
|
||||||
rest_api_id
|
|
||||||
)
|
|
||||||
return 200, {}, json.dumps({"item": models})
|
return 200, {}, json.dumps({"item": models})
|
||||||
|
|
||||||
elif self.method == "POST":
|
elif self.method == "POST":
|
||||||
@ -617,9 +615,7 @@ class APIGatewayResponse(BaseResponse):
|
|||||||
schema = self._get_param("schema")
|
schema = self._get_param("schema")
|
||||||
content_type = self._get_param("contentType")
|
content_type = self._get_param("contentType")
|
||||||
cli_input_json = self._get_param("cliInputJson")
|
cli_input_json = self._get_param("cliInputJson")
|
||||||
generate_cli_skeleton = self._get_param(
|
generate_cli_skeleton = self._get_param("generateCliSkeleton")
|
||||||
"generateCliSkeleton"
|
|
||||||
)
|
|
||||||
model = self.backend.create_model(
|
model = self.backend.create_model(
|
||||||
rest_api_id,
|
rest_api_id,
|
||||||
name,
|
name,
|
||||||
@ -627,12 +623,12 @@ class APIGatewayResponse(BaseResponse):
|
|||||||
description,
|
description,
|
||||||
schema,
|
schema,
|
||||||
cli_input_json,
|
cli_input_json,
|
||||||
generate_cli_skeleton
|
generate_cli_skeleton,
|
||||||
)
|
)
|
||||||
|
|
||||||
return 200, {}, json.dumps(model)
|
return 200, {}, json.dumps(model)
|
||||||
|
|
||||||
except (InvalidRestApiId, InvalidModelName,RestAPINotFound) as error:
|
except (InvalidRestApiId, InvalidModelName, RestAPINotFound) as error:
|
||||||
return (
|
return (
|
||||||
error.code,
|
error.code,
|
||||||
{},
|
{},
|
||||||
@ -649,13 +645,14 @@ class APIGatewayResponse(BaseResponse):
|
|||||||
model_info = {}
|
model_info = {}
|
||||||
try:
|
try:
|
||||||
if self.method == "GET":
|
if self.method == "GET":
|
||||||
model_info = self.backend.get_model(
|
model_info = self.backend.get_model(rest_api_id, model_name)
|
||||||
rest_api_id,
|
|
||||||
model_name
|
|
||||||
)
|
|
||||||
return 200, {}, json.dumps(model_info)
|
return 200, {}, json.dumps(model_info)
|
||||||
except (ModelNotFound, RestAPINotFound, InvalidRestApiId,
|
except (
|
||||||
InvalidModelName) as error:
|
ModelNotFound,
|
||||||
|
RestAPINotFound,
|
||||||
|
InvalidRestApiId,
|
||||||
|
InvalidModelName,
|
||||||
|
) as error:
|
||||||
return (
|
return (
|
||||||
error.code,
|
error.code,
|
||||||
{},
|
{},
|
||||||
|
@ -1550,20 +1550,18 @@ def test_get_domain_name():
|
|||||||
@mock_apigateway
|
@mock_apigateway
|
||||||
def test_create_model():
|
def test_create_model():
|
||||||
client = boto3.client("apigateway", region_name="us-west-2")
|
client = boto3.client("apigateway", region_name="us-west-2")
|
||||||
response = client.create_rest_api(name="my_api",
|
response = client.create_rest_api(name="my_api", description="this is my api")
|
||||||
description="this is my api"
|
|
||||||
)
|
|
||||||
rest_api_id = response["id"]
|
rest_api_id = response["id"]
|
||||||
dummy_rest_api_id = 'a12b3c4d'
|
dummy_rest_api_id = "a12b3c4d"
|
||||||
model_name = "testModel"
|
model_name = "testModel"
|
||||||
description = "test model"
|
description = "test model"
|
||||||
content_type = 'application/json'
|
content_type = "application/json"
|
||||||
# success case with valid params
|
# success case with valid params
|
||||||
response = client.create_model(
|
response = client.create_model(
|
||||||
restApiId=rest_api_id,
|
restApiId=rest_api_id,
|
||||||
name=model_name,
|
name=model_name,
|
||||||
description=description,
|
description=description,
|
||||||
contentType=content_type
|
contentType=content_type,
|
||||||
)
|
)
|
||||||
response["name"].should.equal(model_name)
|
response["name"].should.equal(model_name)
|
||||||
response["description"].should.equal(description)
|
response["description"].should.equal(description)
|
||||||
@ -1574,58 +1572,45 @@ def test_create_model():
|
|||||||
restApiId=dummy_rest_api_id,
|
restApiId=dummy_rest_api_id,
|
||||||
name=model_name,
|
name=model_name,
|
||||||
description=description,
|
description=description,
|
||||||
contentType=content_type
|
contentType=content_type,
|
||||||
)
|
)
|
||||||
ex.exception.response["Error"]["Message"].should.equal(
|
ex.exception.response["Error"]["Message"].should.equal(
|
||||||
"Invalid Rest API Id specified"
|
"Invalid Rest API Id specified"
|
||||||
)
|
)
|
||||||
ex.exception.response["Error"]["Code"].should.equal(
|
ex.exception.response["Error"]["Code"].should.equal("NotFoundException")
|
||||||
"NotFoundException"
|
|
||||||
)
|
|
||||||
|
|
||||||
with assert_raises(ClientError) as ex:
|
with assert_raises(ClientError) as ex:
|
||||||
client.create_model(
|
client.create_model(
|
||||||
restApiId=rest_api_id,
|
restApiId=rest_api_id,
|
||||||
name="",
|
name="",
|
||||||
description=description,
|
description=description,
|
||||||
contentType=content_type
|
contentType=content_type,
|
||||||
)
|
)
|
||||||
|
|
||||||
ex.exception.response["Error"]["Message"].should.equal(
|
ex.exception.response["Error"]["Message"].should.equal("No Model Name specified")
|
||||||
"No Model Name specified"
|
ex.exception.response["Error"]["Code"].should.equal("BadRequestException")
|
||||||
)
|
|
||||||
ex.exception.response["Error"]["Code"].should.equal(
|
|
||||||
"BadRequestException"
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
@mock_apigateway
|
@mock_apigateway
|
||||||
def test_get_api_models():
|
def test_get_api_models():
|
||||||
client = boto3.client("apigateway", region_name="us-west-2")
|
client = boto3.client("apigateway", region_name="us-west-2")
|
||||||
response = client.create_rest_api(
|
response = client.create_rest_api(name="my_api", description="this is my api")
|
||||||
name="my_api",
|
|
||||||
description="this is my api"
|
|
||||||
)
|
|
||||||
rest_api_id = response["id"]
|
rest_api_id = response["id"]
|
||||||
model_name = "testModel"
|
model_name = "testModel"
|
||||||
description = "test model"
|
description = "test model"
|
||||||
content_type = 'application/json'
|
content_type = "application/json"
|
||||||
# when no models are present
|
# when no models are present
|
||||||
result = client.get_models(
|
result = client.get_models(restApiId=rest_api_id)
|
||||||
restApiId=rest_api_id
|
|
||||||
)
|
|
||||||
result["items"].should.equal([])
|
result["items"].should.equal([])
|
||||||
# add a model
|
# add a model
|
||||||
client.create_model(
|
client.create_model(
|
||||||
restApiId=rest_api_id,
|
restApiId=rest_api_id,
|
||||||
name=model_name,
|
name=model_name,
|
||||||
description=description,
|
description=description,
|
||||||
contentType=content_type
|
contentType=content_type,
|
||||||
)
|
)
|
||||||
# get models after adding
|
# get models after adding
|
||||||
result = client.get_models(
|
result = client.get_models(restApiId=rest_api_id)
|
||||||
restApiId=rest_api_id
|
|
||||||
)
|
|
||||||
result["items"][0]["name"] = model_name
|
result["items"][0]["name"] = model_name
|
||||||
result["items"][0]["description"] = description
|
result["items"][0]["description"] = description
|
||||||
|
|
||||||
@ -1633,60 +1618,44 @@ def test_get_api_models():
|
|||||||
@mock_apigateway
|
@mock_apigateway
|
||||||
def test_get_model_by_name():
|
def test_get_model_by_name():
|
||||||
client = boto3.client("apigateway", region_name="us-west-2")
|
client = boto3.client("apigateway", region_name="us-west-2")
|
||||||
response = client.create_rest_api(
|
response = client.create_rest_api(name="my_api", description="this is my api")
|
||||||
name="my_api",
|
|
||||||
description="this is my api"
|
|
||||||
)
|
|
||||||
rest_api_id = response["id"]
|
rest_api_id = response["id"]
|
||||||
dummy_rest_api_id = 'a12b3c4d'
|
dummy_rest_api_id = "a12b3c4d"
|
||||||
model_name = "testModel"
|
model_name = "testModel"
|
||||||
description = "test model"
|
description = "test model"
|
||||||
content_type = 'application/json'
|
content_type = "application/json"
|
||||||
# add a model
|
# add a model
|
||||||
client.create_model(
|
client.create_model(
|
||||||
restApiId=rest_api_id,
|
restApiId=rest_api_id,
|
||||||
name=model_name,
|
name=model_name,
|
||||||
description=description,
|
description=description,
|
||||||
contentType=content_type
|
contentType=content_type,
|
||||||
)
|
)
|
||||||
# get models after adding
|
# get models after adding
|
||||||
result = client.get_model(
|
result = client.get_model(restApiId=rest_api_id, modelName=model_name)
|
||||||
restApiId=rest_api_id, modelName=model_name
|
|
||||||
)
|
|
||||||
result["name"] = model_name
|
result["name"] = model_name
|
||||||
result["description"] = description
|
result["description"] = description
|
||||||
|
|
||||||
with assert_raises(ClientError) as ex:
|
with assert_raises(ClientError) as ex:
|
||||||
client.get_model(
|
client.get_model(restApiId=dummy_rest_api_id, modelName=model_name)
|
||||||
restApiId=dummy_rest_api_id, modelName=model_name
|
|
||||||
)
|
|
||||||
ex.exception.response["Error"]["Message"].should.equal(
|
ex.exception.response["Error"]["Message"].should.equal(
|
||||||
"Invalid Rest API Id specified"
|
"Invalid Rest API Id specified"
|
||||||
)
|
)
|
||||||
ex.exception.response["Error"]["Code"].should.equal(
|
ex.exception.response["Error"]["Code"].should.equal("NotFoundException")
|
||||||
"NotFoundException"
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
@mock_apigateway
|
@mock_apigateway
|
||||||
def test_get_model_with_invalid_name():
|
def test_get_model_with_invalid_name():
|
||||||
client = boto3.client("apigateway", region_name="us-west-2")
|
client = boto3.client("apigateway", region_name="us-west-2")
|
||||||
response = client.create_rest_api(
|
response = client.create_rest_api(name="my_api", description="this is my api")
|
||||||
name="my_api",
|
|
||||||
description="this is my api"
|
|
||||||
)
|
|
||||||
rest_api_id = response["id"]
|
rest_api_id = response["id"]
|
||||||
# test with an invalid model name
|
# test with an invalid model name
|
||||||
with assert_raises(ClientError) as ex:
|
with assert_raises(ClientError) as ex:
|
||||||
client.get_model(
|
client.get_model(restApiId=rest_api_id, modelName="fake")
|
||||||
restApiId=rest_api_id, modelName="fake"
|
|
||||||
)
|
|
||||||
ex.exception.response["Error"]["Message"].should.equal(
|
ex.exception.response["Error"]["Message"].should.equal(
|
||||||
"Invalid Model Name specified"
|
"Invalid Model Name specified"
|
||||||
)
|
)
|
||||||
ex.exception.response["Error"]["Code"].should.equal(
|
ex.exception.response["Error"]["Code"].should.equal("NotFoundException")
|
||||||
"NotFoundException"
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
@mock_apigateway
|
@mock_apigateway
|
||||||
|
Loading…
Reference in New Issue
Block a user