protected static T ProcessMethod <T>( Type clazz, T resource, string methodName, Dictionary <string, string> parameters, bool useCache, SDK sdk) where T : MPBase { if ((object)resource == null) { try { resource = (T)Activator.CreateInstance(clazz, sdk); } catch (Exception ex) { throw new MPException(ex.Message); } } Dictionary <string, object> restInformation = MPBase.GetRestInformation(MPBase.GetAnnotatedMethod(clazz, methodName)); HttpMethod httpMethod = (HttpMethod)restInformation["method"]; string path = MPBase.ParsePath <T>(restInformation["path"].ToString(), parameters, resource, sdk); PayloadType payloadType = (PayloadType)restInformation["payloadType"]; JObject payload = MPBase.GeneratePayload <T>(httpMethod, resource); int requestTimeout = (int)restInformation["requestTimeout"]; int retries = (int)restInformation["retries"]; WebHeaderCollection colHeaders = new WebHeaderCollection(); MPAPIResponse response = MPBase.CallAPI(httpMethod, path, payloadType, payload, colHeaders, useCache, requestTimeout, retries); if (response.StatusCode >= 200 && response.StatusCode < 300) { if (httpMethod != HttpMethod.DELETE) { resource = (T)MPBase.FillResourceWithResponseData <T>(resource, response); resource._lastApiResponse = response; } else { resource = default(T); } } else if (response.StatusCode >= 400 && response.StatusCode < 500) { BadParamsError badParamsError = MPCoreUtils.GetBadParamsError(response.StringResponse); resource.Errors = new BadParamsError?(badParamsError); } else { MPException mpException = new MPException() { StatusCode = new int?(response.StatusCode), ErrorMessage = response.StringResponse, Cause = { response.JsonObjectResponse.ToString() } }; } return(resource); }
/// <summary> /// Core implementation of processMethod. Retrieves a generic type. /// </summary> /// <typeparam name="T">Generic type that will return.</typeparam> /// <param name="clazz">Type of Class we are using.</param> /// <param name="resource">Resource we will use and return in the implementation.</param> /// <param name="methodName">The name of the method we are trying to call.</param> /// <param name="parameters">Parameters to use in the process.</param> /// <param name="useCache">Cache configuration.</param> /// <returns>Generic type object, containing information about retrieval process.</returns> protected static T ProcessMethod <T>(Type clazz, T resource, string methodName, Dictionary <string, string> parameters, bool useCache) where T : MPBase { if (resource == null) { try { resource = (T)Activator.CreateInstance(clazz); } catch (Exception ex) { throw new MPException(ex.Message); } } var clazzMethod = GetAnnotatedMethod(clazz, methodName); var restData = GetRestInformation(clazzMethod); HttpMethod httpMethod = (HttpMethod)restData["method"]; string path = ParsePath(restData["path"].ToString(), parameters, resource); PayloadType payloadType = (PayloadType)restData["payloadType"]; JObject payload = GeneratePayload(httpMethod, resource); int requestTimeout = (int)restData["requestTimeout"]; int retries = (int)restData["retries"]; WebHeaderCollection colHeaders = new WebHeaderCollection(); MPAPIResponse response = CallAPI(httpMethod, path, payloadType, payload, colHeaders, useCache, requestTimeout, retries); if (response.StatusCode >= 200 && response.StatusCode < 300) { if (httpMethod != HttpMethod.DELETE) { resource = (T)FillResourceWithResponseData(resource, response); resource._lastApiResponse = response; } else { resource = null; } } else if (response.StatusCode >= 400 && response.StatusCode < 500) { BadParamsError badParamsError = MPCoreUtils.GetBadParamsError(response.StringResponse); resource.Errors = badParamsError; } else { MPException webserverError = new MPException() { StatusCode = response.StatusCode, ErrorMessage = response.StringResponse }; webserverError.Cause.Add(response.JsonObjectResponse.ToString()); } return(resource); }
internal static void ProcessResponse(string path, T resource, MPAPIResponse response, HttpMethod httpMethod) { var errorDetails = response.JsonObjectResponse?.ToString() ?? response.StringResponse ?? "[No additional details could be retrieved from the response]"; var errorMessage = $"HTTP {httpMethod} request to Endpoint '{path}' with payload of type '{typeof(T).Name}' resulted in an unsuccessful HTTP response with status code {response.StatusCode}.\r\nDetails:\r\n{errorDetails}"; if (response.StatusCode >= 200 && response.StatusCode < 300) { if (httpMethod != HttpMethod.DELETE) { FillResourceWithResponseData(resource, response); } } else if (response.StatusCode >= 400 && response.StatusCode < 500) { var badParamsError = MPCoreUtils.GetBadParamsError(response.StringResponse); var exception = new MPException(errorMessage) { Error = badParamsError, ErrorMessage = badParamsError.ToString() }; throw exception; } else { var exception = new MPException(errorMessage) { StatusCode = response.StatusCode, ErrorMessage = response.StringResponse, Cause = { errorDetails } }; throw exception; } }
internal static List <T> GetList(string path, string accessToken = null, bool useCache = false, Dictionary <string, string> queryParameters = null, int requestTimeOut = 0, int retries = 1) { var response = Invoke(HttpMethod.GET, path, PayloadType.NONE, null, accessToken, queryParameters, useCache, requestTimeOut, retries); if (response.StatusCode >= 200 && response.StatusCode < 300) { return(response.ToList <T>()); } var exception = new MPException { StatusCode = response.StatusCode, ErrorMessage = response.StringResponse }; if (response.JsonObjectResponse != null) { exception.Cause.Add(response.JsonObjectResponse.ToString()); } throw exception; }