Browse Source

dynamite: Implement urlencoded requests

pull/163/head
jld3103 2 years ago
parent
commit
97f226bbed
No known key found for this signature in database
GPG Key ID: 9062417B9E8EB7B3
  1. 9
      packages/dynamite/lib/src/openapi_builder.dart
  2. 6
      packages/dynamite/lib/src/type_result/base.dart
  3. 7
      packages/dynamite/lib/src/type_result/enum.dart
  4. 16
      packages/dynamite/lib/src/type_result/list.dart
  5. 15
      packages/dynamite/lib/src/type_result/map.dart
  6. 15
      packages/dynamite/lib/src/type_result/object.dart
  7. 6
      packages/dynamite/lib/src/type_result/type_result.dart

9
packages/dynamite/lib/src/openapi_builder.dart

@ -606,6 +606,8 @@ class OpenAPIBuilder implements Builder {
final value = result.encode( final value = result.encode(
result.serialize(_toDartName(parameter.name)), result.serialize(_toDartName(parameter.name)),
onlyChildren: isPlainList && parameter.in_ == 'query', onlyChildren: isPlainList && parameter.in_ == 'query',
// Objects inside the query always have to be interpreted in some way
mimeType: 'application/json',
); );
if (defaultValueCode != null && parameter.in_ != 'path') { if (defaultValueCode != null && parameter.in_ != 'path') {
code.write('if (${_toDartName(parameter.name)} != $defaultValueCode) {'); code.write('if (${_toDartName(parameter.name)} != $defaultValueCode) {');
@ -654,6 +656,7 @@ class OpenAPIBuilder implements Builder {
); );
switch (mimeType) { switch (mimeType) {
case 'application/json': case 'application/json':
case 'application/x-www-form-urlencoded':
final nullable = _isParameterNullable( final nullable = _isParameterNullable(
operation.requestBody!.required, operation.requestBody!.required,
mediaType.schema?.default_, mediaType.schema?.default_,
@ -672,7 +675,7 @@ class OpenAPIBuilder implements Builder {
code.write('if (${_toDartName(result.name)} != null) {'); code.write('if (${_toDartName(result.name)} != null) {');
} }
code.write( code.write(
'body = Uint8List.fromList(utf8.encode(${result.encode(result.serialize(_toDartName(result.name)))}));', 'body = Uint8List.fromList(utf8.encode(${result.encode(result.serialize(_toDartName(result.name)), mimeType: mimeType)}));',
); );
if (nullable) { if (nullable) {
code.write('}'); code.write('}');
@ -773,6 +776,10 @@ class OpenAPIBuilder implements Builder {
dataType = 'Uint8List'; dataType = 'Uint8List';
dataValue = 'response.body'; dataValue = 'response.body';
break; break;
case 'text/plain':
dataType = 'String';
dataValue = 'utf8.decode(response.body)';
break;
default: default:
throw Exception('Can not parse mime type "$mimeType"'); throw Exception('Can not parse mime type "$mimeType"');
} }

6
packages/dynamite/lib/src/type_result/base.dart

@ -7,7 +7,11 @@ class TypeResultBase extends TypeResult {
String serialize(final String object) => object; String serialize(final String object) => object;
@override @override
String encode(final String object, {final bool onlyChildren = false}) => String encode(
final String object, {
final bool onlyChildren = false,
final String? mimeType,
}) =>
name == 'String' ? object : '$object.toString()'; name == 'String' ? object : '$object.toString()';
@override @override

7
packages/dynamite/lib/src/type_result/enum.dart

@ -12,7 +12,12 @@ class TypeResultEnum extends TypeResult {
String serialize(final String object) => '$object.value'; String serialize(final String object) => '$object.value';
@override @override
String encode(final String object, {final bool onlyChildren = false}) => subType.encode(object); String encode(
final String object, {
final bool onlyChildren = false,
final String? mimeType,
}) =>
subType.encode(object);
@override @override
String deserialize(final String object) => '$name.fromValue($object as ${subType.name})'; String deserialize(final String object) => '$name.fromValue($object as ${subType.name})';

16
packages/dynamite/lib/src/type_result/list.dart

@ -14,11 +14,23 @@ class TypeResultList extends TypeResult {
String serialize(final String object) => '$object.map((final e) => ${subType.serialize('e')}).toList()'; String serialize(final String object) => '$object.map((final e) => ${subType.serialize('e')}).toList()';
@override @override
String encode(final String object, {final bool onlyChildren = false}) { String encode(
final String object, {
final bool onlyChildren = false,
final String? mimeType,
}) {
if (onlyChildren) { if (onlyChildren) {
return '$object.map((final e) => ${subType.encode('e')}).toList()'; return '$object.map((final e) => ${subType.encode('e')}).toList()';
} }
return 'json.encode($object)';
switch (mimeType) {
case 'application/json':
return 'json.encode($object)';
case 'application/x-www-form-urlencoded':
return 'Uri(queryParameters: $object).query';
default:
throw Exception('Can not encode mime type "$mimeType"');
}
} }
@override @override

15
packages/dynamite/lib/src/type_result/map.dart

@ -12,7 +12,20 @@ class TypeResultMap extends TypeResult {
String serialize(final String object) => object; String serialize(final String object) => object;
@override @override
String encode(final String object, {final bool onlyChildren = false}) => 'json.encode($object)'; String encode(
final String object, {
final bool onlyChildren = false,
final String? mimeType,
}) {
switch (mimeType) {
case 'application/json':
return 'json.encode($object)';
case 'application/x-www-form-urlencoded':
return 'Uri(queryParameters: $object).query';
default:
throw Exception('Can not encode mime type "$mimeType"');
}
}
@override @override
String deserialize(final String object) => '($object as Map<String, ${subType.name}>)'; String deserialize(final String object) => '($object as Map<String, ${subType.name}>)';

15
packages/dynamite/lib/src/type_result/object.dart

@ -17,7 +17,20 @@ class TypeResultObject extends TypeResult {
} }
@override @override
String encode(final String object, {final bool onlyChildren = false}) => 'json.encode($object)'; String encode(
final String object, {
final bool onlyChildren = false,
final String? mimeType,
}) {
switch (mimeType) {
case 'application/json':
return 'json.encode($object)';
case 'application/x-www-form-urlencoded':
return 'Uri(queryParameters: $object).query';
default:
throw Exception('Can not encode mime type "$mimeType"');
}
}
@override @override
String deserialize(final String object) { String deserialize(final String object) {

6
packages/dynamite/lib/src/type_result/type_result.dart

@ -11,5 +11,9 @@ abstract class TypeResult {
String decode(final String object); String decode(final String object);
String encode(final String object, {final bool onlyChildren = false}); String encode(
final String object, {
final bool onlyChildren = false,
final String? mimeType,
});
} }

Loading…
Cancel
Save