diff --git a/CHANGELOG.md b/CHANGELOG.md index 986ea72fa7..45216285a5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added +- Added support for changing the base url #795 + ### Changed - Fixes a bug where arrays of enums could be wrongly mapped. diff --git a/abstractions/dotnet/src/IRequestAdapter.cs b/abstractions/dotnet/src/IRequestAdapter.cs index b6c0ae3d3e..830f554ebc 100644 --- a/abstractions/dotnet/src/IRequestAdapter.cs +++ b/abstractions/dotnet/src/IRequestAdapter.cs @@ -58,5 +58,9 @@ public interface IRequestAdapter /// The response handler to use for the HTTP request instead of the default handler. /// A Task to await completion. Task SendNoContentAsync(RequestInformation requestInfo, IResponseHandler responseHandler = default); + /// + /// The base url for every request. + /// + string BaseUrl { get; set; } } } diff --git a/abstractions/dotnet/src/Microsoft.Kiota.Abstractions.csproj b/abstractions/dotnet/src/Microsoft.Kiota.Abstractions.csproj index 7965762641..49e4eeca76 100644 --- a/abstractions/dotnet/src/Microsoft.Kiota.Abstractions.csproj +++ b/abstractions/dotnet/src/Microsoft.Kiota.Abstractions.csproj @@ -5,7 +5,7 @@ net5.0 true https://github.com/microsoft/kiota - 1.0.22 + 1.0.23 true true diff --git a/abstractions/go/request_adapter.go b/abstractions/go/request_adapter.go index 4d028a6884..68be6685b0 100644 --- a/abstractions/go/request_adapter.go +++ b/abstractions/go/request_adapter.go @@ -55,4 +55,8 @@ type RequestAdapter interface { GetSerializationWriterFactory() s.SerializationWriterFactory // Enables the backing store proxies for the SerializationWriters and ParseNodes in use. EnableBackingStore() + // Sets the base url for every request. + SetBaseUrl(baseUrl string) + // Gets the base url for every request. + GetBaseUrl() string } diff --git a/abstractions/java/lib/build.gradle b/abstractions/java/lib/build.gradle index 9e201c22ab..28b205af54 100644 --- a/abstractions/java/lib/build.gradle +++ b/abstractions/java/lib/build.gradle @@ -48,7 +48,7 @@ publishing { publications { gpr(MavenPublication) { artifactId 'kiota-abstractions' - version '1.0.22' + version '1.0.23' from(components.java) } } diff --git a/abstractions/java/lib/src/main/java/com/microsoft/kiota/RequestAdapter.java b/abstractions/java/lib/src/main/java/com/microsoft/kiota/RequestAdapter.java index d3d645640e..62121bb6d2 100644 --- a/abstractions/java/lib/src/main/java/com/microsoft/kiota/RequestAdapter.java +++ b/abstractions/java/lib/src/main/java/com/microsoft/kiota/RequestAdapter.java @@ -58,4 +58,15 @@ public interface RequestAdapter { * @return a {@link CompletableFuture} with the deserialized primitive collection response model. */ CompletableFuture> sendPrimitiveCollectionAsync(@Nonnull final RequestInformation requestInfo, @Nonnull final Class targetClass, @Nullable final ResponseHandler responseHandler); + /** + * Sets The base url for every request. + * @param baseUrl The base url for every request. + */ + void setBaseUrl(@Nonnull final String baseUrl); + /** + * Gets The base url for every request. + * @return The base url for every request. + */ + @Nonnull + String getBaseUrl(); } \ No newline at end of file diff --git a/abstractions/ruby/microsoft_kiota_abstractions/lib/microsoft_kiota_abstractions/request_adapter.rb b/abstractions/ruby/microsoft_kiota_abstractions/lib/microsoft_kiota_abstractions/request_adapter.rb index 396aaf9c41..e4631a92a6 100644 --- a/abstractions/ruby/microsoft_kiota_abstractions/lib/microsoft_kiota_abstractions/request_adapter.rb +++ b/abstractions/ruby/microsoft_kiota_abstractions/lib/microsoft_kiota_abstractions/request_adapter.rb @@ -13,5 +13,13 @@ def get_serialization_writer_factory() raise NotImplementedError.new end + def set_base_url(base_url) + raise NotImplementedError.new + end + + def get_base_url() + raise NotImplementedError.new + end + end end diff --git a/abstractions/ruby/microsoft_kiota_abstractions/lib/microsoft_kiota_abstractions/version.rb b/abstractions/ruby/microsoft_kiota_abstractions/lib/microsoft_kiota_abstractions/version.rb index 40d2223108..ecd6d6d85f 100644 --- a/abstractions/ruby/microsoft_kiota_abstractions/lib/microsoft_kiota_abstractions/version.rb +++ b/abstractions/ruby/microsoft_kiota_abstractions/lib/microsoft_kiota_abstractions/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module MicrosoftKiotaAbstractions - VERSION = "0.1.10" + VERSION = "0.1.11" end diff --git a/abstractions/typescript/package-lock.json b/abstractions/typescript/package-lock.json index 77e7a93ffc..570c5e21bf 100644 --- a/abstractions/typescript/package-lock.json +++ b/abstractions/typescript/package-lock.json @@ -1,12 +1,12 @@ { "name": "@microsoft/kiota-abstractions", - "version": "1.0.22", + "version": "1.0.23", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@microsoft/kiota-abstractions", - "version": "1.0.22", + "version": "1.0.23", "license": "MIT", "dependencies": { "uri-template-lite": "^20.5.0", diff --git a/abstractions/typescript/package.json b/abstractions/typescript/package.json index 9274e83dea..eaac653332 100644 --- a/abstractions/typescript/package.json +++ b/abstractions/typescript/package.json @@ -1,6 +1,6 @@ { "name": "@microsoft/kiota-abstractions", - "version": "1.0.22", + "version": "1.0.23", "description": "Core abstractions for kiota generated libraries in TypeScript and JavaScript", "main": "dist/index.js", "types": "dist/index.d.ts", diff --git a/abstractions/typescript/src/requestAdapter.ts b/abstractions/typescript/src/requestAdapter.ts index 80fb1208fc..6f3c9f1750 100644 --- a/abstractions/typescript/src/requestAdapter.ts +++ b/abstractions/typescript/src/requestAdapter.ts @@ -59,4 +59,6 @@ export interface RequestAdapter { * @param backingStoreFactory the backing store factory to use. */ enableBackingStore(backingStoreFactory?: BackingStoreFactory | undefined): void; + /** The base url for every request. */ + baseUrl: string; } \ No newline at end of file diff --git a/http/dotnet/httpclient/Microsoft.Kiota.Http.HttpClientLibrary.Tests/Extensions/HttpRequestMessageExtensionsTests.cs b/http/dotnet/httpclient/Microsoft.Kiota.Http.HttpClientLibrary.Tests/Extensions/HttpRequestMessageExtensionsTests.cs index 5ec82d01b6..f180cfcf55 100644 --- a/http/dotnet/httpclient/Microsoft.Kiota.Http.HttpClientLibrary.Tests/Extensions/HttpRequestMessageExtensionsTests.cs +++ b/http/dotnet/httpclient/Microsoft.Kiota.Http.HttpClientLibrary.Tests/Extensions/HttpRequestMessageExtensionsTests.cs @@ -5,6 +5,7 @@ using System.Text; using System.Threading.Tasks; using Microsoft.Kiota.Abstractions; +using Microsoft.Kiota.Abstractions.Authentication; using Microsoft.Kiota.Http.HttpClientLibrary.Extensions; using Microsoft.Kiota.Http.HttpClientLibrary.Middleware.Options; using Xunit; @@ -14,6 +15,10 @@ namespace Microsoft.Kiota.Http.HttpClientLibrary.Tests.Extensions { public class HttpRequestMessageExtensionsTests { + private readonly HttpClientRequestAdapter requestAdapter; + public HttpRequestMessageExtensionsTests () { + requestAdapter = new HttpClientRequestAdapter(new AnonymousAuthenticationProvider()); + } [Fact] public void GetRequestOptionCanExtractRequestOptionFromHttpRequestMessage() { @@ -29,7 +34,7 @@ public void GetRequestOptionCanExtractRequestOptionFromHttpRequestMessage() }; requestInfo.AddRequestOptions(redirectHandlerOption); // Act and get a request message - var requestMessage = HttpClientRequestAdapter.GetRequestMessageFromRequestInformation(requestInfo); + var requestMessage = requestAdapter.GetRequestMessageFromRequestInformation(requestInfo); var extractedOption = requestMessage.GetRequestOption(); // Assert Assert.NotNull(extractedOption); @@ -45,7 +50,7 @@ public async Task CloneAsyncWithEmptyHttpContent() HttpMethod = HttpMethod.GET, URI = new Uri("http://localhost") }; - var originalRequest = HttpClientRequestAdapter.GetRequestMessageFromRequestInformation(requestInfo); + var originalRequest = requestAdapter.GetRequestMessageFromRequestInformation(requestInfo); HttpRequestMessage clonedRequest = await originalRequest.CloneAsync(); Assert.NotNull(clonedRequest); @@ -63,7 +68,7 @@ public async Task CloneAsyncWithHttpContent() URI = new Uri("http://localhost") }; requestInfo.SetStreamContent(new MemoryStream(Encoding.UTF8.GetBytes("contents"))); - var originalRequest = HttpClientRequestAdapter.GetRequestMessageFromRequestInformation(requestInfo); + var originalRequest = requestAdapter.GetRequestMessageFromRequestInformation(requestInfo); originalRequest.Content = new StringContent("contents"); var clonedRequest = await originalRequest.CloneAsync(); @@ -89,7 +94,7 @@ public async Task CloneAsyncWithRequestOption() MaxRedirect = 7 }; requestInfo.AddRequestOptions(redirectHandlerOption); - var originalRequest = HttpClientRequestAdapter.GetRequestMessageFromRequestInformation(requestInfo); + var originalRequest = requestAdapter.GetRequestMessageFromRequestInformation(requestInfo); originalRequest.Content = new StringContent("contents"); var clonedRequest = await originalRequest.CloneAsync(); @@ -111,7 +116,7 @@ public void IsBufferedReturnsTrueForGetRequest() HttpMethod = HttpMethod.GET, URI = new Uri("http://localhost") }; - var originalRequest = HttpClientRequestAdapter.GetRequestMessageFromRequestInformation(requestInfo); + var originalRequest = requestAdapter.GetRequestMessageFromRequestInformation(requestInfo); // Act var response = originalRequest.IsBuffered(); // Assert @@ -126,7 +131,7 @@ public void IsBufferedReturnsTrueForPostWithNoContent() HttpMethod = HttpMethod.POST, URI = new Uri("http://localhost") }; - var originalRequest = HttpClientRequestAdapter.GetRequestMessageFromRequestInformation(requestInfo); + var originalRequest = requestAdapter.GetRequestMessageFromRequestInformation(requestInfo); // Act var response = originalRequest.IsBuffered(); // Assert @@ -143,7 +148,7 @@ public void IsBufferedReturnsTrueForPostWithBufferStringContent() URI = new Uri("http://localhost"), Content = new MemoryStream(data) }; - var originalRequest = HttpClientRequestAdapter.GetRequestMessageFromRequestInformation(requestInfo); + var originalRequest = requestAdapter.GetRequestMessageFromRequestInformation(requestInfo); // Act var response = originalRequest.IsBuffered(); // Assert diff --git a/http/dotnet/httpclient/Microsoft.Kiota.Http.HttpClientLibrary.Tests/Middleware/TelemetryHandlerTests.cs b/http/dotnet/httpclient/Microsoft.Kiota.Http.HttpClientLibrary.Tests/Middleware/TelemetryHandlerTests.cs index fa15f94bfb..8fa9192a75 100644 --- a/http/dotnet/httpclient/Microsoft.Kiota.Http.HttpClientLibrary.Tests/Middleware/TelemetryHandlerTests.cs +++ b/http/dotnet/httpclient/Microsoft.Kiota.Http.HttpClientLibrary.Tests/Middleware/TelemetryHandlerTests.cs @@ -4,6 +4,7 @@ using System.Threading; using System.Threading.Tasks; using Microsoft.Kiota.Abstractions; +using Microsoft.Kiota.Abstractions.Authentication; using Microsoft.Kiota.Http.HttpClientLibrary.Middleware; using Microsoft.Kiota.Http.HttpClientLibrary.Middleware.Options; using Xunit; @@ -14,6 +15,7 @@ public class TelemetryHandlerTests { private readonly HttpMessageInvoker _invoker; + private readonly HttpClientRequestAdapter requestAdapter; public TelemetryHandlerTests() { var telemetryHandler = new TelemetryHandler @@ -21,6 +23,7 @@ public TelemetryHandlerTests() InnerHandler = new FakeSuccessHandler() }; this._invoker = new HttpMessageInvoker(telemetryHandler); + requestAdapter = new HttpClientRequestAdapter(new AnonymousAuthenticationProvider()); } [Fact] @@ -33,7 +36,7 @@ public async Task DefaultTelemetryHandlerDoesNotChangeRequest() URI = new Uri("http://localhost") }; // Act and get a request message - var requestMessage = HttpClientRequestAdapter.GetRequestMessageFromRequestInformation(requestInfo); + var requestMessage = requestAdapter.GetRequestMessageFromRequestInformation(requestInfo); Assert.Empty(requestMessage.Headers); // Act @@ -64,7 +67,7 @@ public async Task TelemetryHandlerSelectivelyEnrichesRequestsBasedOnRequestMiddl // Configures the telemetry at the request level requestInfo.AddRequestOptions(telemetryHandlerOption); // Act and get a request message - var requestMessage = HttpClientRequestAdapter.GetRequestMessageFromRequestInformation(requestInfo); + var requestMessage = requestAdapter.GetRequestMessageFromRequestInformation(requestInfo); Assert.Empty(requestMessage.Headers); // Act @@ -102,7 +105,7 @@ public async Task TelemetryHandlerGloballyEnrichesRequests() URI = new Uri("http://localhost") }; - var requestMessage = HttpClientRequestAdapter.GetRequestMessageFromRequestInformation(requestInfo);// get a request message + var requestMessage = requestAdapter.GetRequestMessageFromRequestInformation(requestInfo);// get a request message Assert.Empty(requestMessage.Headers); // Act diff --git a/http/dotnet/httpclient/Microsoft.Kiota.Http.HttpClientLibrary.Tests/HttpCoreTests.cs b/http/dotnet/httpclient/Microsoft.Kiota.Http.HttpClientLibrary.Tests/RequestAdapterTests.cs similarity index 90% rename from http/dotnet/httpclient/Microsoft.Kiota.Http.HttpClientLibrary.Tests/HttpCoreTests.cs rename to http/dotnet/httpclient/Microsoft.Kiota.Http.HttpClientLibrary.Tests/RequestAdapterTests.cs index fcbb945ada..bd80bc9c9e 100644 --- a/http/dotnet/httpclient/Microsoft.Kiota.Http.HttpClientLibrary.Tests/HttpCoreTests.cs +++ b/http/dotnet/httpclient/Microsoft.Kiota.Http.HttpClientLibrary.Tests/RequestAdapterTests.cs @@ -10,10 +10,12 @@ namespace Microsoft.Kiota.Http.HttpClientLibrary.Tests public class HttpClientRequestAdapterTests { private readonly IAuthenticationProvider _authenticationProvider; + private readonly HttpClientRequestAdapter requestAdapter; public HttpClientRequestAdapterTests() { _authenticationProvider = new Mock().Object; + requestAdapter = new HttpClientRequestAdapter(new AnonymousAuthenticationProvider()); } [Fact] @@ -56,7 +58,7 @@ public void GetRequestMessageFromRequestInformationSetsQueryParametersCorrectlyW requestInfo.QueryParameters.Add(queryParam, queryParamObject); // Act - var requestMessage = HttpClientRequestAdapter.GetRequestMessageFromRequestInformation(requestInfo); + var requestMessage = requestAdapter.GetRequestMessageFromRequestInformation(requestInfo); // Assert Assert.NotNull(requestMessage.RequestUri); diff --git a/http/dotnet/httpclient/src/HttpClientRequestAdapter.cs b/http/dotnet/httpclient/src/HttpClientRequestAdapter.cs index 7fcfb7f6c0..d091250319 100644 --- a/http/dotnet/httpclient/src/HttpClientRequestAdapter.cs +++ b/http/dotnet/httpclient/src/HttpClientRequestAdapter.cs @@ -51,6 +51,10 @@ public ISerializationWriterFactory SerializationWriterFactory } } /// + /// The base url for every request. + /// + public string BaseUrl { get; set; } + /// /// Send a instance with a collection instance of /// /// The instance to send @@ -199,8 +203,9 @@ private async Task GetHttpResponseMessage(RequestInformatio return response; } private const string ContentTypeHeaderName = "content-type"; - internal static HttpRequestMessage GetRequestMessageFromRequestInformation(RequestInformation requestInfo) + internal HttpRequestMessage GetRequestMessageFromRequestInformation(RequestInformation requestInfo) { + requestInfo.PathParameters.Add("baseurl", BaseUrl); var message = new HttpRequestMessage { Method = new System.Net.Http.HttpMethod(requestInfo.HttpMethod.ToString().ToUpperInvariant()), diff --git a/http/dotnet/httpclient/src/Microsoft.Kiota.Http.HttpClientLibrary.csproj b/http/dotnet/httpclient/src/Microsoft.Kiota.Http.HttpClientLibrary.csproj index f2c08f4f56..0acf828923 100644 --- a/http/dotnet/httpclient/src/Microsoft.Kiota.Http.HttpClientLibrary.csproj +++ b/http/dotnet/httpclient/src/Microsoft.Kiota.Http.HttpClientLibrary.csproj @@ -5,7 +5,7 @@ net5.0 true https://github.com/microsoft/kiota - 1.0.11 + 1.0.12 true @@ -13,7 +13,7 @@ - + diff --git a/http/go/nethttp/go.mod b/http/go/nethttp/go.mod index 8937440342..8c95f73382 100644 --- a/http/go/nethttp/go.mod +++ b/http/go/nethttp/go.mod @@ -4,7 +4,7 @@ go 1.16 require ( github.com/davecgh/go-spew v1.1.1 // indirect - github.com/microsoft/kiota/abstractions/go v0.0.0-20211101171910-f479920da5ba + github.com/microsoft/kiota/abstractions/go v0.0.14 github.com/stretchr/testify v1.7.0 gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect ) diff --git a/http/go/nethttp/nethttp_request_adapter.go b/http/go/nethttp/nethttp_request_adapter.go index 1dacf6ace3..8efa17fb48 100644 --- a/http/go/nethttp/nethttp_request_adapter.go +++ b/http/go/nethttp/nethttp_request_adapter.go @@ -24,6 +24,8 @@ type NetHttpRequestAdapter struct { httpClient *nethttp.Client // authenticationProvider is the provider used to authenticate requests authenticationProvider absauth.AuthenticationProvider + // The base url for every request. + baseUrl string } // NewNetHttpRequestAdapter creates a new NetHttpRequestAdapter with the given parameters @@ -73,6 +75,7 @@ func NewNetHttpRequestAdapterWithParseNodeFactoryAndSerializationWriterFactoryAn parseNodeFactory: parseNodeFactory, httpClient: httpClient, authenticationProvider: authenticationProvider, + baseUrl: "", } if result.httpClient == nil { defaultClient := GetDefaultClient() @@ -92,6 +95,12 @@ func (a *NetHttpRequestAdapter) GetSerializationWriterFactory() absser.Serializa func (a *NetHttpRequestAdapter) EnableBackingStore() { //TODO implement when backing store is available for go } +func (a *NetHttpRequestAdapter) SetBaseUrl(baseUrl string) { + a.baseUrl = baseUrl +} +func (a *NetHttpRequestAdapter) GetBaseUrl() string { + return a.baseUrl +} func (a *NetHttpRequestAdapter) getHttpResponseMessage(requestInfo abs.RequestInformation) (*nethttp.Response, error) { err := a.authenticationProvider.AuthenticateRequest(requestInfo) if err != nil { @@ -112,6 +121,7 @@ func (a *NetHttpRequestAdapter) getResponsePrimaryContentType(response *nethttp. return strings.ToLower(splat[0]) } func (a *NetHttpRequestAdapter) getRequestFromRequestInformation(requestInfo abs.RequestInformation) (*nethttp.Request, error) { + requestInfo.PathParameters["baseurl"] = a.GetBaseUrl() uri, err := requestInfo.GetUri() if err != nil { return nil, err diff --git a/http/java/okhttp/lib/build.gradle b/http/java/okhttp/lib/build.gradle index ffed968f55..0d1ba92e58 100644 --- a/http/java/okhttp/lib/build.gradle +++ b/http/java/okhttp/lib/build.gradle @@ -36,7 +36,7 @@ dependencies { // This dependency is used internally, and not exposed to consumers on their own compile classpath. implementation 'com.google.guava:guava:31.0.1-jre' api 'com.squareup.okhttp3:okhttp:4.9.2' - api 'com.microsoft.kiota:kiota-abstractions:1.0.22' + api 'com.microsoft.kiota:kiota-abstractions:1.0.23' } publishing { @@ -53,7 +53,7 @@ publishing { publications { gpr(MavenPublication) { artifactId 'kiota-http-okhttplibrary' - version '1.0.11' + version '1.0.12' from(components.java) } } diff --git a/http/java/okhttp/lib/src/main/java/com/microsoft/kiota/http/OkHttpRequestAdapter.java b/http/java/okhttp/lib/src/main/java/com/microsoft/kiota/http/OkHttpRequestAdapter.java index c1a65e12e9..54d39398ed 100644 --- a/http/java/okhttp/lib/src/main/java/com/microsoft/kiota/http/OkHttpRequestAdapter.java +++ b/http/java/okhttp/lib/src/main/java/com/microsoft/kiota/http/OkHttpRequestAdapter.java @@ -42,6 +42,14 @@ public class OkHttpRequestAdapter implements com.microsoft.kiota.RequestAdapter private final AuthenticationProvider authProvider; private ParseNodeFactory pNodeFactory; private SerializationWriterFactory sWriterFactory; + private String baseUrl = ""; + public void setBaseUrl(@Nonnull final String baseUrl) { + this.baseUrl = Objects.requireNonNull(baseUrl); + } + @Nonnull + public String getBaseUrl() { + return baseUrl; + } public OkHttpRequestAdapter(@Nonnull final AuthenticationProvider authenticationProvider){ this(authenticationProvider, null, null, null); } @@ -214,6 +222,7 @@ private CompletableFuture getHttpResponseMessage(@Nonnull final Reques } private Request getRequestFromRequestInformation(@Nonnull final RequestInformation requestInfo) throws URISyntaxException, MalformedURLException { + requestInfo.pathParameters.put("baseurl", getBaseUrl()); final RequestBody body = requestInfo.content == null ? null : new RequestBody() { @Override diff --git a/http/ruby/nethttp/microsoft_kiota_nethttplibrary/Gemfile b/http/ruby/nethttp/microsoft_kiota_nethttplibrary/Gemfile index 49230dd086..eaf775c317 100644 --- a/http/ruby/nethttp/microsoft_kiota_nethttplibrary/Gemfile +++ b/http/ruby/nethttp/microsoft_kiota_nethttplibrary/Gemfile @@ -7,7 +7,7 @@ gemspec git_source(:github) { |repo_name| 'https://rubygems.pkg.github.com/microsoft' } source 'https://rubygems.pkg.github.com/microsoft' do - gem 'microsoft_kiota_abstractions', '0.1.10' + gem 'microsoft_kiota_abstractions', '0.1.11' end gem 'rake', '~> 13.0' diff --git a/http/ruby/nethttp/microsoft_kiota_nethttplibrary/lib/microsoft_kiota_nethttplibrary/net_http_request_adapter.rb b/http/ruby/nethttp/microsoft_kiota_nethttplibrary/lib/microsoft_kiota_nethttplibrary/net_http_request_adapter.rb index e2ef70bea8..021cab41a1 100644 --- a/http/ruby/nethttp/microsoft_kiota_nethttplibrary/lib/microsoft_kiota_nethttplibrary/net_http_request_adapter.rb +++ b/http/ruby/nethttp/microsoft_kiota_nethttplibrary/lib/microsoft_kiota_nethttplibrary/net_http_request_adapter.rb @@ -21,6 +21,15 @@ def initialize(authentication_provider, parse_node_factory, serialization_writer @parse_node_factory = parse_node_factory @serialization_writer_factory = serialization_writer_factory @client = client + @base_url = '' + end + + def set_base_url(base_url) + @base_url = base_url + end + + def get_base_url() + @base_url end def get_serialization_writer_factory() @@ -54,6 +63,7 @@ def send_async(request_info, type, response_handler) end def get_request_from_request_info(request_info) + request_info.path_parameters['baseurl'] = @base_url case request_info.http_method when :GET request = @client::Get.new(request_info.uri.request_uri) diff --git a/http/ruby/nethttp/microsoft_kiota_nethttplibrary/lib/microsoft_kiota_nethttplibrary/version.rb b/http/ruby/nethttp/microsoft_kiota_nethttplibrary/lib/microsoft_kiota_nethttplibrary/version.rb index bba3c96bbb..dd2ce7b305 100644 --- a/http/ruby/nethttp/microsoft_kiota_nethttplibrary/lib/microsoft_kiota_nethttplibrary/version.rb +++ b/http/ruby/nethttp/microsoft_kiota_nethttplibrary/lib/microsoft_kiota_nethttplibrary/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module MicrosoftKiotaNethttplibrary - VERSION = '0.1.6' + VERSION = '0.1.7' end diff --git a/http/typescript/fetch/package-lock.json b/http/typescript/fetch/package-lock.json index 5c5de29bfb..cd1066e929 100644 --- a/http/typescript/fetch/package-lock.json +++ b/http/typescript/fetch/package-lock.json @@ -1,12 +1,12 @@ { "name": "@microsoft/kiota-http-fetchlibrary", - "version": "1.0.11", + "version": "1.0.12", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@microsoft/kiota-http-fetchlibrary", - "version": "1.0.11", + "version": "1.0.12", "license": "MIT", "dependencies": { "@microsoft/kiota-abstractions": "^1.0.22", diff --git a/http/typescript/fetch/package.json b/http/typescript/fetch/package.json index 01b1d2c26d..60d71de434 100644 --- a/http/typescript/fetch/package.json +++ b/http/typescript/fetch/package.json @@ -1,6 +1,6 @@ { "name": "@microsoft/kiota-http-fetchlibrary", - "version": "1.0.11", + "version": "1.0.12", "description": "Kiota request adapter implementation with fetch", "main": "dist/index.js", "types": "dist/index.d.ts", @@ -29,7 +29,7 @@ "registry": "https://npm.pkg.github.com" }, "dependencies": { - "@microsoft/kiota-abstractions": "^1.0.22", + "@microsoft/kiota-abstractions": "^1.0.23", "cross-fetch": "^3.1.4", "web-streams-polyfill": "^3.1.1" }, diff --git a/http/typescript/fetch/src/fetchRequestAdapter.ts b/http/typescript/fetch/src/fetchRequestAdapter.ts index 785d24e327..fb114f1284 100644 --- a/http/typescript/fetch/src/fetchRequestAdapter.ts +++ b/http/typescript/fetch/src/fetchRequestAdapter.ts @@ -3,6 +3,8 @@ import { Headers as FetchHeadersCtor } from 'cross-fetch'; import { ReadableStream } from 'web-streams-polyfill'; import { HttpClient } from './httpClient'; export class FetchRequestAdapter implements RequestAdapter { + /** The base url for every request. */ + public baseUrl: string = ''; public getSerializationWriterFactory(): SerializationWriterFactory { return this.serializationWriterFactory; } @@ -176,6 +178,7 @@ export class FetchRequestAdapter implements RequestAdapter { return await this.httpClient.fetch(requestInfo.URL.toString(), request); } private getRequestFromRequestInformation = (requestInfo: RequestInformation): RequestInit => { + requestInfo.pathParameters.set("baseurl", this.baseUrl); const request = { method: requestInfo.httpMethod?.toString(), headers: new FetchHeadersCtor(), diff --git a/src/Kiota.Builder/CodeDOM/CodeMethod.cs b/src/Kiota.Builder/CodeDOM/CodeMethod.cs index 4e518a7e7c..a281a7fb5d 100644 --- a/src/Kiota.Builder/CodeDOM/CodeMethod.cs +++ b/src/Kiota.Builder/CodeDOM/CodeMethod.cs @@ -79,6 +79,11 @@ public bool IsSerializationMethod { /// The original indexer codedom element this method replaces when it is of kind IndexerBackwardCompatibility. /// public CodeIndexer OriginalIndexer { get; set; } + /// + /// The base url for every request read from the servers property on the description. + /// Only provided for constructor on Api client + /// + public string BaseUrl { get; set; } public object Clone() { @@ -92,6 +97,7 @@ public object Clone() IsStatic = IsStatic, Description = Description?.Clone() as string, ContentType = ContentType?.Clone() as string, + BaseUrl = BaseUrl?.Clone() as string, AccessedProperty = AccessedProperty, SerializerModules = SerializerModules == null ? null : new (SerializerModules), DeserializerModules = DeserializerModules == null ? null : new (DeserializerModules), diff --git a/src/Kiota.Builder/Extensions/OpenApiUrlTreeNodeExtensions.cs b/src/Kiota.Builder/Extensions/OpenApiUrlTreeNodeExtensions.cs index 83e4811c75..1e12c864dc 100644 --- a/src/Kiota.Builder/Extensions/OpenApiUrlTreeNodeExtensions.cs +++ b/src/Kiota.Builder/Extensions/OpenApiUrlTreeNodeExtensions.cs @@ -89,8 +89,7 @@ public static bool IsComplexPathWithAnyNumberOfParameters(this OpenApiUrlTreeNod { return (currentNode?.Segment?.Contains(requestParametersSectionChar) ?? false) && currentNode.Segment.EndsWith(requestParametersSectionEndChar); } - public static string GetUrlTemplate(this OpenApiUrlTreeNode currentNode, string rootUrl) { - if(string.IsNullOrEmpty(rootUrl)) throw new ArgumentNullException(nameof(rootUrl)); + public static string GetUrlTemplate(this OpenApiUrlTreeNode currentNode) { var queryStringParameters = string.Empty; if(currentNode.HasOperations(Constants.DefaultOpenApiLabel)) { @@ -106,7 +105,7 @@ public static string GetUrlTemplate(this OpenApiUrlTreeNode currentNode, string .Aggregate((x, y) => $"{x},{y}") + '}'; } - return rootUrl + + return "{+baseurl}" + SanitizePathParameterNames(currentNode.Path.Replace('\\', '/')) + queryStringParameters; } diff --git a/src/Kiota.Builder/KiotaBuilder.cs b/src/Kiota.Builder/KiotaBuilder.cs index 81aa33927f..fbc2ad7b0f 100644 --- a/src/Kiota.Builder/KiotaBuilder.cs +++ b/src/Kiota.Builder/KiotaBuilder.cs @@ -321,7 +321,7 @@ private void CreateUrlManagement(CodeClass currentClass, OpenApiUrlTreeNode curr var pathProperty = new CodeProperty { Access = AccessModifier.Private, Name = "urlTemplate", - DefaultValue = $"\"{currentNode.GetUrlTemplate(config.ApiRootUrl)}\"", + DefaultValue = $"\"{currentNode.GetUrlTemplate()}\"", ReadOnly = true, Description = "Url template to use to build the URL for the current request builder", PropertyKind = CodePropertyKind.UrlTemplate, @@ -371,6 +371,7 @@ private void CreateUrlManagement(CodeClass currentClass, OpenApiUrlTreeNode curr if(isApiClientClass) { constructor.SerializerModules = config.Serializers; constructor.DeserializerModules = config.Deserializers; + constructor.BaseUrl = config.ApiRootUrl; pathParametersProperty.DefaultValue = $"new {pathParametersProperty.Type.Name}()"; } else { constructor.AddParameter(new CodeParameter { diff --git a/src/Kiota.Builder/Writers/CSharp/CodeMethodWriter.cs b/src/Kiota.Builder/Writers/CSharp/CodeMethodWriter.cs index cb71130181..85abef5d2d 100644 --- a/src/Kiota.Builder/Writers/CSharp/CodeMethodWriter.cs +++ b/src/Kiota.Builder/Writers/CSharp/CodeMethodWriter.cs @@ -81,6 +81,7 @@ private static void WriteApiConstructorBody(CodeClass parentClass, CodeMethod me var requestAdapterPropertyName = requestAdapterProperty.Name.ToFirstCharacterUpperCase(); WriteSerializationRegistration(method.SerializerModules, writer, "RegisterDefaultSerializer"); WriteSerializationRegistration(method.DeserializerModules, writer, "RegisterDefaultDeserializer"); + writer.WriteLine($"{requestAdapterPropertyName}.BaseUrl = \"{method.BaseUrl}\";"); if(backingStoreParameter != null) writer.WriteLine($"{requestAdapterPropertyName}.EnableBackingStore({backingStoreParameter.Name});"); } diff --git a/src/Kiota.Builder/Writers/Go/CodeMethodWriter.cs b/src/Kiota.Builder/Writers/Go/CodeMethodWriter.cs index bb114c3b10..ceca2bbf88 100644 --- a/src/Kiota.Builder/Writers/Go/CodeMethodWriter.cs +++ b/src/Kiota.Builder/Writers/Go/CodeMethodWriter.cs @@ -195,6 +195,7 @@ private void WriteApiConstructorBody(CodeClass parentClass, CodeMethod method, L var backingStoreParameter = method.Parameters.FirstOrDefault(x => x.IsOfKind(CodeParameterKind.BackingStore)); WriteSerializationRegistration(method.SerializerModules, writer, parentClass, "RegisterDefaultSerializer", "SerializationWriterFactory"); WriteSerializationRegistration(method.DeserializerModules, writer, parentClass, "RegisterDefaultDeserializer", "ParseNodeFactory"); + writer.WriteLine($"m.{requestAdapterPropertyName}.SetBaseUrl(\"{method.BaseUrl}\")"); if(backingStoreParameter != null) writer.WriteLine($"m.{requestAdapterPropertyName}.EnableBackingStore({backingStoreParameter.Name});"); } diff --git a/src/Kiota.Builder/Writers/Java/CodeMethodWriter.cs b/src/Kiota.Builder/Writers/Java/CodeMethodWriter.cs index 1f34aedc01..37a2a23dc6 100644 --- a/src/Kiota.Builder/Writers/Java/CodeMethodWriter.cs +++ b/src/Kiota.Builder/Writers/Java/CodeMethodWriter.cs @@ -105,6 +105,7 @@ private static void WriteApiConstructorBody(CodeClass parentClass, CodeMethod me var requestAdapterPropertyName = requestAdapterProperty.Name.ToFirstCharacterLowerCase(); WriteSerializationRegistration(method.SerializerModules, writer, "registerDefaultSerializer"); WriteSerializationRegistration(method.DeserializerModules, writer, "registerDefaultDeserializer"); + writer.WriteLine($"{requestAdapterPropertyName}.setBaseUrl(\"{method.BaseUrl}\");"); if(backingStoreParameter != null) writer.WriteLine($"this.{requestAdapterPropertyName}.enableBackingStore({backingStoreParameter.Name});"); } diff --git a/src/Kiota.Builder/Writers/Ruby/CodeMethodWriter.cs b/src/Kiota.Builder/Writers/Ruby/CodeMethodWriter.cs index e63161245c..cd90fcb3b1 100644 --- a/src/Kiota.Builder/Writers/Ruby/CodeMethodWriter.cs +++ b/src/Kiota.Builder/Writers/Ruby/CodeMethodWriter.cs @@ -80,6 +80,7 @@ private static void WriteApiConstructorBody(CodeClass parentClass, CodeMethod me var requestAdapterParameter = method.Parameters.FirstOrDefault(x => x.IsOfKind(CodeParameterKind.RequestAdapter)); var requestAdapterPropertyName = requestAdapterProperty.Name.ToSnakeCase(); writer.WriteLine($"@{requestAdapterPropertyName} = {requestAdapterParameter.Name.ToSnakeCase()}"); + writer.WriteLine($"{requestAdapterPropertyName}.set_base_url('{method.BaseUrl}')"); } private static void WriteConstructorBody(CodeClass parentClass, CodeMethod currentMethod, LanguageWriter writer, bool inherits) { if(inherits) diff --git a/src/Kiota.Builder/Writers/TypeScript/CodeMethodWriter.cs b/src/Kiota.Builder/Writers/TypeScript/CodeMethodWriter.cs index 752b117547..c643ca56e2 100644 --- a/src/Kiota.Builder/Writers/TypeScript/CodeMethodWriter.cs +++ b/src/Kiota.Builder/Writers/TypeScript/CodeMethodWriter.cs @@ -95,6 +95,7 @@ private static void WriteApiConstructorBody(CodeClass parentClass, CodeMethod me var requestAdapterPropertyName = requestAdapterProperty.Name.ToFirstCharacterLowerCase(); WriteSerializationRegistration(method.SerializerModules, writer, "registerDefaultSerializer"); WriteSerializationRegistration(method.DeserializerModules, writer, "registerDefaultDeserializer"); + writer.WriteLine($"{requestAdapterPropertyName}.baseUrl = \"{method.BaseUrl}\";"); if(backingStoreParameter != null) writer.WriteLine($"this.{requestAdapterPropertyName}.enableBackingStore({backingStoreParameter.Name});"); }