diff --git a/CHANGELOG.md b/CHANGELOG.md index afa29e057..df015cbb7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [0.9.0] - 2023-11-10 +### Added + +- Added helper methods to request information to reduce the amount of generated code. [Kiota #3651](https://github.com/microsoft/kiota/issues/3651) + ### Changed - Kiota-Java has moved away from Async/Completable futures, thus Async components are no longer utilized and have been removed. Furthermore, requestAdapter methods no longer use the async suffix. [#175](https://github.com/microsoft/kiota-java/issues/175) diff --git a/components/abstractions/src/main/java/com/microsoft/kiota/RequestInformation.java b/components/abstractions/src/main/java/com/microsoft/kiota/RequestInformation.java index e04714f67..170705deb 100644 --- a/components/abstractions/src/main/java/com/microsoft/kiota/RequestInformation.java +++ b/components/abstractions/src/main/java/com/microsoft/kiota/RequestInformation.java @@ -38,6 +38,46 @@ public class RequestInformation { /** Creates a new instance of the request information class. */ public RequestInformation() { //Default constructor } + /** + * Creates a new instance of the request information class. + * @param method The HTTP method for the request. + * @param urlTemplate The url template for the request. + * @param pathParameters The path parameters for the request. + */ + public RequestInformation(@Nonnull final HttpMethod method, @Nonnull final String urlTemplate, @Nonnull final Map pathParameters) { + this.httpMethod = Objects.requireNonNull(method); + this.urlTemplate = Objects.requireNonNull(urlTemplate); + this.pathParameters = Objects.requireNonNull(pathParameters); + } + /** + * Configures the request information based on the request configuration and the query parameters getter. + * @param The type of the request configuration. + * @param requestConfiguration The request configuration to apply to the request information. + * @param configurationFactory The factory to create the request configuration from. + */ + public void configure(@Nullable final java.util.function.Consumer requestConfiguration, @Nonnull final java.util.function.Supplier configurationFactory) { + configure(requestConfiguration, configurationFactory, null); + } + /** + * Configures the request information based on the request configuration and the query parameters getter. + * @param The type of the request configuration. + * @param requestConfiguration The request configuration to apply to the request information. + * @param configurationFactory The factory to create the request configuration from. + * @param queryParametersGetter The function to get the query parameters from the request configuration. + */ + public void configure(@Nullable final java.util.function.Consumer requestConfiguration, @Nonnull final java.util.function.Supplier configurationFactory, @Nullable final java.util.function.Function queryParametersGetter) { + Objects.requireNonNull(configurationFactory); + if (requestConfiguration == null) { + return; + } + final T requestConfig = configurationFactory.get(); + requestConfiguration.accept(requestConfig); + if (queryParametersGetter != null) { + addQueryParameters(queryParametersGetter.apply(requestConfig)); + } + headers.putAll(requestConfig.headers); + addRequestOptions(requestConfig.options); + } /** The url template for the current request */ @Nullable public String urlTemplate;