public async Task <IServiceFilterResponse> HandleRequest(IServiceFilterRequest request, IServiceFilterContinuation continuation) { IServiceFilterResponse response = null; try { for (int i = 0; i < this.NumberOfRequests; i++) { response = await continuation.Handle(request); test.AddLog("Sent the request number {0}", i + 1); test.AddLog("Response: {0} - {1}", response.StatusCode, response.StatusDescription); if (response.StatusCode >= 400) { test.AddLog("Invalid response. Content-Type: {0}", response.ContentType); test.AddLog("Response content: {0}", response.Content); throw new InvalidOperationException(); } } } catch (Exception ex) { test.AddLog("Exception while calling continuation: {0}", ex); this.TestFailed = true; throw; } return(response); }
private static Exception CreateMobileServiceException(string errorMessage, IServiceFilterRequest request, IServiceFilterResponse response) { Debug.Assert(!string.IsNullOrEmpty(errorMessage), "errorMessage cannot be null or empty!"); Debug.Assert(request != null, "request cannot be null!"); Debug.Assert(response != null, "response cannot be null!"); return new InvalidOperationException(errorMessage); }
private async Task <IServiceFilterResponse> Log(IServiceFilterRequest request, IServiceFilterContinuation next) { Test.Log(" >>> {0} {1} {2}", request.Method, request.Uri, request.Content); IServiceFilterResponse response = await next.Handle(request).AsTask(); Test.Log(" <<< {0} {1} {2}", response.StatusCode, response.StatusDescription, response.Content); return(response); }
public void OnResponse(IServiceFilterResponse response, System.Exception exception) { mToDoActivity.RunOnUiThread(new MyRunnable(mToDoActivity.mProgressBar, ProgressBar.GONE)); if (mResponseCallback != null) { mResponseCallback.OnResponse(response, exception); } }
private static IJsonValue GetResponseJsonAsync(IServiceFilterResponse response) { Debug.Assert(response != null, "response cannot be null."); // Try to parse the response as JSON JsonValue result = null; if (response.Content != null) { JsonValue.TryParse(response.Content, out result); } return result; }
/// <summary> /// Throw an exception for an invalid response to a web request. /// </summary> /// <param name="request">The request.</param> /// <param name="response">The response.</param> /// <param name="body">The body of the response as JSON.</param> private static void ThrowInvalidResponse(IServiceFilterRequest request, IServiceFilterResponse response, IJsonValue body) { Debug.Assert(request != null, "request cannot be null!"); Debug.Assert(response != null, "response cannot be null!"); Debug.Assert( response.ResponseStatus != ServiceFilterResponseStatus.Success || response.StatusCode >= 400, "response should be failing!"); // Create either an invalid response or connection failed message // (check the status code first because some status codes will // set a protocol ErrorStatus). string message = null; if (response.StatusCode >= 400) { if (body != null) { if (body.ValueType == JsonValueType.String) { // User scripts might return errors with just a plain string message as the // body content, so use it as the exception message message = body.GetString(); } else if (body.ValueType == JsonValueType.Object) { // Get the error message, but default to the status description // below if there's no error message present. message = body.Get("error").AsString() ?? body.Get("description").AsString(); } } if (string.IsNullOrWhiteSpace(message)) { message = string.Format( CultureInfo.InvariantCulture, Resources.MobileServiceClient_ErrorMessage, response.StatusDescription); } } else { message = string.Format( CultureInfo.InvariantCulture, Resources.MobileServiceClient_ErrorMessage, response.ResponseStatus); } // Combine the pieces and throw the exception throw CreateMobileServiceException(message, request, response); }
public void OnCompleted(ToDoItem entity, System.Exception exception, IServiceFilterResponse response) { if (exception == null) { if (!entity.IsComplete()) { mToDoActivity.mAdapter.Add(entity); } } else { mToDoActivity.CreateAndShowDialog(exception, "Error"); } }
/// <summary> /// Perform a web request and include the standard Mobile Services /// headers. /// </summary> /// <param name="method"> /// The HTTP method used to request the resource. /// </param> /// <param name="uriFragment"> /// URI of the resource to request (relative to the Mobile Services /// runtime). /// </param> /// <param name="content"> /// Optional content to send to the resource. /// </param> /// <param name="ignoreFilters"> /// Optional parameter to indicate if the client filters should be ignored /// and the request should be sent directly. Is <c>false</c> by default. /// </param> /// <returns>The JSON value of the response.</returns> internal async Task <IJsonValue> RequestAsync(string method, string uriFragment, IJsonValue content, bool ignoreFilters = false) { Debug.Assert(!string.IsNullOrEmpty(method), "method cannot be null or empty!"); Debug.Assert(!string.IsNullOrEmpty(uriFragment), "uriFragment cannot be null or empty!"); // Create the web request IServiceFilterRequest request = new ServiceFilterRequest(); request.Uri = new Uri(this.ApplicationUri, uriFragment); request.Method = method.ToUpper(); request.Accept = RequestJsonContentType; // Set Mobile Services authentication, application, and telemetry // headers request.Headers[RequestInstallationIdHeader] = applicationInstallationId; if (!string.IsNullOrEmpty(this.ApplicationKey)) { request.Headers[RequestApplicationKeyHeader] = this.ApplicationKey; } if (this.CurrentUser != null && !string.IsNullOrEmpty(this.CurrentUser.MobileServiceAuthenticationToken)) { request.Headers[RequestAuthenticationHeader] = this.CurrentUser.MobileServiceAuthenticationToken; } // TODO: Set the User-Agent header; currently HttpWebRequest throws when the // User-Agent header is set. // Add any request as JSON if (content != null) { request.ContentType = RequestJsonContentType; request.Content = content.Stringify(); } // Send the request and get the response back as JSON IServiceFilter filter = ignoreFilters ? null : this.filter; IServiceFilterResponse response = await ServiceFilter.ApplyAsync(request, filter); IJsonValue body = GetResponseJsonAsync(response); // Throw errors for any failing responses if (response.ResponseStatus != ServiceFilterResponseStatus.Success || response.StatusCode >= 400) { ThrowInvalidResponse(request, response, body); } return(body); }
/// <summary> /// Throw an exception for an invalid response to a web request. /// </summary> /// <param name="request">The request.</param> /// <param name="response">The response.</param> /// <param name="body">The body of the response as JSON.</param> private static void ThrowInvalidResponse(IServiceFilterRequest request, IServiceFilterResponse response, IJsonValue body) { Debug.Assert(request != null, "request cannot be null!"); Debug.Assert(response != null, "response cannot be null!"); Debug.Assert( response.ResponseStatus != ServiceFilterResponseStatus.Success || response.StatusCode >= 400, "response should be failing!"); // Create either an invalid response or connection failed message // (check the status code first because some status codes will // set a protocol ErrorStatus). string message = null; if (response.StatusCode >= 400) { // Get the error message, but default to the status message // if there's no error message present. string error = body.Get("error").AsString() ?? body.Get("description").AsString() ?? response.StatusDescription; // Get the status code, text int code = body.Get("code").AsInteger() ?? response.StatusCode; // Combine the pieces and throw the exception message = string.Format(CultureInfo.InvariantCulture, Resources.MobileServiceClient_ThrowInvalidResponse_ErrorMessage, code, (HttpStatusCode)code, error, response.Content); } else { message = string.Format( CultureInfo.InvariantCulture, Resources.MobileServiceClient_ThrowConnectionFailure_ErrorMessage, response.ResponseStatus); } // Combine the pieces and throw the exception throw CreateMobileServiceException(message, request, response); }
/// <summary> /// Perform a web request and include the standard Mobile Services /// headers. /// </summary> /// <param name="method"> /// The HTTP method used to request the resource. /// </param> /// <param name="uriFragment"> /// URI of the resource to request (relative to the Mobile Services /// runtime). /// </param> /// <param name="content"> /// Optional content to send to the resource. /// </param> /// <returns>The JSON value of the response.</returns> internal async Task <JToken> RequestAsync(string method, string uriFragment, JToken content) { Debug.Assert(!string.IsNullOrEmpty(method), "method cannot be null or empty!"); Debug.Assert(!string.IsNullOrEmpty(uriFragment), "uriFragment cannot be null or empty!"); // Create the web request IServiceFilterRequest request = new ServiceFilterRequest(); request.Uri = new Uri(this.ApplicationUri, uriFragment); request.Method = method.ToUpper(); request.Accept = RequestJsonContentType; // Set Mobile Services authentication, application, and telemetry // headers request.Headers[RequestInstallationIdHeader] = applicationInstallationId; if (!string.IsNullOrEmpty(this.ApplicationKey)) { request.Headers[RequestApplicationKeyHeader] = this.ApplicationKey; } if (!string.IsNullOrEmpty(this.currentUserAuthenticationToken)) { request.Headers[RequestAuthenticationHeader] = this.currentUserAuthenticationToken; } // Add any request as JSON if (content != null) { request.ContentType = RequestJsonContentType; request.Content = content.ToString(); } // Send the request and get the response back as JSON IServiceFilterResponse response = await ServiceFilter.ApplyAsync(request, this.filter); JToken body = GetResponseJsonAsync(response); // Throw errors for any failing responses if (response.ResponseStatus != ServiceFilterResponseStatus.Success || response.StatusCode >= 400) { ThrowInvalidResponse(request, response, body); } return(body); }
private static JToken GetResponseJsonAsync(IServiceFilterResponse response) { Debug.Assert(response != null, "response cannot be null."); // Try to parse the response as JSON JToken result = null; if (response.Content != null) { try { result = JToken.Parse(response.Content); } catch (Newtonsoft.Json.JsonException) { } } return(result); }
private async Task <IServiceFilterResponse> HandleAsync(IServiceFilterRequest request, IServiceFilterContinuation continuation) { int ic = _callCount; bool invokeBusy = false; lock (_countLock) { if (_callCount == 0) { invokeBusy = true; } _callCount++; } if (invokeBusy) { _busyIndicator.Invoke(true); } IServiceFilterResponse response = await continuation.Handle(request).AsTask(); bool invokeIdle = false; lock (_countLock) { if (_callCount == 1) { invokeIdle = true; } _callCount--; } if (invokeIdle) { _busyIndicator.Invoke(false); } return(response); }
/// <summary> /// Initializes a new instance of the /// MobileServiceInvalidOperationException class. /// </summary> /// <param name="message">The exception message.</param> /// <param name="request">The originating service request.</param> /// <param name="response">The returned service response.</param> internal MobileServiceInvalidOperationException(string message, IServiceFilterRequest request, IServiceFilterResponse response) : base(message) { this.Request = request; this.Response = response; }
public void OnCompleted(IList <ToDoItem> result, int count, System.Exception exception, IServiceFilterResponse response) { if (exception == null) { mToDoActivity.mAdapter.Clear(); #warning : why isn't foreach working? //foreach (ToDoItem item in result) //{ // mToDoActivity.mAdapter.Add(item); //} for (int i = 0; i < result.Count; i++) { mToDoActivity.mAdapter.Add(result[i]); } } else { mToDoActivity.CreateAndShowDialog(exception, "Error"); } }
private static JToken GetResponseJsonAsync(IServiceFilterResponse response) { Debug.Assert(response != null, "response cannot be null."); // Try to parse the response as JSON JToken result = null; if (response.Content != null) { try { result = JToken.Parse(response.Content); } catch (Newtonsoft.Json.JsonException) { } } return result; }
/// <summary> /// Create an exception for an invalid response to a web request. /// </summary> /// <param name="errorMessage">The error message.</param> /// <param name="request">The request.</param> /// <param name="response">The response.</param> private static Exception CreateMobileServiceException(string errorMessage, IServiceFilterRequest request, IServiceFilterResponse response) { Debug.Assert(!string.IsNullOrEmpty(errorMessage), "errorMessage cannot be null or empty!"); Debug.Assert(request != null, "request cannot be null!"); Debug.Assert(response != null, "response cannot be null!"); throw new MobileServiceInvalidOperationException(errorMessage, request, response); }