Exemple #1
0
        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);
        }
Exemple #2
0
        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);
        }