Browse Source

Merge pull request #1051 from nextcloud/feature/dynamite/default-responses

feat(dynamite): Support default responses
pull/1031/head
Kate 1 year ago committed by GitHub
parent
commit
15454260e3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 18
      packages/dynamite/dynamite/lib/src/builder/client.dart
  2. 245
      packages/dynamite/dynamite_end_to_end_test/lib/responses.openapi.dart
  3. 108
      packages/dynamite/dynamite_end_to_end_test/lib/responses.openapi.json

18
packages/dynamite/dynamite/lib/src/builder/client.dart

@ -190,14 +190,10 @@ Iterable<Method> buildTags(
]..sort(sortRequiredParameters); ]..sort(sortRequiredParameters);
final name = toDartName(filterMethodName(operationName, tag ?? '')); final name = toDartName(filterMethodName(operationName, tag ?? ''));
var responses = <openapi.Response, List<int>>{}; var responses = <openapi.Response, List<String>>{};
if (operation.responses != null) { if (operation.responses != null) {
for (final responseEntry in operation.responses!.entries) { for (final responseEntry in operation.responses!.entries) {
final statusCode = int.tryParse(responseEntry.key); final statusCode = responseEntry.key;
if (statusCode == null) {
print('Default responses are not supported right now. Skipping it for $operationName');
continue;
}
final response = responseEntry.value; final response = responseEntry.value;
responses[response] ??= []; responses[response] ??= [];
@ -225,7 +221,6 @@ Iterable<Method> buildTags(
'''); ''');
buildAuthCheck( buildAuthCheck(
responses,
pathEntry, pathEntry,
operation, operation,
spec, spec,
@ -357,8 +352,12 @@ final _uri = Uri(path: _path, queryParameters: _queryParameters.isNotEmpty ? _qu
'''); ''');
if (responses.values.isNotEmpty) { if (responses.values.isNotEmpty) {
final codes = statusCodes.join(','); if (statusCodes.contains('default')) {
code.writeln('const {$codes},'); code.writeln('null,');
} else {
final codes = statusCodes.join(',');
code.writeln('const {$codes},');
}
} }
code.writeln(''' code.writeln('''
@ -478,7 +477,6 @@ Iterable<String> buildPatternCheck(
} }
Iterable<String> buildAuthCheck( Iterable<String> buildAuthCheck(
final Map<openapi.Response, List<int>> responses,
final MapEntry<String, openapi.PathItem> pathEntry, final MapEntry<String, openapi.PathItem> pathEntry,
final openapi.Operation operation, final openapi.Operation operation,
final openapi.OpenAPI spec, final openapi.OpenAPI spec,

245
packages/dynamite/dynamite_end_to_end_test/lib/responses.openapi.dart

@ -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
///
/// 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

108
packages/dynamite/dynamite_end_to_end_test/lib/responses.openapi.json

@ -0,0 +1,108 @@
{
"openapi": "3.1.0",
"info": {
"title": "responses test",
"version": "0.0.1"
},
"paths": {
"/": {
"get": {
"responses": {
"default": {
"description": "",
"content": {
"application/json": {
"schema": {
"type": "string"
}
}
}
}
}
},
"post": {
"responses": {
"default": {
"description": "",
"content": {
"application/json": {
"schema": {
"type": "string"
}
}
}
},
"200": {
"description": "",
"content": {
"application/json": {
"schema": {
"type": "string"
}
}
}
},
"201": {
"description": "",
"content": {
"application/json": {
"schema": {
"type": "integer"
}
}
}
}
}
},
"put": {
"responses": {
"200": {
"description": "",
"content": {
"application/json": {
"schema": {
"type": "string"
}
}
}
},
"201": {
"description": "",
"content": {
"application/json": {
"schema": {
"type": "integer"
}
}
}
}
}
},
"patch": {
"responses": {
"200": {
"description": "",
"content": {
"application/json": {
"schema": {
"type": "string"
}
}
}
},
"201": {
"description": "",
"content": {
"application/json": {
"schema": {
"type": "string"
}
}
}
}
}
}
}
},
"tags": []
}
Loading…
Cancel
Save