Exemplo n.º 1
0
        public void MPExceptionTestToStringOverride()
        {
            MPException exception = new MPException("Some Exception message");

            Assert.AreEqual("MercadoPago.MPException: Some Exception message", exception.ToString().Trim());

            MPException exception2 = new MPException("Some Exception message 2", new MPException("InnerExceptionMessage 2"));

            Assert.AreEqual("MercadoPago.MPException: InnerExceptionMessage 2", exception2.InnerException.ToString().Trim());

            MPException exception3 = new MPException("Some Exception message 3", "requestId", 666);

            Assert.AreEqual(string.Format("MercadoPago.MPException: Some Exception message 3; request-id: {0}; status_code: {1}", "requestId", 666), exception3.ToString().Trim());

            MPException exception4 = new MPException("Some Exception message 4", "requestId", 666, new MPException("InnerExceptionMessage 4"));

            Assert.AreEqual("MercadoPago.MPException: InnerExceptionMessage 4", exception4.InnerException.ToString().Trim());
        }
Exemplo n.º 2
0
        /// <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>
        /// <param name="requestOptions">Object containing the request options.</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, MPRequestOptions requestOptions) 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"];
            PayloadType payloadType = (PayloadType)restData["payloadType"];
            JObject     payload     = GeneratePayload(httpMethod, resource);

            if (requestOptions == null)
            {
                int requestTimeout = (int)restData["requestTimeout"];
                int retries        = (int)restData["retries"];
                requestOptions = new MPRequestOptions
                {
                    Retries = retries,
                    Timeout = requestTimeout
                };
            }

            string        path     = ParsePath(restData["path"].ToString(), parameters, resource, requestOptions);
            MPAPIResponse response = CallAPI(httpMethod, path, payloadType, payload, useCache, requestOptions);

            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());
                throw webserverError;
            }

            return(resource);
        }