Exemplo n.º 1
0
        private Models.Rest.RestResponce MakeCall(verb _verb, string _route, Dictionary <string, string> QueryParams, object body = null)
        {
            Models.Rest.RestResponce restResponce = new Models.Rest.RestResponce();
            GetAuth();

            var client  = new RestClient(Endpoint + _route);
            var request = new RestRequest();

            switch (_verb)
            {
            case verb.get:
                request = new RestRequest(Method.GET);
                break;

            case verb.patch:
                request = new RestRequest(Method.PATCH);
                break;

            case verb.post:
                request = new RestRequest(Method.POST);
                break;

            case verb.put:
                request = new RestRequest(Method.PUT);
                break;
            }
            foreach (var i in QueryParams)
            {
                request.AddQueryParameter(i.Key, i.Value);
            }
            request.AddHeader("Authorization", "Bearer " + Token);
            request.AddHeader("Accept", "application/json");

            if (body != null)
            {
                request.AddParameter("undefined", body, ParameterType.RequestBody);
            }

            var    response = client.Execute(request);
            string L        = response.Content;

            JObject respo = Newtonsoft.Json.JsonConvert.DeserializeObject <JObject>(L);

            restResponce.data         = respo[dataProperty];
            restResponce.status       = respo.Value <string>(statusProperty);
            restResponce.userMessage  = respo.Value <string>(messageProperty);
            restResponce.responseCode = respo.Value <int>(responseCodeProperty);

            restResponce.isError          = Convert.ToBoolean(respo.Value <string>("isError"));
            restResponce.developerMessage = respo["developerMessage"];
            restResponce.moreInfo         = respo["moreInfo"];
            restResponce.httpStatusCode   = respo.Value <int>("httpStatusCode");


            if (restResponce.isError == true)
            {
                restResponce.errors = new List <Rest.Error>();
                try
                {
                    JArray errors = (JArray)respo["errors"];
                    foreach (var i in errors)
                    {
                        Rest.Error error = new Rest.Error();
                        error._object       = i.Value <string>("object");
                        error._key          = i.Value <string>("key");
                        error._value        = i.Value <string>("value");
                        error._isVisible    = i.Value <bool>("isVisible");
                        error._errorMessage = i.Value <string>("errorMessage");

                        restResponce.errors.Add(error);
                    }
                }
                catch
                {
                    JObject    error = (JObject)respo["errors"];
                    Rest.Error er    = new Rest.Error();
                    er._object       = error.Value <string>("object");
                    er._key          = error.Value <string>("key");
                    er._value        = error.Value <string>("value");
                    er._isVisible    = error.Value <bool>("isVisible");
                    er._errorMessage = error.Value <string>("errorMessage");

                    restResponce.errors.Add(er);
                }
            }

            return(restResponce);
        }
Exemplo n.º 2
0
        private Models.Rest.RestResponce MakeHTTPClientCall(verb _verb, string _route, Dictionary <string, string> QueryParams, object body = null)
        {
            Models.Rest.RestResponce restResponce = new Models.Rest.RestResponce();

            string Output = string.Empty;

            switch (_verb)
            {
            case verb.get:

                break;

            case verb.patch:

                break;

            case verb.post:
                Output = Newtonsoft.Json.JsonConvert.SerializeObject(PostAsync(_route, QueryParams, body));
                break;

            case verb.put:

                break;
            }

            JValue jValue = (JValue)Output;
            string LLLL   = Output.ToString();

            dynamic respo = Newtonsoft.Json.JsonConvert.DeserializeObject <dynamic>(LLLL);
            JObject RET   = JObject.Parse(respo);

            restResponce.data         = RET[dataProperty];
            restResponce.status       = RET.Value <string>(statusProperty);
            restResponce.userMessage  = RET.Value <string>(messageProperty);
            restResponce.responseCode = RET.Value <int>(responseCodeProperty);

            restResponce.isError          = Convert.ToBoolean(RET.Value <string>("isError"));
            restResponce.developerMessage = RET["developerMessage"];
            restResponce.moreInfo         = RET["moreInfo"];
            restResponce.httpStatusCode   = RET.Value <int>("httpStatusCode");



            if (restResponce.isError == true)
            {
                restResponce.errors = new List <Rest.Error>();
                try
                {
                    JArray errors = (JArray)RET["errors"];
                    foreach (var i in errors)
                    {
                        Rest.Error error = new Rest.Error();
                        error._object       = i.Value <string>("object");
                        error._key          = i.Value <string>("key");
                        error._value        = i.Value <string>("value");
                        error._isVisible    = i.Value <bool>("isVisible");
                        error._errorMessage = i.Value <string>("errorMessage");

                        restResponce.errors.Add(error);
                    }
                }
                catch
                {
                    JObject    error = (JObject)RET["errors"];
                    Rest.Error er    = new Rest.Error();
                    er._object       = error.Value <string>("object");
                    er._key          = error.Value <string>("key");
                    er._value        = error.Value <string>("value");
                    er._isVisible    = error.Value <bool>("isVisible");
                    er._errorMessage = error.Value <string>("errorMessage");

                    restResponce.errors.Add(er);
                }
            }

            return(restResponce);
        }