Skip to content
This repository has been archived by the owner on Feb 29, 2020. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
phvannor committed Oct 22, 2015
2 parents 0611b44 + b0b2ffc commit 1b11c2b
Show file tree
Hide file tree
Showing 28 changed files with 496 additions and 143 deletions.
Expand Up @@ -12,6 +12,7 @@
using System.Net.Http.Headers;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;

Expand Down Expand Up @@ -197,7 +198,8 @@ public async Task<string> RequestWithoutHandlersAsync(HttpMethod method, string
/// <param name="features">
/// Value indicating which features of the SDK are being used in this call. Useful for telemetry.
/// </param>
/// <returns>
/// <param name="cancellationToken">The <see cref="System.Threading.CancellationToken"/> token to observe</param>
/// <returns>
/// The response.
/// </returns>
public Task<MobileServiceHttpResponse> RequestAsync(HttpMethod method,
Expand All @@ -206,10 +208,11 @@ public async Task<string> RequestWithoutHandlersAsync(HttpMethod method, string
string content = null,
bool ensureResponseContent = true,
IDictionary<string, string> requestHeaders = null,
MobileServiceFeatures features = MobileServiceFeatures.None)
MobileServiceFeatures features = MobileServiceFeatures.None,
CancellationToken cancellationToken = default(CancellationToken))
{
requestHeaders = FeaturesHelper.AddFeaturesHeader(requestHeaders, features);
return this.RequestAsync(true, method, uriPathAndQuery, user, content, ensureResponseContent, requestHeaders);
return this.RequestAsync(true, method, uriPathAndQuery, user, content, ensureResponseContent, requestHeaders, cancellationToken);
}

/// <summary>
Expand Down Expand Up @@ -237,6 +240,7 @@ public async Task<string> RequestWithoutHandlersAsync(HttpMethod method, string
/// <param name="requestHeaders">
/// Additional request headers to include with the request.
/// </param>
/// <param name="cancellationToken">The <see cref="System.Threading.CancellationToken"/> token to observe</param>
/// <returns>
/// The content of the response as a string.
/// </returns>
Expand All @@ -246,7 +250,8 @@ public async Task<string> RequestWithoutHandlersAsync(HttpMethod method, string
MobileServiceUser user,
string content = null,
bool ensureResponseContent = true,
IDictionary<string, string> requestHeaders = null)
IDictionary<string, string> requestHeaders = null,
CancellationToken cancellationToken = default(CancellationToken))
{
Debug.Assert(method != null);
Debug.Assert(!string.IsNullOrEmpty(uriPathAndQuery));
Expand All @@ -266,7 +271,7 @@ public async Task<string> RequestWithoutHandlersAsync(HttpMethod method, string
{
client = this.httpClientSansHandlers;
}
HttpResponseMessage response = await this.SendRequestAsync(client, request, ensureResponseContent);
HttpResponseMessage response = await this.SendRequestAsync(client, request, ensureResponseContent, cancellationToken);
string responseContent = await GetResponseContent(response);
string etag = null;
if (response.Headers.ETag != null)
Expand Down Expand Up @@ -310,10 +315,17 @@ public async Task<string> RequestWithoutHandlersAsync(HttpMethod method, string
/// <param name="features">
/// Value indicating which features of the SDK are being used in this call. Useful for telemetry.
/// </param>
/// <param name="cancellationToken">The <see cref="System.Threading.CancellationToken"/> token to observe</param>
/// <returns>
/// An <see cref="HttpResponseMessage"/>.
/// </returns>
public async Task<HttpResponseMessage> RequestAsync(HttpMethod method, string uriPathAndQuery, MobileServiceUser user, HttpContent content, IDictionary<string, string> requestHeaders, MobileServiceFeatures features = MobileServiceFeatures.None)
public async Task<HttpResponseMessage> RequestAsync(HttpMethod method,
string uriPathAndQuery,
MobileServiceUser user,
HttpContent content,
IDictionary<string, string> requestHeaders,
MobileServiceFeatures features = MobileServiceFeatures.None,
CancellationToken cancellationToken = default(CancellationToken))
{
Debug.Assert(method != null);
Debug.Assert(!string.IsNullOrEmpty(uriPathAndQuery));
Expand All @@ -323,7 +335,7 @@ public async Task<HttpResponseMessage> RequestAsync(HttpMethod method, string ur
HttpRequestMessage request = this.CreateHttpRequestMessage(method, uriPathAndQuery, requestHeaders, content, user);

// Get the response
HttpResponseMessage response = await this.SendRequestAsync(httpClient, request, ensureResponseContent: false);
HttpResponseMessage response = await this.SendRequestAsync(httpClient, request, ensureResponseContent: false, cancellationToken: cancellationToken);

return response;
}
Expand Down Expand Up @@ -577,16 +589,20 @@ private HttpRequestMessage CreateHttpRequestMessage(HttpMethod method, string ur
/// <param name="ensureResponseContent">
/// Optional parameter to indicate if the response should include content.
/// </param>
/// <param name="cancellationToken">The <see cref="System.Threading.CancellationToken"/> token to observe</param>
/// <returns>
/// An <see cref="HttpResponseMessage"/>.
/// </returns>
private async Task<HttpResponseMessage> SendRequestAsync(HttpClient client, HttpRequestMessage request, bool ensureResponseContent)
private async Task<HttpResponseMessage> SendRequestAsync(HttpClient client,
HttpRequestMessage request,
bool ensureResponseContent,
CancellationToken cancellationToken)
{
Debug.Assert(client != null);
Debug.Assert(request != null);

// Send the request and get the response back as string
HttpResponseMessage response = await client.SendAsync(request);
HttpResponseMessage response = await client.SendAsync(request, cancellationToken);

// Throw errors for any failing responses
if (!response.IsSuccessStatusCode)
Expand Down Expand Up @@ -756,4 +772,4 @@ private static string FeaturesToString(MobileServiceFeatures features)
}
}
}
}
}
Expand Up @@ -8,6 +8,7 @@
using System.Threading.Tasks;
using Microsoft.WindowsAzure.MobileServices.Sync;
using Newtonsoft.Json.Linq;
using System.Threading;

namespace Microsoft.WindowsAzure.MobileServices
{
Expand Down Expand Up @@ -157,8 +158,9 @@ public interface IMobileServiceClient
/// </summary>
/// <typeparam name="T">The type of instance returned from the Microsoft Azure Mobile Service.</typeparam>
/// <param name="apiName">The name of the custom API.</param>
/// <param name="cancellationToken">The <see cref="System.Threading.CancellationToken"/> token to observe</param>
/// <returns>The response content from the custom api invocation.</returns>
Task<T> InvokeApiAsync<T>(string apiName);
Task<T> InvokeApiAsync<T>(string apiName, CancellationToken cancellationToken = default(CancellationToken));

/// <summary>
/// Invokes a user-defined custom API of a Microsoft Azure Mobile Service using an HTTP POST with
Expand All @@ -168,8 +170,9 @@ public interface IMobileServiceClient
/// <typeparam name="U">The type of instance returned from the Microsoft Azure Mobile Service.</typeparam>
/// <param name="apiName">The name of the custom API.</param>
/// <param name="body">The value to be sent as the HTTP body.</param>
/// <param name="cancellationToken">The <see cref="System.Threading.CancellationToken"/> token to observe</param>
/// <returns>The response content from the custom api invocation.</returns>
Task<U> InvokeApiAsync<T, U>(string apiName, T body);
Task<U> InvokeApiAsync<T, U>(string apiName, T body, CancellationToken cancellationToken = default(CancellationToken));

/// <summary>
/// Invokes a user-defined custom API of a Microsoft Azure Mobile Service using the specified HTTP Method.
Expand All @@ -181,8 +184,9 @@ public interface IMobileServiceClient
/// <param name="parameters">
/// A dictionary of user-defined parameters and values to include in the request URI query string.
/// </param>
/// <param name="cancellationToken">The <see cref="System.Threading.CancellationToken"/> token to observe</param>
/// <returns>The response content from the custom api invocation.</returns>
Task<T> InvokeApiAsync<T>(string apiName, HttpMethod method, IDictionary<string, string> parameters);
Task<T> InvokeApiAsync<T>(string apiName, HttpMethod method, IDictionary<string, string> parameters, CancellationToken cancellationToken = default(CancellationToken));

/// <summary>
/// Invokes a user-defined custom API of a Microsoft Azure Mobile Service using the specified HTTP Method.
Expand All @@ -196,24 +200,27 @@ public interface IMobileServiceClient
/// <param name="parameters">
/// A dictionary of user-defined parameters and values to include in the request URI query string.
/// </param>
/// <param name="cancellationToken">The <see cref="System.Threading.CancellationToken"/> token to observe</param>
/// <returns>The response content from the custom api invocation.</returns>
Task<U> InvokeApiAsync<T, U>(string apiName, T body, HttpMethod method, IDictionary<string, string> parameters);
Task<U> InvokeApiAsync<T, U>(string apiName, T body, HttpMethod method, IDictionary<string, string> parameters, CancellationToken cancellationToken = default(CancellationToken));

/// <summary>
/// Invokes a user-defined custom API of a Microsoft Azure Mobile Service using an HTTP POST.
/// </summary>
/// <param name="apiName">The name of the custom API.</param>
/// <param name="cancellationToken">The <see cref="System.Threading.CancellationToken"/> token to observe</param>
/// <returns></returns>
Task<JToken> InvokeApiAsync(string apiName);
Task<JToken> InvokeApiAsync(string apiName, CancellationToken cancellationToken = default(CancellationToken));

/// <summary>
/// Invokes a user-defined custom API of a Microsoft Azure Mobile Service using an HTTP POST, with
/// support for sending HTTP content.
/// </summary>
/// <param name="apiName">The name of the custom API.</param>
/// <param name="body">The value to be sent as the HTTP body.</param>
/// <param name="cancellationToken">The <see cref="System.Threading.CancellationToken"/> token to observe</param>
/// <returns></returns>
Task<JToken> InvokeApiAsync(string apiName, JToken body);
Task<JToken> InvokeApiAsync(string apiName, JToken body, CancellationToken cancellationToken = default(CancellationToken));

/// <summary>
/// Invokes a user-defined custom API of a Microsoft Azure Mobile Service using the specified HTTP Method.
Expand All @@ -224,8 +231,9 @@ public interface IMobileServiceClient
/// <param name="parameters">
/// A dictionary of user-defined parameters and values to include in the request URI query string.
/// </param>
/// <param name="cancellationToken">The <see cref="System.Threading.CancellationToken"/> token to observe</param>
/// <returns>The response content from the custom api invocation.</returns>
Task<JToken> InvokeApiAsync(string apiName, HttpMethod method, IDictionary<string, string> parameters);
Task<JToken> InvokeApiAsync(string apiName, HttpMethod method, IDictionary<string, string> parameters, CancellationToken cancellationToken = default(CancellationToken));

/// <summary>
/// Invokes a user-defined custom API of a Microsoft Azure Mobile Service using the specified HTTP method.
Expand All @@ -237,9 +245,10 @@ public interface IMobileServiceClient
/// <param name="parameters">
/// A dictionary of user-defined parameters and values to include in the request URI query string.
/// </param>
/// <param name="cancellationToken">The <see cref="System.Threading.CancellationToken"/> token to observe</param>
/// <returns>The response content from the custom api invocation.</returns>
Task<JToken> InvokeApiAsync(string apiName, JToken body, HttpMethod method, IDictionary<string, string> parameters);
Task<JToken> InvokeApiAsync(string apiName, JToken body, HttpMethod method, IDictionary<string, string> parameters, CancellationToken cancellationToken = default(CancellationToken));

/// <summary>
/// Invokes a user-defined custom API of a Microsoft Azure Mobile Service using the specified HttpMethod.
/// Additional data can be sent though the HTTP content or the query string.
Expand All @@ -253,8 +262,9 @@ public interface IMobileServiceClient
/// <param name="parameters">
/// A dictionary of user-defined parameters and values to include in the request URI query string.
/// </param>
/// <param name="cancellationToken">The <see cref="System.Threading.CancellationToken"/> token to observe</param>
/// <returns>The HTTP Response from the custom api invocation.</returns>
Task<HttpResponseMessage> InvokeApiAsync(string apiName, HttpContent content, HttpMethod method, IDictionary<string, string> requestHeaders, IDictionary<string, string> parameters);
Task<HttpResponseMessage> InvokeApiAsync(string apiName, HttpContent content, HttpMethod method, IDictionary<string, string> requestHeaders, IDictionary<string, string> parameters, CancellationToken cancellationToken = default(CancellationToken));

/// <summary>
/// Gets or sets the settings used for serialization.
Expand Down

0 comments on commit 1b11c2b

Please sign in to comment.