Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(dynamite): Support default responses #1051

Merged
merged 2 commits into from
Oct 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 8 additions & 10 deletions packages/dynamite/dynamite/lib/src/builder/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -190,14 +190,10 @@ Iterable<Method> buildTags(
]..sort(sortRequiredParameters);
final name = toDartName(filterMethodName(operationName, tag ?? ''));

var responses = <openapi.Response, List<int>>{};
var responses = <openapi.Response, List<String>>{};
if (operation.responses != null) {
for (final responseEntry in operation.responses!.entries) {
final statusCode = int.tryParse(responseEntry.key);
if (statusCode == null) {
print('Default responses are not supported right now. Skipping it for $operationName');
continue;
}
final statusCode = responseEntry.key;
final response = responseEntry.value;

responses[response] ??= [];
Expand Down Expand Up @@ -225,7 +221,6 @@ Iterable<Method> buildTags(
''');

buildAuthCheck(
responses,
pathEntry,
operation,
spec,
Expand Down Expand Up @@ -357,8 +352,12 @@ final _uri = Uri(path: _path, queryParameters: _queryParameters.isNotEmpty ? _qu
''');

if (responses.values.isNotEmpty) {
final codes = statusCodes.join(',');
code.writeln('const {$codes},');
if (statusCodes.contains('default')) {
code.writeln('null,');
} else {
final codes = statusCodes.join(',');
code.writeln('const {$codes},');
}
}

code.writeln('''
Expand Down Expand Up @@ -478,7 +477,6 @@ Iterable<String> buildPatternCheck(
}

Iterable<String> buildAuthCheck(
final Map<openapi.Response, List<int>> responses,
final MapEntry<String, openapi.PathItem> pathEntry,
final openapi.Operation operation,
final openapi.OpenAPI spec,
Expand Down
245 changes: 245 additions & 0 deletions packages/dynamite/dynamite_end_to_end_test/lib/responses.openapi.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,245 @@
// ignore_for_file: camel_case_types
// ignore_for_file: discarded_futures
// ignore_for_file: public_member_api_docs
// ignore_for_file: unreachable_switch_case
import 'dart:typed_data';

import 'package:built_value/serializer.dart';
import 'package:built_value/standard_json_plugin.dart';
import 'package:dynamite_runtime/built_value.dart';
import 'package:dynamite_runtime/http_client.dart';
import 'package:meta/meta.dart';
import 'package:universal_io/io.dart';

class Client extends DynamiteClient {
Client(
super.baseURL, {
super.baseHeaders,
super.userAgent,
super.httpClient,
super.cookieJar,
});

Client.fromClient(final DynamiteClient client)
: super(
client.baseURL,
baseHeaders: client.baseHeaders,
httpClient: client.httpClient,
cookieJar: client.cookieJar,
authentications: client.authentications,
);

/// Returns a [Future] containing a [DynamiteResponse] with the status code, deserialized body and headers.
/// Throws a [DynamiteApiException] if the API call does not return an expected status code.
///
/// Status codes:
/// * default
///
/// See:
/// * [$getRaw] for an experimental operation that returns a [DynamiteRawResponse] that can be serialized.
Future<DynamiteResponse<String, void>> $get() async {
final rawResponse = $getRaw();

return rawResponse.future;
}

/// This method and the response it returns is experimental. The API might change without a major version bump.
///
/// Returns a [Future] containing a [DynamiteRawResponse] with the raw [HttpClientResponse] and serialization helpers.
/// Throws a [DynamiteApiException] if the API call does not return an expected status code.
///
/// Status codes:
/// * default
///
/// See:
/// * [$get] for an operation that returns a [DynamiteResponse] with a stable API.
@experimental
DynamiteRawResponse<String, void> $getRaw() {
final queryParameters = <String, dynamic>{};
final headers = <String, String>{
'Accept': 'application/json',
};
Uint8List? body;

const path = '/';
final uri = Uri(path: path, queryParameters: queryParameters.isNotEmpty ? queryParameters : null);

return DynamiteRawResponse<String, void>(
response: executeRequest(
'get',
uri,
headers,
body,
null,
),
bodyType: const FullType(String),
headersType: null,
serializers: _jsonSerializers,
);
}

/// Returns a [Future] containing a [DynamiteResponse] with the status code, deserialized body and headers.
/// Throws a [DynamiteApiException] if the API call does not return an expected status code.
///
/// Status codes:
/// * 200
/// * 201
///
/// See:
/// * [putRaw] for an experimental operation that returns a [DynamiteRawResponse] that can be serialized.
Future<DynamiteResponse<String, void>> put() async {
final rawResponse = putRaw();

return rawResponse.future;
}

/// This method and the response it returns is experimental. The API might change without a major version bump.
///
/// Returns a [Future] containing a [DynamiteRawResponse] with the raw [HttpClientResponse] and serialization helpers.
/// Throws a [DynamiteApiException] if the API call does not return an expected status code.
///
/// Status codes:
/// * 200
/// * 201
provokateurin marked this conversation as resolved.
Show resolved Hide resolved
///
/// See:
/// * [put] for an operation that returns a [DynamiteResponse] with a stable API.
@experimental
DynamiteRawResponse<String, void> putRaw() {
final queryParameters = <String, dynamic>{};
final headers = <String, String>{
'Accept': 'application/json',
};
Uint8List? body;

const path = '/';
final uri = Uri(path: path, queryParameters: queryParameters.isNotEmpty ? queryParameters : null);

return DynamiteRawResponse<String, void>(
response: executeRequest(
'put',
uri,
headers,
body,
const {200},
),
bodyType: const FullType(String),
headersType: null,
serializers: _jsonSerializers,
);
}

/// Returns a [Future] containing a [DynamiteResponse] with the status code, deserialized body and headers.
/// Throws a [DynamiteApiException] if the API call does not return an expected status code.
///
/// Status codes:
/// * default
/// * 200
/// * 201
///
/// See:
/// * [postRaw] for an experimental operation that returns a [DynamiteRawResponse] that can be serialized.
Future<DynamiteResponse<String, void>> post() async {
final rawResponse = postRaw();

return rawResponse.future;
}

/// This method and the response it returns is experimental. The API might change without a major version bump.
///
/// Returns a [Future] containing a [DynamiteRawResponse] with the raw [HttpClientResponse] and serialization helpers.
/// Throws a [DynamiteApiException] if the API call does not return an expected status code.
///
/// Status codes:
/// * default
/// * 200
/// * 201
///
/// See:
/// * [post] for an operation that returns a [DynamiteResponse] with a stable API.
@experimental
DynamiteRawResponse<String, void> postRaw() {
final queryParameters = <String, dynamic>{};
final headers = <String, String>{
'Accept': 'application/json',
};
Uint8List? body;

const path = '/';
final uri = Uri(path: path, queryParameters: queryParameters.isNotEmpty ? queryParameters : null);

return DynamiteRawResponse<String, void>(
response: executeRequest(
'post',
uri,
headers,
body,
null,
),
bodyType: const FullType(String),
headersType: null,
serializers: _jsonSerializers,
);
}

/// Returns a [Future] containing a [DynamiteResponse] with the status code, deserialized body and headers.
/// Throws a [DynamiteApiException] if the API call does not return an expected status code.
///
/// Status codes:
/// * 200
/// * 201
///
/// See:
/// * [patchRaw] for an experimental operation that returns a [DynamiteRawResponse] that can be serialized.
Future<DynamiteResponse<String, void>> patch() async {
final rawResponse = patchRaw();

return rawResponse.future;
}

/// This method and the response it returns is experimental. The API might change without a major version bump.
///
/// Returns a [Future] containing a [DynamiteRawResponse] with the raw [HttpClientResponse] and serialization helpers.
/// Throws a [DynamiteApiException] if the API call does not return an expected status code.
///
/// Status codes:
/// * 200
/// * 201
///
/// See:
/// * [patch] for an operation that returns a [DynamiteResponse] with a stable API.
@experimental
DynamiteRawResponse<String, void> patchRaw() {
final queryParameters = <String, dynamic>{};
final headers = <String, String>{
'Accept': 'application/json',
};
Uint8List? body;

const path = '/';
final uri = Uri(path: path, queryParameters: queryParameters.isNotEmpty ? queryParameters : null);

return DynamiteRawResponse<String, void>(
response: executeRequest(
'patch',
uri,
headers,
body,
const {200, 201},
),
bodyType: const FullType(String),
headersType: null,
serializers: _jsonSerializers,
);
}
}

// coverage:ignore-start
final Serializers _serializers = Serializers().toBuilder().build();

final Serializers _jsonSerializers = (_serializers.toBuilder()
..add(DynamiteDoubleSerializer())
..addPlugin(StandardJsonPlugin())
..addPlugin(const ContentStringPlugin()))
.build();
// coverage:ignore-end
Loading