@@ -82,15 +82,20 @@ def begin_struct(self, schema: Schema) -> Iterator[ShapeSerializer]:
82
82
host_prefix = self ._endpoint_trait .host_prefix
83
83
84
84
content_type = self ._payload_codec .media_type
85
-
86
85
binding_matcher = RequestBindingMatcher (schema )
87
86
if (payload_member := binding_matcher .payload_member ) is not None :
88
- if payload_member .shape_type in (ShapeType .BLOB , ShapeType .STRING ):
89
- content_type = (
90
- "application/octet-stream"
91
- if payload_member .shape_type is ShapeType .BLOB
92
- else "text/plain"
93
- )
87
+ if payload_member .shape_type in (
88
+ ShapeType .BLOB ,
89
+ ShapeType .STRING ,
90
+ ShapeType .ENUM ,
91
+ ):
92
+ if (media_type := payload_member .get_trait (MediaTypeTrait )) is not None :
93
+ content_type = media_type .value
94
+ elif payload_member .shape_type is ShapeType .BLOB :
95
+ content_type = "application/octet-stream"
96
+ else :
97
+ content_type = "text/plain"
98
+
94
99
payload_serializer = RawPayloadSerializer ()
95
100
binding_serializer = HTTPRequestBindingSerializer (
96
101
payload_serializer ,
@@ -113,11 +118,11 @@ def begin_struct(self, schema: Schema) -> Iterator[ShapeSerializer]:
113
118
)
114
119
yield binding_serializer
115
120
else :
116
- if binding_matcher .event_stream_member is not None :
117
- content_type = "application/vnd.amazon.eventstream"
118
121
payload = BytesIO ()
119
122
payload_serializer = self ._payload_codec .create_serializer (payload )
120
123
if binding_matcher .should_write_body (self ._omit_empty_payload ):
124
+ if binding_matcher .event_stream_member is not None :
125
+ content_type = "application/vnd.amazon.eventstream"
121
126
with payload_serializer .begin_struct (schema ) as body_serializer :
122
127
binding_serializer = HTTPRequestBindingSerializer (
123
128
body_serializer ,
@@ -127,6 +132,7 @@ def begin_struct(self, schema: Schema) -> Iterator[ShapeSerializer]:
127
132
)
128
133
yield binding_serializer
129
134
else :
135
+ content_type = None
130
136
binding_serializer = HTTPRequestBindingSerializer (
131
137
payload_serializer ,
132
138
self ._http_trait .path ,
@@ -140,9 +146,9 @@ def begin_struct(self, schema: Schema) -> Iterator[ShapeSerializer]:
140
146
) is not None and not iscoroutinefunction (seek ):
141
147
seek (0 )
142
148
143
- # TODO: conditional on empty-ness and based on the protocol
144
149
headers = binding_serializer .header_serializer .headers
145
- headers .append (("content-type" , content_type ))
150
+ if content_type is not None :
151
+ headers .append (("content-type" , content_type ))
146
152
147
153
self .result = _HTTPRequest (
148
154
method = self ._http_trait .method ,
@@ -228,16 +234,25 @@ def begin_struct(self, schema: Schema) -> Iterator[ShapeSerializer]:
228
234
payload : Any
229
235
binding_serializer : HTTPResponseBindingSerializer
230
236
237
+ content_type : str | None = self ._payload_codec .media_type
231
238
binding_matcher = ResponseBindingMatcher (schema )
232
239
if (payload_member := binding_matcher .payload_member ) is not None :
233
240
if payload_member .shape_type in (ShapeType .BLOB , ShapeType .STRING ):
241
+ if (media_type := payload_member .get_trait (MediaTypeTrait )) is not None :
242
+ content_type = media_type .value
243
+ elif payload_member .shape_type is ShapeType .BLOB :
244
+ content_type = "application/octet-stream"
245
+ else :
246
+ content_type = "text/plain"
234
247
payload_serializer = RawPayloadSerializer ()
235
248
binding_serializer = HTTPResponseBindingSerializer (
236
249
payload_serializer , binding_matcher
237
250
)
238
251
yield binding_serializer
239
252
payload = payload_serializer .payload
240
253
else :
254
+ if (media_type := payload_member .get_trait (MediaTypeTrait )) is not None :
255
+ content_type = media_type .value
241
256
payload = BytesIO ()
242
257
payload_serializer = self ._payload_codec .create_serializer (payload )
243
258
binding_serializer = HTTPResponseBindingSerializer (
@@ -248,12 +263,15 @@ def begin_struct(self, schema: Schema) -> Iterator[ShapeSerializer]:
248
263
payload = BytesIO ()
249
264
payload_serializer = self ._payload_codec .create_serializer (payload )
250
265
if binding_matcher .should_write_body (self ._omit_empty_payload ):
266
+ if binding_matcher .event_stream_member is not None :
267
+ content_type = "application/vnd.amazon.eventstream"
251
268
with payload_serializer .begin_struct (schema ) as body_serializer :
252
269
binding_serializer = HTTPResponseBindingSerializer (
253
270
body_serializer , binding_matcher
254
271
)
255
272
yield binding_serializer
256
273
else :
274
+ content_type = None
257
275
binding_serializer = HTTPResponseBindingSerializer (
258
276
payload_serializer ,
259
277
binding_matcher ,
@@ -265,6 +283,10 @@ def begin_struct(self, schema: Schema) -> Iterator[ShapeSerializer]:
265
283
) is not None and not iscoroutinefunction (seek ):
266
284
seek (0 )
267
285
286
+ headers = binding_serializer .header_serializer .headers
287
+ if content_type is not None :
288
+ headers .append (("content-type" , content_type ))
289
+
268
290
status = binding_serializer .response_code_serializer .response_code
269
291
if status is None :
270
292
if binding_matcher .response_status > 0 :
0 commit comments