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); }
public bool ProcessMethodBool <T>(string methodName, bool useCache, SDK sdk) where T : MPBase { Dictionary <string, string> parameters = (Dictionary <string, string>)null; MPBase.ProcessMethod <T>(this.GetType(), (T)this, methodName, parameters, useCache, sdk); return(this.Errors.HasValue); }
protected static List <T> ProcessMethodBulk <T>( Type clazz, string methodName, Dictionary <string, string> mapParams, bool useCache, SDK sdk) where T : MPBase { if (!MPBase.ALLOWED_BULK_METHODS.Contains(methodName)) { throw new MPException("Method \"" + methodName + "\" not allowed"); } List <T> objList1 = new List <T>(); Dictionary <string, object> restInformation = MPBase.GetRestInformation(MPBase.GetAnnotatedMethod(clazz, methodName)); HttpMethod httpMethod = (HttpMethod)restInformation["method"]; T resource = default(T); string path = MPBase.ParsePath <T>(restInformation["path"].ToString(), mapParams, resource, sdk); int retries = (int)restInformation["retries"]; int requestTimeout = (int)restInformation["requestTimeout"]; Console.WriteLine("Path: {0}", (object)path); PayloadType payloadType = (PayloadType)restInformation["payloadType"]; WebHeaderCollection standardHeaders = MPBase.GetStandardHeaders(); MPAPIResponse response = MPBase.CallAPI(httpMethod, path, payloadType, (JObject)null, standardHeaders, useCache, requestTimeout, retries); List <T> objList2 = new List <T>(); if (response.StatusCode >= 200 && response.StatusCode < 300) { objList2 = MPBase.FillArrayWithResponseData <T>(clazz, response); } return(objList2); }
public static MPBase ProcessMethod(string methodName, bool useCache, SDK sdk) { Type typeFromStack = MPBase.GetTypeFromStack(); MPBase.AdmitIdempotencyKey(typeFromStack); Dictionary <string, string> parameters = new Dictionary <string, string>(); return(MPBase.ProcessMethod <MPBase>(typeFromStack, (MPBase)null, methodName, parameters, useCache, sdk)); }
public static JObject getDiffFromLastChange(JToken jactual, JToken jold) { JObject jobject = new JObject(); if (((JObject)jactual).Properties().Count <JProperty>() <= 0) { return((JObject)null); } foreach (JProperty property in ((JObject)jactual).Properties()) { string snakeCase = MPBase.ToSnakeCase(property.Name); if (property.Value.GetType() == typeof(JObject)) { if (jold != null) { JObject diffFromLastChange = MPBase.getDiffFromLastChange(property.Value, jold[(object)property.Name]); if (diffFromLastChange != null && diffFromLastChange.Properties().Count <JProperty>() > 0) { jobject.Add(snakeCase, (JToken)diffFromLastChange); } } else { jobject.Add(snakeCase, property.Value); } } else if (property.Value.GetType() == typeof(JArray)) { jobject.Add(snakeCase, property.Value); } else if (property.Value.GetType() == typeof(JValue)) { if (jold != null) { if (jold[(object)property.Name] != null) { if ((string)property.Value != (string)jold[(object)property.Name]) { jobject.Add(snakeCase, property.Value); } } else { jobject.Add(snakeCase, property.Value); } } else { jobject.Add(snakeCase, property.Value); } } } return(jobject); }
public static JObject GeneratePayload <T>(HttpMethod httpMethod, T resource) where T : MPBase { if (httpMethod.ToString() == "PUT") { return(MPBase.getDiffFromLastChange((JToken)MPCoreUtils.GetJsonFromResource <T>(resource), (JToken)resource.GetLastKnownJson())); } if (httpMethod.ToString() == "POST") { return(MPCoreUtils.GetJsonFromResource <T>(resource)); } return((JObject)null); }
protected static MPBase FillResourceWithResponseData <T>( T resource, MPAPIResponse response) where T : MPBase { if (response.JsonObjectResponse != null && response.JsonObjectResponse != null) { JObject jsonObjectResponse = response.JsonObjectResponse; resource = (T)MPBase.FillResource <T>((T)MPCoreUtils.GetResourceFromJson <T>(resource.GetType(), jsonObjectResponse), resource); resource._lastKnownJson = MPCoreUtils.GetJsonFromResource <T>(resource); } return((MPBase)resource); }
public static MPBase ProcessMethod <T>(string methodName, string param, bool useCache, SDK sdk) where T : MPBase { Type typeFromStack = MPBase.GetTypeFromStack(); MPBase.AdmitIdempotencyKey(typeFromStack); return((MPBase)MPBase.ProcessMethod <T>(typeFromStack, default(T), methodName, new Dictionary <string, string>() { { "id", param } }, useCache, sdk)); }
public static List <T> ProcessMethodBulk <T>( Type clazz, string methodName, string param1, bool useCache, SDK sdk) where T : MPBase { return(MPBase.ProcessMethodBulk <T>(clazz, methodName, new Dictionary <string, string>() { { nameof(param1), param1 } }, useCache, sdk)); }
public static string ParsePath <T>( string path, Dictionary <string, string> mapParams, T resource, SDK sdk) where T : MPBase { StringBuilder stringBuilder = new StringBuilder(); bool flag = !path.Contains <char>(':') && mapParams != null && mapParams.Any <KeyValuePair <string, string> >(); string pattern = ":([a-z0-9_]+)"; foreach (Match match in Regex.Matches(path, pattern)) { string text = match.Value.Replace(":", ""); if (mapParams != null) { foreach (KeyValuePair <string, string> mapParam in mapParams) { if (text == mapParam.Key) { path = path.Replace(match.Value, mapParam.Value); } } } JToken jtoken = JObject.FromObject((object)resource).GetValue(MPBase.ToPascalCase(text)); if (jtoken != null) { path = path.Replace(match.Value, jtoken.ToString()); } } stringBuilder.Insert(0, sdk.BaseUrl); stringBuilder.Append(path); string str = (object)resource != null ? (!string.IsNullOrEmpty(resource.MarketplaceAccessToken) ? resource.MarketplaceAccessToken : sdk.GetAccessToken()) : sdk.GetAccessToken(); Console.Out.WriteLine("STOP"); if (!string.IsNullOrEmpty(str)) { stringBuilder.Append(string.Format("{0}{1}", (object)"?access_token=", (object)str)); } if (flag) { foreach (KeyValuePair <string, string> mapParam in mapParams) { stringBuilder.Append(string.Format("{0}{1}={2}", (object)"&", (object)mapParam.Key, (object)mapParam.Value)); } } return(stringBuilder.ToString()); }
public static MPBase ProcessMethod <T>( Type clazz, string methodName, string param1, string param2, bool useCache, SDK sdk) where T : MPBase { return((MPBase)MPBase.ProcessMethod <T>(clazz, default(T), methodName, new Dictionary <string, string>() { { "param0", param1 }, { nameof(param1), param2 } }, useCache, sdk)); }
public static List <T> ProcessMethodBulk <T>(Type clazz, string methodName, bool useCache, SDK sdk) where T : MPBase { Dictionary <string, string> mapParams = (Dictionary <string, string>)null; return(MPBase.ProcessMethodBulk <T>(clazz, methodName, mapParams, useCache, sdk)); }
public static MercadoPago.Customer FindById(string id, bool useCache, SDK sdk) { return((MercadoPago.Customer)MPBase.ProcessMethod <MercadoPago.Customer>(nameof(FindById), id, useCache, sdk)); }
public static List <MercadoPago.Customer> Search( Dictionary <string, string> filters, bool useCache, SDK sdk) { return(MPBase.ProcessMethodBulk <MercadoPago.Customer>(typeof(MercadoPago.Customer), nameof(Search), filters, useCache, sdk)); }