Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adds support for base url #795

Merged
merged 9 commits into from
Nov 8, 2021
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: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 4 additions & 0 deletions abstractions/dotnet/src/IRequestAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,9 @@ public interface IRequestAdapter
/// <param name="responseHandler">The response handler to use for the HTTP request instead of the default handler.</param>
/// <returns>A Task to await completion.</returns>
Task SendNoContentAsync(RequestInformation requestInfo, IResponseHandler responseHandler = default);
/// <summary>
/// The base url for every request.
/// </summary>
string BaseUrl { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<TargetFramework>net5.0</TargetFramework>
<PublishRepositoryUrl>true</PublishRepositoryUrl>
<RepositoryUrl>https://github.com/microsoft/kiota</RepositoryUrl>
<Version>1.0.22</Version>
<Version>1.0.23</Version>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<!-- Enable this line once we go live to prevent breaking changes -->
Expand Down
4 changes: 4 additions & 0 deletions abstractions/go/request_adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion abstractions/java/lib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ publishing {
publications {
gpr(MavenPublication) {
artifactId 'kiota-abstractions'
version '1.0.22'
version '1.0.23'
from(components.java)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,15 @@ public interface RequestAdapter {
* @return a {@link CompletableFuture} with the deserialized primitive collection response model.
*/
<ModelType> CompletableFuture<Iterable<ModelType>> sendPrimitiveCollectionAsync(@Nonnull final RequestInformation requestInfo, @Nonnull final Class<ModelType> 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();
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module MicrosoftKiotaAbstractions
VERSION = "0.1.10"
VERSION = "0.1.11"
end
4 changes: 2 additions & 2 deletions abstractions/typescript/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion abstractions/typescript/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 2 additions & 0 deletions abstractions/typescript/src/requestAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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()
{
Expand All @@ -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<RedirectHandlerOption>();
// Assert
Assert.NotNull(extractedOption);
Expand All @@ -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);
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -14,13 +15,15 @@ public class TelemetryHandlerTests
{
private readonly HttpMessageInvoker _invoker;

private readonly HttpClientRequestAdapter requestAdapter;
public TelemetryHandlerTests()
{
var telemetryHandler = new TelemetryHandler
{
InnerHandler = new FakeSuccessHandler()
};
this._invoker = new HttpMessageInvoker(telemetryHandler);
requestAdapter = new HttpClientRequestAdapter(new AnonymousAuthenticationProvider());
}

[Fact]
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<IAuthenticationProvider>().Object;
requestAdapter = new HttpClientRequestAdapter(new AnonymousAuthenticationProvider());
}

[Fact]
Expand Down Expand Up @@ -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);
Expand Down
7 changes: 6 additions & 1 deletion http/dotnet/httpclient/src/HttpClientRequestAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ public ISerializationWriterFactory SerializationWriterFactory
}
}
/// <summary>
/// The base url for every request.
/// </summary>
public string BaseUrl { get; set; }
/// <summary>
/// Send a <see cref="RequestInformation"/> instance with a collection instance of <typeparam name="ModelType"></typeparam>
/// </summary>
/// <param name="requestInfo">The <see cref="RequestInformation"/> instance to send</param>
Expand Down Expand Up @@ -199,8 +203,9 @@ private async Task<HttpResponseMessage> 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()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
<TargetFramework>net5.0</TargetFramework>
<PublishRepositoryUrl>true</PublishRepositoryUrl>
<RepositoryUrl>https://github.com/microsoft/kiota</RepositoryUrl>
<Version>1.0.11</Version>
<Version>1.0.12</Version>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<!-- Enable this line once we go live to prevent breaking changes -->
<!-- <PackageValidationBaselineVersion>1.0.0</PackageValidationBaselineVersion> -->
<GenerateDocumentationFile>true</GenerateDocumentationFile>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Kiota.Abstractions" Version="1.0.22" />
<PackageReference Include="Microsoft.Kiota.Abstractions" Version="1.0.23" />
</ItemGroup>

</Project>
2 changes: 1 addition & 1 deletion http/go/nethttp/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
10 changes: 10 additions & 0 deletions http/go/nethttp/nethttp_request_adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -73,6 +75,7 @@ func NewNetHttpRequestAdapterWithParseNodeFactoryAndSerializationWriterFactoryAn
parseNodeFactory: parseNodeFactory,
httpClient: httpClient,
authenticationProvider: authenticationProvider,
baseUrl: "",
}
if result.httpClient == nil {
defaultClient := GetDefaultClient()
Expand All @@ -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 {
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions http/java/okhttp/lib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -53,7 +53,7 @@ publishing {
publications {
gpr(MavenPublication) {
artifactId 'kiota-http-okhttplibrary'
version '1.0.11'
version '1.0.12'
from(components.java)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -214,6 +222,7 @@ private CompletableFuture<Response> 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
Expand Down
Loading