Skip to content

Commit

Permalink
refactor(dynamite_runtime): rework DynamiteRawResponse
Browse files Browse the repository at this point in the history
Signed-off-by: Nikolas Rimikis <leptopoda@users.noreply.github.com>
  • Loading branch information
Leptopoda committed Mar 3, 2024
1 parent 3c38e99 commit e7f2f35
Show file tree
Hide file tree
Showing 36 changed files with 4,650 additions and 5,239 deletions.
98 changes: 45 additions & 53 deletions packages/dynamite/dynamite/example/lib/petstore.openapi.dart
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,12 @@ class $Client extends _i1.DynamiteClient {
BuiltList<String>? tags,
int? limit,
}) async {
final rawResponse = findPetsRaw(
final _rawResponse = await findPetsRaw(
tags: tags,
limit: limit,
);

return rawResponse.future;
return _i1.DynamiteResponse.fromRawResponse(_rawResponse);
}

/// Returns all pets from the system that the user has access to.
Expand All @@ -115,10 +115,10 @@ class $Client extends _i1.DynamiteClient {
/// See:
/// * [findPets] for an operation that returns a `DynamiteResponse` with a stable API.
@_i2.experimental
_i1.DynamiteRawResponse<BuiltList<Pet>, void> findPetsRaw({
Future<_i1.DynamiteRawResponse<BuiltList<Pet>, void>> findPetsRaw({
BuiltList<String>? tags,
int? limit,
}) {
}) async {
final _parameters = <String, dynamic>{};
const _headers = <String, String>{'Accept': 'application/json'};

Expand All @@ -129,17 +129,15 @@ class $Client extends _i1.DynamiteClient {
_parameters['limit'] = $limit;

final _path = _i3.UriTemplate('/pets{?tags*,limit*}').expand(_parameters);
return _i1.DynamiteRawResponse<BuiltList<Pet>, void>(
response: executeRequest(
'get',
_path,
headers: _headers,
validStatuses: const {200},
),
bodyType: const FullType(BuiltList, [FullType(Pet)]),
headersType: null,
serializers: _$jsonSerializers,
final _response = await executeRequest(
'get',
_path,
headers: _headers,
validStatuses: const {200},
);

final _serializer = $findPets_Serializer();
return _i1.ResponseConverter<BuiltList<Pet>, void>(_serializer).convert(_response);
}

/// Builds a serializer to parse the response of `$addPet_Request`.
Expand All @@ -162,11 +160,11 @@ class $Client extends _i1.DynamiteClient {
/// See:
/// * [addPetRaw] for an experimental operation that returns a `DynamiteRawResponse` that can be serialized.
Future<_i1.DynamiteResponse<Pet, void>> addPet({required NewPet newPet}) async {
final rawResponse = addPetRaw(
final _rawResponse = await addPetRaw(
newPet: newPet,
);

return rawResponse.future;
return _i1.DynamiteResponse.fromRawResponse(_rawResponse);
}

/// Creates a new pet in the store. Duplicates are allowed.
Expand All @@ -183,25 +181,23 @@ class $Client extends _i1.DynamiteClient {
/// See:
/// * [addPet] for an operation that returns a `DynamiteResponse` with a stable API.
@_i2.experimental
_i1.DynamiteRawResponse<Pet, void> addPetRaw({required NewPet newPet}) {
Future<_i1.DynamiteRawResponse<Pet, void>> addPetRaw({required NewPet newPet}) async {
final _headers = <String, String>{'Accept': 'application/json'};
Uint8List? _body;

_headers['Content-Type'] = 'application/json';
_body = utf8.encode(json.encode(_$jsonSerializers.serialize(newPet, specifiedType: const FullType(NewPet))));
const _path = '/pets';
return _i1.DynamiteRawResponse<Pet, void>(
response: executeRequest(
'post',
_path,
headers: _headers,
body: _body,
validStatuses: const {200},
),
bodyType: const FullType(Pet),
headersType: null,
serializers: _$jsonSerializers,
final _response = await executeRequest(
'post',
_path,
headers: _headers,
body: _body,
validStatuses: const {200},
);

final _serializer = $addPet_Serializer();
return _i1.ResponseConverter<Pet, void>(_serializer).convert(_response);
}

/// Builds a serializer to parse the response of `$findPetById_Request`.
Expand All @@ -227,11 +223,11 @@ class $Client extends _i1.DynamiteClient {
/// See:
/// * [findPetByIdRaw] for an experimental operation that returns a `DynamiteRawResponse` that can be serialized.
Future<_i1.DynamiteResponse<Pet, void>> findPetById({required int id}) async {
final rawResponse = findPetByIdRaw(
final _rawResponse = await findPetByIdRaw(
id: id,
);

return rawResponse.future;
return _i1.DynamiteResponse.fromRawResponse(_rawResponse);
}

/// Returns a user based on a single ID, if the user does not have access to the pet.
Expand All @@ -251,25 +247,23 @@ class $Client extends _i1.DynamiteClient {
/// See:
/// * [findPetById] for an operation that returns a `DynamiteResponse` with a stable API.
@_i2.experimental
_i1.DynamiteRawResponse<Pet, void> findPetByIdRaw({required int id}) {
Future<_i1.DynamiteRawResponse<Pet, void>> findPetByIdRaw({required int id}) async {
final _parameters = <String, dynamic>{};
const _headers = <String, String>{'Accept': 'application/json'};

final $id = _$jsonSerializers.serialize(id, specifiedType: const FullType(int));
_parameters['id'] = $id;

final _path = _i3.UriTemplate('/pets/{id}').expand(_parameters);
return _i1.DynamiteRawResponse<Pet, void>(
response: executeRequest(
'get',
_path,
headers: _headers,
validStatuses: const {200},
),
bodyType: const FullType(Pet),
headersType: null,
serializers: _$jsonSerializers,
final _response = await executeRequest(
'get',
_path,
headers: _headers,
validStatuses: const {200},
);

final _serializer = $findPetById_Serializer();
return _i1.ResponseConverter<Pet, void>(_serializer).convert(_response);
}

/// Builds a serializer to parse the response of `$deletePet_Request`.
Expand All @@ -295,11 +289,11 @@ class $Client extends _i1.DynamiteClient {
/// See:
/// * [deletePetRaw] for an experimental operation that returns a `DynamiteRawResponse` that can be serialized.
Future<_i1.DynamiteResponse<void, void>> deletePet({required int id}) async {
final rawResponse = deletePetRaw(
final _rawResponse = await deletePetRaw(
id: id,
);

return rawResponse.future;
return _i1.DynamiteResponse.fromRawResponse(_rawResponse);
}

/// deletes a single pet based on the ID supplied.
Expand All @@ -319,23 +313,21 @@ class $Client extends _i1.DynamiteClient {
/// See:
/// * [deletePet] for an operation that returns a `DynamiteResponse` with a stable API.
@_i2.experimental
_i1.DynamiteRawResponse<void, void> deletePetRaw({required int id}) {
Future<_i1.DynamiteRawResponse<void, void>> deletePetRaw({required int id}) async {
final _parameters = <String, dynamic>{};

final $id = _$jsonSerializers.serialize(id, specifiedType: const FullType(int));
_parameters['id'] = $id;

final _path = _i3.UriTemplate('/pets/{id}').expand(_parameters);
return _i1.DynamiteRawResponse<void, void>(
response: executeRequest(
'delete',
_path,
validStatuses: const {204},
),
bodyType: null,
headersType: null,
serializers: _$jsonSerializers,
final _response = await executeRequest(
'delete',
_path,
validStatuses: const {204},
);

final _serializer = $deletePet_Serializer();
return _i1.ResponseConverter<void, void>(_serializer).convert(_response);
}
}

Expand Down
62 changes: 37 additions & 25 deletions packages/dynamite/dynamite/lib/src/builder/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -362,19 +362,22 @@ ${allocate(returnType)}(
..symbol = 'Future'
..types.add(responseType),
)
..body = Code('''
final rawResponse = ${name}Raw(
..body = Code.scope(
(allocate) => '''
final _rawResponse = await ${name}Raw(
$rawParameters
);
return rawResponse.future;
''');
return ${allocate(responseType)}.fromRawResponse(_rawResponse);
''',
);
});

yield Method(
(b) {
b
..name = '${name}Raw'
..modifier = MethodModifier.async
..docs.addAll(operation.formattedDescription(name, isRawRequest: true))
..annotations.add(refer('experimental', 'package:meta/meta.dart'));

Expand All @@ -384,9 +387,15 @@ return rawResponse.future;

b
..optionalParameters.addAll(operationParameters)
..returns = refer(
'DynamiteRawResponse<${bodyType.name}, ${headersType.name}>',
'package:dynamite_runtime/http_client.dart',
..returns = TypeReference(
(b) => b
..symbol = 'Future'
..types.add(
refer(
'DynamiteRawResponse<${bodyType.name}, ${headersType.name}>',
'package:dynamite_runtime/http_client.dart',
),
),
)
..body = Code.scope((allocate) {
final hasUriParameters = pathParameters.isNotEmpty;
Expand Down Expand Up @@ -458,26 +467,29 @@ return rawResponse.future;
buildUrlPath(pathEntry.key, parameters, code, state.emitter.allocator.allocate);

final statusCodes = responseEntry.value;
final returnValue = refer(
'DynamiteRawResponse<${bodyType.name}, ${headersType.name}>',
'package:dynamite_runtime/http_client.dart',
).newInstance(const [], {
'response': refer(client).property('executeRequest').call([
literalString(httpMethod),
refer('_path'),
], {
if (acceptHeader.isNotEmpty || hasHeaderParameters || hasAuthentication || hasContentEncoding)
'headers': refer('_headers'),
if (operation.requestBody != null) 'body': refer('_body'),
if (responses.values.isNotEmpty && !statusCodes.contains('default'))
'validStatuses': literalConstSet(statusCodes),
}),
'bodyType': refer(bodyType.fullType ?? 'null'),
'headersType': refer(headersType.fullType ?? 'null'),
'serializers': refer(r'_$jsonSerializers'),
final requestExpression = refer(client).property('executeRequest').call([
literalString(httpMethod),
refer('_path'),
], {
if (acceptHeader.isNotEmpty || hasHeaderParameters || hasAuthentication || hasContentEncoding)
'headers': refer('_headers'),
if (operation.requestBody != null) 'body': refer('_body'),
if (responses.values.isNotEmpty && !statusCodes.contains('default'))
'validStatuses': literalConstSet(statusCodes),
});

code.writeln(returnValue.returned.statement.accept(state.emitter));
final responseConverterType = refer(
'ResponseConverter<${bodyType.name}, ${headersType.name}>',
'package:dynamite_runtime/http_client.dart',
);

code.write('''
final _response = ${requestExpression.awaited.accept(state.emitter)};
final _serializer = \$${name}_Serializer();
return ${allocate(responseConverterType)}(_serializer).convert(_response);
''');

return code.toString();
});
},
Expand Down

0 comments on commit e7f2f35

Please sign in to comment.