From 6e625e760330dc87a0ea027b1f653666a2638f1f Mon Sep 17 00:00:00 2001 From: Ricardo Boss Date: Wed, 14 Feb 2024 19:51:21 +0100 Subject: [PATCH] Move withUrl to an extension method --- lib/kiota_abstractions.dart | 1 + lib/src/base_request_builder.dart | 4 ---- .../base_request_builder_extensions.dart | 11 +++++++++++ test/request_builder_test.dart | 17 ++++++++++------- 4 files changed, 22 insertions(+), 11 deletions(-) create mode 100644 lib/src/extensions/base_request_builder_extensions.dart diff --git a/lib/kiota_abstractions.dart b/lib/kiota_abstractions.dart index 739f84a..f7bb09c 100644 --- a/lib/kiota_abstractions.dart +++ b/lib/kiota_abstractions.dart @@ -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'; diff --git a/lib/src/base_request_builder.dart b/lib/src/base_request_builder.dart index 9bbc60f..d15af2e 100644 --- a/lib/src/base_request_builder.dart +++ b/lib/src/base_request_builder.dart @@ -19,8 +19,4 @@ abstract class BaseRequestBuilder> { /// 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; } diff --git a/lib/src/extensions/base_request_builder_extensions.dart b/lib/src/extensions/base_request_builder_extensions.dart new file mode 100644 index 0000000..3220ccc --- /dev/null +++ b/lib/src/extensions/base_request_builder_extensions.dart @@ -0,0 +1,11 @@ +part of '../../kiota_abstractions.dart'; + +extension BaseRequestBuilderExtensions> on BaseRequestBuilder { + /// 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); +} diff --git a/test/request_builder_test.dart b/test/request_builder_test.dart index c29b380..b3da2c3 100644 --- a/test/request_builder_test.dart +++ b/test/request_builder_test.dart @@ -20,14 +20,14 @@ class SampleRequestBuilder extends BaseRequestBuilder { id, requestAdapter, urlTemplate, - pathParameters, + {...pathParameters}, ); } } @GenerateMocks([RequestAdapter]) void main() { - test('BaseRequestBuilder.copyWith', () { + test('BaseRequestBuilder.withUrl', () { final mockRequestAdapter = MockRequestAdapter(); final requestBuilder = SampleRequestBuilder( @@ -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'}), ); }); }