private static void EmulateEditLocking( string url, API.Constants.HttpMethods httpMethod, ref IResponseRootObject responseRootObject) { if (responseRootObject is null) { throw new ArgumentNullException(nameof(responseRootObject)); } if (httpMethod == API.Constants.HttpMethods.Get && (url.EndsWith("edit") || url.Contains("create"))) { foreach (var dto in responseRootObject.Data) { var prop = typeof(DataTransferObject).GetProperty( "Fields", BindingFlags.NonPublic | BindingFlags.Instance); var fields = (IDictionary <string, object>)prop.GetValue(dto); var key = "IsLockedForEditing"; if (fields.ContainsKey(key)) { fields[key] = true; } else { fields.Add(key, true); } } } }
private static async Task <HttpResponseMessage> GetRawResponseAsync( HttpClient client, API.Constants.HttpMethods httpMethod, string url, StringContent httpContent = null, MultipartFormDataContent formContent = null) { HttpResponseMessage rawResponse = null; switch (httpMethod) { case API.Constants.HttpMethods.Post: rawResponse = await client.PostAsync(url, (formContent != null)? (HttpContent)formContent : httpContent); break; case API.Constants.HttpMethods.Put: rawResponse = await client.PutAsync(url, httpContent); break; case API.Constants.HttpMethods.Delete: rawResponse = await client.DeleteAsync(url); break; default: rawResponse = await client.GetAsync(url); break; } return(rawResponse); }
private static async Task <HttpResponseMessage> HttpRequestRawResponseAsync( string uri, string token, API.Constants.HttpMethods httpMethod, IDictionary <string, object> data) { MultipartFormDataContent formData = null; var httpContent = FormatHttpRequestContent(data); HttpClient client = SetupHttpClient(token); var url = GetUrl(uri); //var args = GetArgs(data); Log.Debug(httpMethod + ": " + url); if (data?.Count > 0) { var requestContent = await httpContent.ReadAsStringAsync(); Log.DebugFormat("\twith request content: {0}", requestContent); } HttpResponseMessage rawResponse = null; try { rawResponse = await GetRawResponseAsync(client, httpMethod, url, httpContent, formData); } catch (Exception ex) { Log.Fatal($"Unexpected error accessing gateway. \n{ex.Message}"); throw; } return(rawResponse); }
public async Task <IResponseRootObject> HttpRequestAsync( string uri, string token, API.Constants.HttpMethods httpMethod, IDictionary <string, object> data) { var url = GetUrl(uri); Log.Info($"Request initiated from {httpMethod}: {url}"); var rawResponse = await HttpRequestRawResponseAsync(uri, token, httpMethod, data); var rawResponseContent = await ReadResponseContentAsync(rawResponse); var statusCode = (int)rawResponse.StatusCode; var responseRootObject = DeserializeResponseRootObject(rawResponseContent, statusCode, url); // TODO on API change from using an Error property and put it in the response ReasonPhrase // or the base class of all models //var statusCodeReason = rawResponse.ReasonPhrase; AnticipatedStatusCodeErrorCheck(statusCode, responseRootObject); EmulateEditLocking(url, httpMethod, ref responseRootObject); Log.Info($"Request complete from {httpMethod}: {url}"); Log.Debug($"Response from {httpMethod}: {url}-> {responseRootObject.ToString()}"); return(responseRootObject); }
/// <summary> /// We can move this into the API if we feel it is needed. /// The ResponseRootObject argument passed in with its editable flag set. /// </summary> /// <param name="url"></param> /// <param name="httpMethod"></param> /// <param name="responseRootObject"></param> /// <returns></returns> private static void EmulateEditLocking( string url, API.Constants.HttpMethods httpMethod, ref ResponseRootObject responseRootObject) { if (responseRootObject is null) { throw new ArgumentNullException(nameof(responseRootObject)); } if (httpMethod == API.Constants.HttpMethods.Get && (url.EndsWith("edit") || url.Contains("create"))) { responseRootObject.__SetIsLockedForEditing(); } }
public async Task <IResponseRootObject> HttpRequestAsync( string uri, string token, API.Constants.HttpMethods httpMethod, IDictionary <string, object> data) { Log.Info($"Request initiated from {httpMethod}: {uri}"); if (data?.Count > 0) { Log.DebugFormat("\twith request content: {0}", data); } var responseRootObject = GetResponse(httpMethod, uri, data); EmulateEditLocking(uri, httpMethod, ref responseRootObject); await Task.CompletedTask; Log.Info($"Request complete from {httpMethod}: {uri}"); Log.Debug($"Response from {httpMethod}: {uri}-> {responseRootObject.ToString()}"); return(responseRootObject); }
private IResponseRootObject GetResponse( API.Constants.HttpMethods httpMethod, string uri, IDictionary <string, object> data) { ResponseRootObject responseRootObject = null; switch (httpMethod) { case API.Constants.HttpMethods.Post: responseRootObject = MockPost(uri, data); break; case API.Constants.HttpMethods.Put: responseRootObject = MockPut(uri, data); break; case API.Constants.HttpMethods.Delete: responseRootObject = MockDelete(uri); break; default: if (uri.EndsWith("create")) { responseRootObject = MockCreate(uri); } else if (uri.EndsWith("edit")) { responseRootObject = MockEdit(uri); } else { responseRootObject = MockGet(uri); } break; } return(responseRootObject); }
public async Task <IResponseRootObject> HttpRequestAsync( string uri, string token, API.Constants.HttpMethods httpMethod, IDictionary <string, object> data) { var url = GetUrl(uri); Log.Info($"Request initiated from {httpMethod}: {url}"); var rawResponse = await HttpRequestRawResponseAsync(uri, token, httpMethod, data); ResponseRootObject responseRootObject; var statusCode = (int)rawResponse.StatusCode; if (statusCode == 500 || !allowedStatusCodes.Contains(statusCode)) { // It's a 500 or a code we don't expect, we have no error to return, so throw a generic error. var msg = string.Format( "Status Code: {0}, Error: {1}.", statusCode, rawResponse.ReasonPhrase); throw new HttpDataServiceException(statusCode, msg); } else { var rawResponseContent = await ReadResponseContentAsync(rawResponse); responseRootObject = DeserializeResponseRootObject(rawResponseContent, statusCode, url); } // TODO on API change from using an Error property and put it in the response ReasonPhrase // or the base class of all models //var statusCodeReason = rawResponse.ReasonPhrase; AnticipatedStatusCodeErrorCheck(statusCode, responseRootObject); EmulateEditLocking(url, httpMethod, ref responseRootObject); Log.Info($"Request complete from {httpMethod}: {url}"); Log.Debug($"Response from {httpMethod}: {url}-> {responseRootObject.ToString()}"); return(responseRootObject); }