Skip to content
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
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ on:
branches: [main]

env:
DART_VERSION: 3.10.4
DART_VERSION: 3.7.2

jobs:
ci:
Expand Down
10 changes: 7 additions & 3 deletions example/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@ import 'package:sturdy_http/sturdy_http.dart';

void main(List<String> args) async {
// Set up some fake HTTP responses using Charlatan
final charlatan = Charlatan()
..whenGet('/foo', (request) => CharlatanHttpResponse(body: 'Hello World!'))
..whenPost('/foo', (request) => CharlatanHttpResponse(statusCode: 204));
final charlatan =
Charlatan()
..whenGet(
'/foo',
(request) => CharlatanHttpResponse(body: 'Hello World!'),
)
..whenPost('/foo', (request) => CharlatanHttpResponse(statusCode: 204));

// Create your client
final client = SturdyHttp(
Expand Down
40 changes: 22 additions & 18 deletions lib/src/sturdy_http.dart
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,13 @@ class SturdyHttp {
/// {@macro http_client}
SturdyHttp withBaseUrl(String baseUrl) {
return SturdyHttp._(
dio: Dio()
..options.baseUrl = baseUrl
..options.listFormat = ListFormat.multiCompatible
..interceptors.addAll(_dio.interceptors)
..interceptors.removeImplyContentTypeInterceptor()
..httpClientAdapter = _dio.httpClientAdapter,
dio:
Dio()
..options.baseUrl = baseUrl
..options.listFormat = ListFormat.multiCompatible
..interceptors.addAll(_dio.interceptors)
..interceptors.removeImplyContentTypeInterceptor()
..httpClientAdapter = _dio.httpClientAdapter,
deserializer: _deserializer,
eventListener: _eventListener,
retryBehavior: _retryBehavior,
Expand Down Expand Up @@ -147,9 +148,10 @@ class SturdyHttp {
RawRequestBody(:final data) => data,
},
queryParameters: request.queryParams,
options: request.options != null
? request.options!.copyWith(method: request.type.name)
: Options(method: request.type.name),
options:
request.options != null
? request.options!.copyWith(method: request.type.name)
: Options(method: request.type.name),
cancelToken: request.cancelToken,
onReceiveProgress: request.onReceiveProgress,
onSendProgress: request.onSendProgress,
Expand All @@ -160,12 +162,13 @@ class SturdyHttp {
final data = dioResponse.data;
if (data == null || data is! R) {
String buildErrorMessage() {
final messageSuffix = data == null
// Disallow empty responses when status code is non-204
? 'was null and status code was ${dioResponse!.statusCode}'
// Enforce that response data matches expected, otherwise we'll run into casting
// issues below
: 'was of type ${data.runtimeType} when it should have been of type $R';
final messageSuffix =
data == null
// Disallow empty responses when status code is non-204
? 'was null and status code was ${dioResponse!.statusCode}'
// Enforce that response data matches expected, otherwise we'll run into casting
// issues below
: 'was of type ${data.runtimeType} when it should have been of type $R';
return 'Request to ${request.path} was successful but response data $messageSuffix';
}

Expand Down Expand Up @@ -272,9 +275,10 @@ Dio _configureDio({
}) {
return Dio()
// Instruct Dio to use the same Isolate approach as requested of SturdyHttp
..transformer = deserializer is MainIsolateDeserializer
? SyncTransformer()
: BackgroundTransformer()
..transformer =
deserializer is MainIsolateDeserializer
? SyncTransformer()
: BackgroundTransformer()
..options.baseUrl = baseUrl
..options.listFormat = ListFormat.multiCompatible
..interceptors.addAll(interceptors)
Expand Down
6 changes: 3 additions & 3 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ homepage: https://github.com/Betterment/sturdy_http
repository: https://github.com/Betterment/sturdy_http

environment:
sdk: '>=3.10.0 <4.0.0'
sdk: '>=3.7.2 <4.0.0'

dependencies:
collection: ^1.19.1
Expand All @@ -14,5 +14,5 @@ dependencies:

dev_dependencies:
charlatan: ^0.5.0
lints: ^6.0.0
test: ^1.28.0
lints: ^5.1.1
test: ^1.29.0
79 changes: 41 additions & 38 deletions test/src/sturdy_http_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -338,9 +338,10 @@ void main() {
OkResponse<Json>(:final response) => Success(
Foo.fromMap(response),
),
GenericError() => throw Exception(
'Decoding error in GenericError',
),
GenericError() =>
throw Exception(
'Decoding error in GenericError',
),
_ => const Failure('Not expected: orElse'),
};
},
Expand Down Expand Up @@ -925,22 +926,23 @@ void main() {
return CharlatanHttpResponse(statusCode: 522);
});

final response = await buildSubject(retryBehavior: NeverRetry())
.execute<Json, Result<bool, String>>(
const GetRequest(
defaultPath,
retryBehavior: Retry(
maxRetries: 2,
retryInterval: Duration(milliseconds: 100),
),
),
onResponse: (response) {
return switch (response) {
GenericError() => const Success(true),
_ => const Failure('Not expected: orElse'),
};
},
);
final response = await buildSubject(
retryBehavior: NeverRetry(),
).execute<Json, Result<bool, String>>(
const GetRequest(
defaultPath,
retryBehavior: Retry(
maxRetries: 2,
retryInterval: Duration(milliseconds: 100),
),
),
onResponse: (response) {
return switch (response) {
GenericError() => const Success(true),
_ => const Failure('Not expected: orElse'),
};
},
);

switch (response) {
case Success(:final success):
Expand All @@ -961,26 +963,27 @@ void main() {
return CharlatanHttpResponse(statusCode: statusCode);
});

final response = await buildSubject(retryBehavior: NeverRetry())
.execute<Json, Result<bool, String>>(
GetRequest(
defaultPath,
retryBehavior: Retry(
maxRetries: 2,
retryInterval: Duration(milliseconds: 100),
retryClause: (r) {
// Body will be `null`; essentially disallow retrying
return r != null;
},
),
),
onResponse: (response) {
return switch (response) {
GenericError() => const Success(true),
_ => const Failure('Not expected: orElse'),
};
final response = await buildSubject(
retryBehavior: NeverRetry(),
).execute<Json, Result<bool, String>>(
GetRequest(
defaultPath,
retryBehavior: Retry(
maxRetries: 2,
retryInterval: Duration(milliseconds: 100),
retryClause: (r) {
// Body will be `null`; essentially disallow retrying
return r != null;
},
);
),
),
onResponse: (response) {
return switch (response) {
GenericError() => const Success(true),
_ => const Failure('Not expected: orElse'),
};
},
);

switch (response) {
case Success(:final success):
Expand Down