예제 #1
0
        /// <summary>
        /// Performs a web request and includes the standard Mobile Services
        /// headers. It will use an HttpClient without any http handlers.
        /// </summary>
        /// <param name="method">
        /// The HTTP method used to request the resource.
        /// </param>
        /// <param name="uriPathAndQuery">
        /// The URI of the resource to request (relative to the Mobile Services
        /// runtime).
        /// </param>
        /// <param name="user">
        /// The object representing the user on behalf of whom the request will be sent.
        /// </param>
        /// <param name="content">
        /// Optional content to send to the resource.
        /// </param>
        /// <param name="features">
        /// Optional MobileServiceFeatures used for telemetry purpose.
        /// </param>>
        /// <returns>
        /// The content of the response as a string.
        /// </returns>
        public async Task <string> RequestWithoutHandlersAsync(HttpMethod method, string uriPathAndQuery, MobileServiceUser user, string content = null, MobileServiceFeatures features = MobileServiceFeatures.None)
        {
            IDictionary <string, string> requestHeaders = FeaturesHelper.AddFeaturesHeader(requestHeaders: null, features: features);
            MobileServiceHttpResponse    response       = await this.RequestAsync(false, method, uriPathAndQuery, user, content, false, requestHeaders);

            return(response.Content);
        }
 /// <summary>
 /// Makes an HTTP request that includes the standard Mobile Services
 /// headers. It will use an HttpClient with user-defined http handlers.
 /// </summary>
 /// <param name="method">
 /// The HTTP method used to request the resource.
 /// </param>
 /// <param name="uriPathAndQuery">
 /// The URI of the resource to request (relative to the Mobile Services
 /// runtime).
 /// </param>
 /// <param name="user">
 /// The object representing the user on behalf of whom the request will be sent.
 /// </param>
 /// <param name="content">
 /// Optional content to send to the resource.
 /// </param>
 /// <param name="ensureResponseContent">
 /// Optional parameter to indicate if the response should include content.
 /// </param>
 /// <param name="requestHeaders">
 /// Additional request headers to include with the request.
 /// </param>
 /// <param name="features">
 /// Value indicating which features of the SDK are being used in this call. Useful for telemetry.
 /// </param>
 /// <returns>
 /// The response.
 /// </returns>
 public Task <MobileServiceHttpResponse> RequestAsync(HttpMethod method,
                                                      string uriPathAndQuery,
                                                      MobileServiceUser user,
                                                      string content             = null,
                                                      bool ensureResponseContent = true,
                                                      IDictionary <string, string> requestHeaders = null,
                                                      MobileServiceFeatures features = MobileServiceFeatures.None)
 {
     requestHeaders = FeaturesHelper.AddFeaturesHeader(requestHeaders, features);
     return(this.RequestAsync(true, method, uriPathAndQuery, user, content, ensureResponseContent, requestHeaders));
 }
        /// <summary>
        /// Makes an HTTP request that includes the standard Mobile Services
        /// headers. It will use an HttpClient with user-defined http handlers.
        /// </summary>
        /// <param name="method">
        /// The HTTP method used to request the resource.
        /// </param>
        /// <param name="uriPathAndQuery">
        /// The URI of the resource to request (relative to the Mobile Services
        /// runtime).
        /// </param>
        /// <param name="user">
        /// The object representing the user on behalf of whom the request will be sent.
        /// </param>
        /// <param name="content">
        /// Content to send to the resource.
        /// </param>
        /// <param name="requestHeaders">
        /// Additional request headers to include with the request.
        /// </param>
        /// <param name="features">
        /// Value indicating which features of the SDK are being used in this call. Useful for telemetry.
        /// </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)
        {
            Debug.Assert(method != null);
            Debug.Assert(!string.IsNullOrEmpty(uriPathAndQuery));

            requestHeaders = FeaturesHelper.AddFeaturesHeader(requestHeaders, features);
            // Create the request
            HttpRequestMessage request = this.CreateHttpRequestMessage(method, uriPathAndQuery, requestHeaders, content, user);

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

            return(response);
        }
예제 #4
0
        /// <summary>
        /// Makes an HTTP request that includes the standard Mobile Services
        /// headers. It will use an HttpClient with user-defined http handlers.
        /// </summary>
        /// <param name="method">
        /// The HTTP method used to request the resource.
        /// </param>
        /// <param name="uriPathAndQuery">
        /// The URI of the resource to request (relative to the Mobile Services
        /// runtime).
        /// </param>
        /// <param name="user">
        /// The object representing the user on behalf of whom the request will be sent.
        /// </param>
        /// <param name="content">
        /// Content to send to the resource.
        /// </param>
        /// <param name="requestHeaders">
        /// Additional request headers to include with the request.
        /// </param>
        /// <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,
                                                             CancellationToken cancellationToken = default)
        {
            Arguments.IsNotNull(method, nameof(method));
            Arguments.IsNotNull(uriPathAndQuery, nameof(uriPathAndQuery));

            requestHeaders = FeaturesHelper.AddFeaturesHeader(requestHeaders, features);
            HttpRequestMessage request = this.CreateHttpRequestMessage(method, uriPathAndQuery, requestHeaders, content, user);

            return(await SendRequestAsync(httpClient, request, ensureResponseContent : false, cancellationToken : cancellationToken));
        }
예제 #5
0
        /// <summary>
        /// Makes an HTTP request that includes the standard Mobile Services
        /// headers. It will use an HttpClient with user-defined http handlers.
        /// </summary>
        /// <param name="method">
        /// The HTTP method used to request the resource.
        /// </param>
        /// <param name="uriPathAndQuery">
        /// The URI of the resource to request (relative to the Mobile Services
        /// runtime).
        /// </param>
        /// <param name="user">
        /// The object representing the user on behalf of whom the request will be sent.
        /// </param>
        /// <param name="content">
        /// Content to send to the resource.
        /// </param>
        /// <param name="requestHeaders">
        /// Additional request headers to include with the request.
        /// </param>
        /// <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,
                                                             CancellationToken cancellationToken = default)
        {
            Arguments.IsNotNull(method, nameof(method));
            Arguments.IsNotNull(uriPathAndQuery, nameof(uriPathAndQuery));

            var stopwatch = new Stopwatch();

            if (MobileServiceClient.LogRequestAndResponse)
            {
                Serilog.Log.Information("MobileServiceHttpClient.RequestAsync : \n ========= BEGIN REQUEST =========");
                Serilog.Log.Information("MobileServiceHttpClient.RequestAsync : URL [" + uriPathAndQuery + "]");
                Serilog.Log.Information("MobileServiceHttpClient.RequestAsync : \n =========  END REQUEST ==========");
            }

            requestHeaders = FeaturesHelper.AddFeaturesHeader(requestHeaders, features);

            HttpRequestMessage request = this.CreateHttpRequestMessage(method, uriPathAndQuery, requestHeaders, content, user);

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

            stopwatch.Stop();
            Serilog.Log.Information($"MobileServiceHttpClient.RequestAsync : ~~~~~~ ElapsedTime [{stopwatch.ElapsedMilliseconds}] Uri[{request.RequestUri}]");

            if (MobileServiceClient.LogRequestAndResponse)
            {
                Serilog.Log.Information($"MobileServiceHttpClient.RequestAsync : \n ========= BEGIN RESPONSE =========");
                string responseContent = await GetResponseContent(response);

                var lines = responseContent.Split(new[] { '\r', '\n' });
                foreach (var line in lines)
                {
                    Serilog.Log.Verbose("MobileServiceHttpClient.RequestAsync : \t\t" + line);
                }
                Serilog.Log.Information("MobileServiceHttpClient.RequestAsync : \n =========  END RESPONSE  ========= \n");
            }

            return(response);
        }