@@ -48,18 +48,24 @@ MediaType mediaTypeFromRequest(Request request, {String? requiredMimeType}) {
4848 return value;
4949}
5050
51- Future <Object ?> decodeJson (Stream <List <int >> data) async {
52- try {
53- final value = await utf8.decoder.bind (data).transform (json.decoder).single;
54- return value;
55- } on FormatException catch (e, stackTrace) {
56- // https://github.com/GoogleCloudPlatform/functions-framework#http-status-codes
57- throw BadRequestException (
58- 400 ,
59- 'Could not parse the request body as JSON.' ,
60- innerError: e,
61- innerStack: stackTrace,
62- );
51+ extension RequestExt on Request {
52+ Future <Object ?> decodeJson () async {
53+ try {
54+ final value = await (encoding ?? utf8)
55+ .decoder
56+ .bind (read ())
57+ .transform (json.decoder)
58+ .single;
59+ return value;
60+ } on FormatException catch (e, stackTrace) {
61+ // https://github.com/GoogleCloudPlatform/functions-framework#http-status-codes
62+ throw BadRequestException (
63+ 400 ,
64+ 'Could not parse the request body as JSON.' ,
65+ innerError: e,
66+ innerStack: stackTrace,
67+ );
68+ }
6369 }
6470}
6571
@@ -89,20 +95,15 @@ enum SupportedContentTypes {
8995
9096 return (
9197 mimeType: type,
92- data: await supportedType._decode (request.read ()),
93- );
94- }
95-
96- Future <Object ?> _decode (
97- Stream <List <int >> data,
98- ) async =>
99- switch (this ) {
100- json => await decodeJson (data),
101- protobuf => await data.fold <List <int >>(
98+ data: switch (supportedType) {
99+ json => await request.decodeJson (),
100+ protobuf => await request.read ().fold <List <int >>(
102101 < int > [],
103102 (previous, element) => previous..addAll (element),
104103 ),
105- };
104+ },
105+ );
106+ }
106107}
107108
108109const contentTypeHeader = 'Content-Type' ;
0 commit comments