Skip to content

Commit

Permalink
Move withUrl to an extension method
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardoboss committed Feb 14, 2024
1 parent b17b983 commit 6e625e7
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 11 deletions.
1 change: 1 addition & 0 deletions lib/kiota_abstractions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import 'package:std_uritemplate/std_uritemplate.dart';

part 'src/api_client_builder.dart';
part 'src/base_request_builder.dart';
part 'src/extensions/base_request_builder_extensions.dart';
part 'src/extensions/map_extensions.dart';
part 'src/extensions/request_information_extensions.dart';
part 'src/http_method.dart';
Expand Down
4 changes: 0 additions & 4 deletions lib/src/base_request_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,4 @@ abstract class BaseRequestBuilder<T extends BaseRequestBuilder<T>> {

/// Clones the current request builder.
T clone();

/// Clones the current request builder using [clone] and sets the given
/// [template] as the url template.
T withUrl(String template) => this.clone()..urlTemplate = template;
}
11 changes: 11 additions & 0 deletions lib/src/extensions/base_request_builder_extensions.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
part of '../../kiota_abstractions.dart';

extension BaseRequestBuilderExtensions<T extends BaseRequestBuilder<T>> on BaseRequestBuilder<T> {
/// Clones the current request builder using [clone] and sets the given
/// [rawUrl] as the url to use.
///
/// This utilizes the [RequestInformation.rawUrlKey] to store the raw url
/// in the [pathParameters].
T withUrl(String rawUrl) => clone()
..pathParameters.addOrReplace(RequestInformation.rawUrlKey, rawUrl);
}
17 changes: 10 additions & 7 deletions test/request_builder_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ class SampleRequestBuilder extends BaseRequestBuilder<SampleRequestBuilder> {
id,
requestAdapter,
urlTemplate,
pathParameters,
{...pathParameters},
);
}
}

@GenerateMocks([RequestAdapter])
void main() {
test('BaseRequestBuilder.copyWith', () {
test('BaseRequestBuilder.withUrl', () {
final mockRequestAdapter = MockRequestAdapter();

final requestBuilder = SampleRequestBuilder(
Expand All @@ -38,18 +38,21 @@ void main() {
);

final newRequestBuilder =
requestBuilder.withUrl('https://graph.microsoft.com/v2.0/users/{id}');
requestBuilder.withUrl('https://graph.microsoft.com/v2.0/users/123');

expect(newRequestBuilder.id, equals(1));
expect(
newRequestBuilder.urlTemplate,
equals('https://graph.microsoft.com/v2.0/users/{id}'),
newRequestBuilder.pathParameters,
equals({
'id': '1',
'request-raw-url': 'https://graph.microsoft.com/v2.0/users/123',
}),
);

// make sure the original requestBuilder is not modified
expect(
requestBuilder.urlTemplate,
equals('https://graph.microsoft.com/v1.0/users/{id}'),
requestBuilder.pathParameters,
equals({'id': '1'}),
);
});
}

0 comments on commit 6e625e7

Please sign in to comment.