Skip to content
This repository has been archived by the owner on Aug 2, 2024. It is now read-only.

(#883) Expose the underlying HttpClient for the service connection. #887

Merged
merged 1 commit into from
Jan 30, 2024
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
17 changes: 11 additions & 6 deletions sdk/dotnet/src/Microsoft.Datasync.Client/DatasyncClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public DatasyncClient(Uri endpoint, AuthenticationProvider authenticationProvide

Endpoint = endpoint.NormalizeEndpoint();
ClientOptions = clientOptions ?? new DatasyncClientOptions();
HttpClient = new ServiceHttpClient(Endpoint, authenticationProvider, ClientOptions);
ServiceHttpClient = new ServiceHttpClient(Endpoint, authenticationProvider, ClientOptions);
if (ClientOptions.SerializerSettings != null)
{
Serializer.SerializerSettings = ClientOptions.SerializerSettings;
Expand All @@ -147,13 +147,18 @@ public DatasyncClient(Uri endpoint, AuthenticationProvider authenticationProvide
/// <summary>
/// Gets the <see cref="ServiceHttpClient"/> associated with the Azure Mobile App.
/// </summary>
internal ServiceHttpClient HttpClient { get; }
internal ServiceHttpClient ServiceHttpClient { get; }

/// <summary>
/// The underlying <see cref="HttpClient"/> that is used to communicate with the service.
/// </summary>
public HttpClient HttpClient { get => ServiceHttpClient.HttpClient; }

/// <summary>
/// The id used to identify this installation of the application to
/// provide telemetry data.
/// </summary>
public string InstallationId { get => HttpClient.InstallationId; }
public string InstallationId { get => ServiceHttpClient.InstallationId; }

/// <summary>
/// The number of pending operations, or null if the offline store has not been defined.
Expand Down Expand Up @@ -311,7 +316,7 @@ public virtual async Task PushTablesAsync(IEnumerable<string> tables, PushOption
/// <param name="cancellationToken">A <see cref="CancellationToken"/> to observe.</param>
/// <returns>A task that returns the <see cref="HttpResponseMessage"/> for the response when complete.</returns>
public Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, HttpCompletionOption completionOption, CancellationToken cancellationToken = default)
=> HttpClient.HttpClient.SendAsync(request, completionOption, cancellationToken);
=> ServiceHttpClient.HttpClient.SendAsync(request, completionOption, cancellationToken);

/// <summary>
/// Sends a HTTP request to the remote service, using the in-built HTTP client.
Expand All @@ -320,7 +325,7 @@ public Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, HttpCompl
/// <param name="cancellationToken">A <see cref="CancellationToken"/> to observe.</param>
/// <returns>A task that returns the <see cref="HttpResponseMessage"/> for the response when complete.</returns>
public Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken = default)
=> HttpClient.HttpClient.SendAsync(request, cancellationToken);
=> ServiceHttpClient.HttpClient.SendAsync(request, cancellationToken);
#endregion

/// <summary>
Expand Down Expand Up @@ -357,7 +362,7 @@ protected virtual void Dispose(bool disposing)
if (disposing)
{
SyncContext.Dispose();
HttpClient.Dispose();
ServiceHttpClient.Dispose();
}
}
#endregion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ protected async Task<JToken> SendRequestAsync(ServiceRequest request, Cancellati
{
try
{
var response = await ServiceClient.HttpClient.SendAsync(request, cancellationToken).ConfigureAwait(false);
var response = await ServiceClient.ServiceHttpClient.SendAsync(request, cancellationToken).ConfigureAwait(false);
return GetJTokenFromResponse(response);
}
catch (DatasyncInvalidOperationException ex) when (ex.IsConflictStatusCode())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public void CtorString_Valid_SetsEndpoint(EndpointTestCase testcase)
var client = new DatasyncClient(testcase.BaseEndpoint);
Assert.Equal(testcase.NormalizedEndpoint, client.Endpoint.ToString());
Assert.NotNull(client.ClientOptions);
Assert.NotNull(client.HttpClient);
Assert.NotNull(client.ServiceHttpClient);
}

[Theory, ClassData(typeof(EndpointTestCases))]
Expand All @@ -94,7 +94,7 @@ public void CtorStringAuth_Valid_SetsEndpoint(EndpointTestCase testcase)
var client = new DatasyncClient(testcase.BaseEndpoint, authProvider);
Assert.Equal(testcase.NormalizedEndpoint, client.Endpoint.ToString());
Assert.NotNull(client.ClientOptions);
Assert.NotNull(client.HttpClient);
Assert.NotNull(client.ServiceHttpClient);
}

[Fact]
Expand All @@ -120,7 +120,7 @@ public void CtorUri_Valid_SetsEndpoint(EndpointTestCase testcase)
var client = new DatasyncClient(new Uri(testcase.BaseEndpoint));
Assert.Equal(testcase.NormalizedEndpoint, client.Endpoint.ToString());
Assert.NotNull(client.ClientOptions);
Assert.NotNull(client.HttpClient);
Assert.NotNull(client.ServiceHttpClient);
}

[Theory, ClassData(typeof(EndpointTestCases))]
Expand All @@ -131,7 +131,7 @@ public void CtorUriAuth_Valid_SetsEndpoint(EndpointTestCase testcase)
var client = new DatasyncClient(new Uri(testcase.BaseEndpoint), authProvider);
Assert.Equal(testcase.NormalizedEndpoint, client.Endpoint.ToString());
Assert.NotNull(client.ClientOptions);
Assert.NotNull(client.HttpClient);
Assert.NotNull(client.ServiceHttpClient);
}

[Fact]
Expand Down Expand Up @@ -160,7 +160,7 @@ public void CtorStringOptions_Valid_SetsEndpoint(EndpointTestCase testcase)
var client = new DatasyncClient(testcase.BaseEndpoint, options);
Assert.Equal(testcase.NormalizedEndpoint, client.Endpoint.ToString());
Assert.Same(options, client.ClientOptions);
Assert.NotNull(client.HttpClient);
Assert.NotNull(client.ServiceHttpClient);
}

[Theory, ClassData(typeof(EndpointTestCases))]
Expand All @@ -172,7 +172,7 @@ public void CtorStringAuthOptions_Valid_SetsEndpoint(EndpointTestCase testcase)
var client = new DatasyncClient(testcase.BaseEndpoint, authProvider, options);
Assert.Equal(testcase.NormalizedEndpoint, client.Endpoint.ToString());
Assert.Same(options, client.ClientOptions);
Assert.NotNull(client.HttpClient);
Assert.NotNull(client.ServiceHttpClient);
}

[Fact]
Expand All @@ -199,7 +199,7 @@ public void CtorUriOptions_Valid_SetsEndpoint(EndpointTestCase testcase)
var client = new DatasyncClient(new Uri(testcase.BaseEndpoint), options);
Assert.Equal(testcase.NormalizedEndpoint, client.Endpoint.ToString());
Assert.Same(options, client.ClientOptions);
Assert.NotNull(client.HttpClient);
Assert.NotNull(client.ServiceHttpClient);
}

[Theory, ClassData(typeof(EndpointTestCases))]
Expand All @@ -211,7 +211,7 @@ public void CtorUriAuthOptions_Valid_SetsEndpoint(EndpointTestCase testcase)
var client = new DatasyncClient(new Uri(testcase.BaseEndpoint), authProvider, options);
Assert.Equal(testcase.NormalizedEndpoint, client.Endpoint.ToString());
Assert.Same(options, client.ClientOptions);
Assert.NotNull(client.HttpClient);
Assert.NotNull(client.ServiceHttpClient);
}

[Fact]
Expand Down
Loading