@@ -48,18 +48,24 @@ MediaType mediaTypeFromRequest(Request request, {String? requiredMimeType}) {
48
48
return value;
49
49
}
50
50
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
+ }
63
69
}
64
70
}
65
71
@@ -89,20 +95,15 @@ enum SupportedContentTypes {
89
95
90
96
return (
91
97
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 >>(
102
101
< int > [],
103
102
(previous, element) => previous..addAll (element),
104
103
),
105
- };
104
+ },
105
+ );
106
+ }
106
107
}
107
108
108
109
const contentTypeHeader = 'Content-Type' ;
0 commit comments