Skip to content

Commit

Permalink
all async functions in dart codegen
Browse files Browse the repository at this point in the history
  • Loading branch information
joshmossas committed Jun 2, 2024
1 parent b72115a commit 579a0d3
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 58 deletions.
10 changes: 6 additions & 4 deletions languages/dart/dart-client/lib/request.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'dart:async';
import 'dart:convert';

import 'package:arri_client/errors.dart';
Expand Down Expand Up @@ -25,7 +26,7 @@ Future<http.Response> arriRequest(
http.Client? httpClient,
HttpMethod method = HttpMethod.get,
Map<String, dynamic>? params,
Map<String, String> Function()? headers,
FutureOr<Map<String, String>> Function()? headers,

/// manually specify specific url query parameters
Map<String, String>? query,
Expand All @@ -40,7 +41,8 @@ Future<http.Response> arriRequest(
"""{"statusCode": 400,"statusMessage":"$defaultErrorMsg"}""",
400,
);
final finalHeaders = {...headers?.call() ?? {}};

final finalHeaders = await headers?.call() ?? {};
if (clientVersion != null && clientVersion.isNotEmpty) {
finalHeaders["client-version"] = clientVersion;
}
Expand Down Expand Up @@ -123,7 +125,7 @@ Future<T> parsedArriRequest<T, E extends Exception>(
http.Client? httpClient,
HttpMethod method = HttpMethod.post,
Map<String, dynamic>? params,
Map<String, String> Function()? headers,
FutureOr<Map<String, String>> Function()? headers,
String? clientVersion,
required T Function(String) parser,
}) async {
Expand All @@ -148,7 +150,7 @@ Future<ArriRequestResult<T>> parsedArriRequestSafe<T>(
http.Client? httpClient,
HttpMethod httpMethod = HttpMethod.get,
Map<String, dynamic>? params,
Map<String, String> Function()? headers,
FutureOr<Map<String, String>> Function()? headers,
required T Function(String) parser,
String? clientVersion,
}) async {
Expand Down
16 changes: 8 additions & 8 deletions languages/dart/dart-client/lib/sse.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ typedef SseHookOnOpen<T> = void Function(
http.StreamedResponse response, EventSource<T> connection);
typedef SseHookOnClose<T> = void Function(EventSource<T> connection);

EventSource<T> parsedArriSseRequest<T>(
Future<EventSource<T>> parsedArriSseRequest<T>(
String url, {
http.Client? httpClient,
required HttpMethod method,
required T Function(String data) parser,
Map<String, dynamic>? params,
Map<String, String> Function()? headers,
FutureOr<Map<String, String>> Function()? headers,
Duration? retryDelay,
int? maxRetryCount,
SseHookOnData<T>? onData,
Expand All @@ -30,15 +30,15 @@ EventSource<T> parsedArriSseRequest<T>(
SseHookOnClose<T>? onClose,
String? lastEventId,
String? clientVersion,
}) {
}) async {
return EventSource(
httpClient: httpClient,
url: url,
method: method,
parser: parser,
params: params,
headers: () {
final result = headers?.call() ?? {};
headers: () async {
final result = await headers?.call() ?? {};
if (clientVersion != null && clientVersion.isNotEmpty) {
result["client-version"] = clientVersion;
}
Expand All @@ -60,7 +60,7 @@ class EventSource<T> {
final String url;
final HttpMethod method;
final Map<String, dynamic>? _params;
final Map<String, String> Function()? _headers;
final FutureOr<Map<String, String>> Function()? _headers;
String? lastEventId;
StreamController<T>? _streamController;
final Duration _retryDelay;
Expand All @@ -83,7 +83,7 @@ class EventSource<T> {
http.Client? httpClient,
this.method = HttpMethod.get,
Map<String, dynamic>? params,
Map<String, String> Function()? headers,
FutureOr<Map<String, String>> Function()? headers,
Duration retryDelay = Duration.zero,
int? maxRetryCount,
// hooks
Expand Down Expand Up @@ -151,7 +151,7 @@ class EventSource<T> {
if (body.isNotEmpty) {
request.body = body;
}
_headers?.call().forEach((key, value) {
(await _headers?.call())?.forEach((key, value) {
request.headers[key] = value;
});
if (lastEventId != null) {
Expand Down
4 changes: 2 additions & 2 deletions languages/dart/dart-client/lib/ws.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import 'package:web_socket_channel/status.dart' as status;
Future<ArriWebsocketController<TServerMessage, TClientMessage>>
arriWebsocketRequest<TServerMessage, TClientMessage>(
String url, {
Map<String, String> Function()? headers,
FutureOr<Map<String, String>> Function()? headers,
required TServerMessage Function(String msg) parser,
required String Function(TClientMessage msg) serializer,
String? clientVersion,
Expand All @@ -18,7 +18,7 @@ Future<ArriWebsocketController<TServerMessage, TClientMessage>>
if (headers != null || clientVersion?.isNotEmpty == true) {
final queryParts = <String>[];
for (final entry
in headers?.call().entries ?? <MapEntry<String, String>>[]) {
in (await headers?.call())?.entries ?? <MapEntry<String, String>>[]) {
queryParts.add("${entry.key}=${entry.value}");
}
if (clientVersion != null) {
Expand Down
15 changes: 8 additions & 7 deletions languages/dart/dart-codegen-reference/reference_client.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// this file was autogenerated by arri
// ignore_for_file: type=lint, unused_field
import "dart:async";
import "dart:convert";
import "package:arri_client/arri_client.dart";
import "package:http/http.dart" as http;
Expand All @@ -8,11 +9,11 @@ class TestClient {
final http.Client? _httpClient;
final String _baseUrl;
final String _clientVersion = "11";
late final Map<String, String> Function()? _headers;
late final FutureOr<Map<String, String>> Function()? _headers;
TestClient({
http.Client? httpClient,
String baseUrl = "",
Map<String, String> Function()? headers,
FutureOr<Map<String, String>> Function()? headers,
}) : _httpClient = httpClient,
_baseUrl = baseUrl,
_headers = headers;
Expand Down Expand Up @@ -44,11 +45,11 @@ class TestClientUsersService {
final http.Client? _httpClient;
final String _baseUrl;
final String _clientVersion = "11";
late final Map<String, String> Function()? _headers;
late final FutureOr<Map<String, String>> Function()? _headers;
TestClientUsersService({
http.Client? httpClient,
String baseUrl = "",
Map<String, String> Function()? headers,
FutureOr<Map<String, String>> Function()? headers,
}) : _httpClient = httpClient,
_baseUrl = baseUrl,
_headers = headers;
Expand Down Expand Up @@ -92,7 +93,7 @@ class TestClientUsersService {
}

/// Watch a user
EventSource<User> watchUser(
Future<EventSource<User>> watchUser(
UserParams params, {
SseHookOnData<User>? onData,
SseHookOnError<User>? onError,
Expand Down Expand Up @@ -135,11 +136,11 @@ class TestClientUsersSettingsService {
final http.Client? _httpClient;
final String _baseUrl;
final String _clientVersion = "11";
late final Map<String, String> Function()? _headers;
late final FutureOr<Map<String, String>> Function()? _headers;
TestClientUsersSettingsService({
http.Client? httpClient,
String baseUrl = "",
Map<String, String> Function()? headers,
FutureOr<Map<String, String>> Function()? headers,
}) : _httpClient = httpClient,
_baseUrl = baseUrl,
_headers = headers;
Expand Down
12 changes: 6 additions & 6 deletions languages/dart/dart-codegen/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ describe("Service Generation", () => {
final http.Client? _httpClient;
final String _baseUrl;
final String _clientVersion = "";
late final Map<String, String> Function()? _headers;
late final FutureOr<Map<String, String>> Function()? _headers;
UserService({
http.Client? httpClient,
String baseUrl = "",
Map<String, String> Function()? headers,
FutureOr<Map<String, String>> Function()? headers,
}) : _httpClient = httpClient,
_baseUrl = baseUrl,
_headers = headers;
Expand Down Expand Up @@ -95,11 +95,11 @@ class UserSettingsService {
final http.Client? _httpClient;
final String _baseUrl;
final String _clientVersion = "";
late final Map<String, String> Function()? _headers;
late final FutureOr<Map<String, String>> Function()? _headers;
UserSettingsService({
http.Client? httpClient,
String baseUrl = "",
Map<String, String> Function()? headers,
FutureOr<Map<String, String>> Function()? headers,
}) : _httpClient = httpClient,
_baseUrl = baseUrl,
_headers = headers;
Expand Down Expand Up @@ -140,11 +140,11 @@ class UserSettingsService {
final http.Client? _httpClient;
final String _baseUrl;
final String _clientVersion = "";
late final Map<String, String> Function()? _headers;
late final FutureOr<Map<String, String>> Function()? _headers;
PostsService({
http.Client? httpClient,
String baseUrl = "",
Map<String, String> Function()? headers,
FutureOr<Map<String, String>> Function()? headers,
}) : _httpClient = httpClient,
_baseUrl = baseUrl,
_headers = headers;
Expand Down
13 changes: 7 additions & 6 deletions languages/dart/dart-codegen/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ ${modelParts.join("\n")}`;
}
return `// this file was autogenerated by arri
// ignore_for_file: type=lint, unused_field
import "dart:async";
import "dart:convert";
import "package:arri_client/arri_client.dart";
import "package:http/http.dart" as http;
Expand All @@ -167,11 +168,11 @@ class ${opts.clientName} {
final http.Client? _httpClient;
final String _baseUrl;
final String _clientVersion = "${def.info?.version ?? ""}";
late final Map<String, String> Function()? _headers;
late final FutureOr<Map<String, String>> Function()? _headers;
${opts.clientName}({
http.Client? httpClient,
String baseUrl = "",
Map<String, String> Function()? headers,
FutureOr<Map<String, String>> Function()? headers,
}) : _httpClient = httpClient,
_baseUrl = baseUrl,
_headers = headers;
Expand Down Expand Up @@ -228,11 +229,11 @@ export function dartServiceFromDefinition(
final http.Client? _httpClient;
final String _baseUrl;
final String _clientVersion = "${opts.versionNumber}";
late final Map<String, String> Function()? _headers;
late final FutureOr<Map<String, String>> Function()? _headers;
${serviceName}Service({
http.Client? httpClient,
String baseUrl = "",
Map<String, String> Function()? headers,
FutureOr<Map<String, String>> Function()? headers,
}) : _httpClient = httpClient,
_baseUrl = baseUrl,
_headers = headers;
Expand Down Expand Up @@ -281,13 +282,13 @@ export function dartHttpRpcFromSchema(
| "Future<number>"
| "Future<void>"
| `Future<${string}>`
| `EventSource<${string}>` = `Future<String>`;
| `Future<EventSource<${string}>>` = `Future<String>`;
let returnTypeName = "String";
if (def.response) {
returnTypeName = pascalCase(def.response);

if (def.isEventStream) {
returnType = `EventSource<${returnTypeName}>`;
returnType = `Future<EventSource<${returnTypeName}>>`;
} else {
returnType = `Future<${returnTypeName}>`;
}
Expand Down
Loading

0 comments on commit 579a0d3

Please sign in to comment.