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. 14
      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(
result.serialize(_toDartName(parameter.name)),
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') {
code.write('if (${_toDartName(parameter.name)} != $defaultValueCode) {');
@ -654,6 +656,7 @@ class OpenAPIBuilder implements Builder {
);
switch (mimeType) {
case 'application/json':
case 'application/x-www-form-urlencoded':
final nullable = _isParameterNullable(
operation.requestBody!.required,
mediaType.schema?.default_,
@ -672,7 +675,7 @@ class OpenAPIBuilder implements Builder {
code.write('if (${_toDartName(result.name)} != null) {');
}
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) {
code.write('}');
@ -773,6 +776,10 @@ class OpenAPIBuilder implements Builder {
dataType = 'Uint8List';
dataValue = 'response.body';
break;
case 'text/plain':
dataType = 'String';
dataValue = 'utf8.decode(response.body)';
break;
default:
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;
@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()';
@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';
@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
String deserialize(final String object) => '$name.fromValue($object as ${subType.name})';

14
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()';
@override
String encode(final String object, {final bool onlyChildren = false}) {
String encode(
final String object, {
final bool onlyChildren = false,
final String? mimeType,
}) {
if (onlyChildren) {
return '$object.map((final e) => ${subType.encode('e')}).toList()';
}
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

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

@ -12,7 +12,20 @@ class TypeResultMap extends TypeResult {
String serialize(final String object) => object;
@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
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
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
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 encode(final String object, {final bool onlyChildren = false});
String encode(
final String object, {
final bool onlyChildren = false,
final String? mimeType,
});
}

Loading…
Cancel
Save