Skip to content

Commit bfe41a6

Browse files
author
刘一辰
committed
1. 新增协议 custom-body , 默认用户参数【Body以外】使用GET方式请求,body参数由用户自定义内容,通过Body请求。
2. 服务下的各API的Action支持单独配置protocol,优先级最高,其次为Service的protocol,支持协议枚举与Service一致。 业务需求可通过针对特定Action配置custom-body协议支持,body内容对象由用户自行组织,如配置input shape,需定义body参数结构
1 parent 4e44b67 commit bfe41a6

File tree

5 files changed

+44
-41
lines changed

5 files changed

+44
-41
lines changed

kscore/client.py

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -557,20 +557,13 @@ def _convert_to_request_dict(self, api_params, operation_model,
557557
operation_name=operation_name),
558558
params=api_params, model=operation_model, context=context)
559559

560-
operation_protocol_serialize = None
561-
if operation_model._operation_model.has_key('protocol'):
562-
operation_protocol = operation_model._operation_model['protocol']
563-
if operation_protocol is not None:
564-
operation_protocol_serialize = kscore.serialize.create_serializer(
565-
operation_protocol, True)
566-
567-
if operation_protocol_serialize is not None:
568-
request_dict = operation_protocol_serialize.serialize_to_request(
569-
api_params, operation_model
570-
)
571-
else:
572-
request_dict = self._serializer.serialize_to_request(
573-
api_params, operation_model)
560+
serializer = self._serializer
561+
562+
if operation_model.is_rewrite_protocol:
563+
564+
serializer = kscore.serialize.create_serializer(operation_model.protocol, True)
565+
566+
request_dict = serializer.serialize_to_request(api_params, operation_model)
574567

575568
prepare_request_dict(request_dict, endpoint_url=self._endpoint.host,
576569
user_agent=self._client_config.user_agent)

kscore/endpoint.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -195,15 +195,9 @@ def _get_response(self, request, operation_model, attempts):
195195
exc_info=True)
196196
return (None, e)
197197
# This returns the http_response and the parsed_data.
198-
response_dict = convert_to_response_dict(http_response,
199-
operation_model)
200-
201-
protocol = operation_model.metadata['protocol']
202-
if operation_model._operation_model.has_key('protocol'):
203-
operation_protocol = operation_model._operation_model['protocol']
204-
if operation_protocol is not None:
205-
protocol = operation_protocol
206-
parser = self._response_parser_factory.create_parser(protocol)
198+
response_dict = convert_to_response_dict(http_response, operation_model)
199+
parser = self._response_parser_factory.create_parser(
200+
operation_model.protocol)
207201

208202
return ((http_response,
209203
parser.parse(response_dict, operation_model.output_shape)),

kscore/model.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,17 @@ def wire_name(self):
380380
"""
381381
return self._operation_model.get('name')
382382

383+
@property
384+
def protocol(self):
385+
"""protocol from operation_model or service_model
386+
"""
387+
return self._operation_model.get("protocol", self.metadata['protocol'])
388+
389+
@property
390+
def is_rewrite_protocol(self):
391+
392+
return "protocol" in self._operation_model
393+
383394
@property
384395
def service_model(self):
385396
return self._service_model

kscore/parsers.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -590,7 +590,20 @@ def _do_parse(self, response, shape):
590590

591591
def _do_error_parse(self, response, shape):
592592
body = self._parse_body_as_json(response['body'])
593-
error = {"ResponseMetadata": {}, 'Error': body.get("Error", body.get("error", {}))}
593+
594+
if "Error" in body or "error" in body:
595+
error = {"ResponseMetadata": {}, 'Error': body.get("Error", body.get("error", {}))}
596+
else:
597+
if "Code" in body or "code" in body:
598+
code = body.get("Code", body.get("code"))
599+
else:
600+
code = response['status_code']
601+
if "Message" in body or "message" in body:
602+
message = body.get("Message", body.get("message"))
603+
else:
604+
message = str(body)
605+
error = {"ResponseMetadata": {}, 'Error': {'Code': code, 'Message': message}}
606+
594607
error['ResponseMetadata'].update(RequestId=body.get("RequestId"))
595608
return error
596609

kscore/serialize.py

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -348,15 +348,7 @@ def _serialize_data(self, serialized, data):
348348
return serialized
349349

350350

351-
class CustomBodySerializer(QuerySerializer):
352-
353-
@property
354-
def headers(self):
355-
return {"Accept": 'application/json'}
356-
357-
def _serialize_not_shape(self, data, parameters):
358-
359-
data.update(parameters)
351+
class CustomBodySerializer(QueryAcceptJsonSerializer):
360352

361353
def serialize_to_request(self, parameters, operation_model):
362354
shape = operation_model.input_shape
@@ -372,21 +364,20 @@ def serialize_to_request(self, parameters, operation_model):
372364
}
373365
)
374366
body_params = self.MAP_TYPE()
367+
custom_body = None
375368
if 'Body' in parameters:
376-
body_params['Body'] = parameters['Body']
377-
del parameters['Body']
378-
369+
custom_body = parameters.pop('Body')
379370
if shape is not None:
380371
self._serialize(body_params, parameters, shape)
381372
else:
382373
self._serialize_not_shape(body_params, parameters)
383-
return self._serialize_data(serialized, body_params)
374+
return self._serialize_data(serialized, body_params, custom_body)
384375

385-
def _serialize_data(self, serialized, data):
386-
serialized['body'] = json.dumps(data).encode(self.DEFAULT_ENCODING)
387-
if 'Body' in data:
388-
del data['Body']
376+
def _serialize_data(self, serialized, data, body=None):
377+
if body is not None:
378+
serialized['body'] = json.dumps(body).encode(self.DEFAULT_ENCODING)
389379
serialized['query_string'] = data
380+
print serialized
390381
return serialized
391382

392383

@@ -397,6 +388,7 @@ class JSONSerializer(Serializer):
397388
TIMESTAMP_FORMAT = 'unixtimestamp'
398389

399390
def serialize_to_request(self, parameters, operation_model):
391+
400392
target = '%s.%s' % (operation_model.metadata['targetPrefix'],
401393
operation_model.name)
402394
serialized = self._create_default_request()

0 commit comments

Comments
 (0)