Beispiel #1
0
        /// <summary>
        /// Generic Web API caller for POST
        /// </summary>
        /// <typeparam name="T">Type of object</typeparam>
        /// <param name="url">Url</param>
        /// <param name="o">Object to post</param>
        /// <returns></returns>
        public WebApiResponse <T> Post <T>(string url, string uri, T o) where T : class
        {
            var result = new WebApiResponse <T>();

            try
            {
                var request = ConfigureBasicAuthRequest(url);

                StringContent content = new StringContent(JsonConvert.SerializeObject(o), Encoding.UTF8, "application/json");
                using (
                    var response = request.PostAsync(uri, content).ContinueWith((taskWithResponse) =>
                {
                    if (taskWithResponse != null)
                    {
                        if (taskWithResponse.Status != TaskStatus.RanToCompletion)
                        {
                            result.Success = false;
                            result.Exception =
                                $"Server error (HTTP {taskWithResponse.Result?.StatusCode}: {taskWithResponse.Exception?.InnerException} : {taskWithResponse.Exception?.Message}).";
                        }
                        else if (taskWithResponse.Result.IsSuccessStatusCode)
                        {
                            var jsonString = taskWithResponse.Result.Content.ReadAsStringAsync();
                            var data = JsonConvert.DeserializeObject <T>(jsonString.Result);
                            result.Success = true;
                            result.Data = data;
                        }
                        else if (taskWithResponse.Result.StatusCode == HttpStatusCode.InternalServerError)
                        {
                            var jsonCisFeedback = taskWithResponse.Result.Content.ReadAsStringAsync();
                            //var cisServerError = JsonConvert.DeserializeObject<CisServerError>(jsonCisFeedback.Result);
                            //var message =
                            //$"CIS Internal server exception: {cisServerError.ExceptionMessage}, for BizObject: {typeof(T)}";
                            throw new Exception(jsonCisFeedback.Result);
                        }
                        // anything else such as 404.
                        else
                        {
                            var jsonCisFeedback = taskWithResponse.Result.Content.ReadAsStringAsync();
                            //var formattedFeedback = JsonConvert.DeserializeObject<ApiFeedbackItem[]>(jsonCisFeedback.Result);
                            result.Success = false;
                            //result.FormattedFeedbackItems = formattedFeedback;
                            result.ReasonPhrase = taskWithResponse.Result.ReasonPhrase;
                            result.StatusCode = taskWithResponse.Result.StatusCode;
                        }
                    }
                }))
                {
                    response.Wait();
                }
            }
            catch (Exception ex)
            {
                result.Success   = false;
                result.Exception = ex.ToString();
            }
            return(result);
        }
Beispiel #2
0
        /// <summary>
        /// Generic Web API caller
        /// </summary>
        /// <returns></returns>
        public WebApiResponse <T> Get <T>(string url, string uri) where T : class
        {
            var result = new WebApiResponse <T>();

            try
            {
                var request = ConfigureRequest(url);
                using (
                    var response = request.GetAsync(uri).ContinueWith((taskWithResponse) =>
                {
                    if (taskWithResponse != null)
                    {
                        if (taskWithResponse.Status != TaskStatus.RanToCompletion)
                        {
                            result.Success = false;
                            result.Exception =
                                $"Server error (HTTP {taskWithResponse.Result?.StatusCode}: {taskWithResponse.Exception?.InnerException} : {taskWithResponse.Exception?.Message}).";
                        }
                        else if (taskWithResponse.Result.IsSuccessStatusCode)
                        {
                            var jsonString = taskWithResponse.Result.Content.ReadAsStringAsync();
                            var data = JsonConvert.DeserializeObject <T>(jsonString.Result);
                            result.Success = true;
                            result.Data = data;
                        }
                    }
                }))
                {
                    response.Wait();
                }
                //var response = await request.GetAsync(url);
                //var res = await response.Content.ReadAsStringAsync();
                //var data = JsonConvert.DeserializeObject<T>(res);
                //return data;
            }
            catch (Exception ex)
            {
                result.Success   = false;
                result.Exception = ex.ToString();
            }
            return(result);
        }
Beispiel #3
0
        /// <summary>
        /// Generic Web API caller for PUT
        /// </summary>
        /// <typeparam name="T">Type of object</typeparam>
        /// <param name="url">Url</param>
        /// <param name="uri"></param>
        /// <param name="o">Object to post</param>
        /// <returns></returns>
        public WebApiResponse <T> Put <T>(string url, string uri, T o) where T : class
        {
            var result = new WebApiResponse <T>();

            try
            {
                var request = ConfigureBasicAuthRequest(url);

                StringContent content = new StringContent(JsonConvert.SerializeObject(o), Encoding.UTF8, "application/json");
                using (
                    var response = request.PutAsync(uri, content).ContinueWith((taskWithResponse) =>
                {
                    if (taskWithResponse != null)
                    {
                        if (taskWithResponse.Status != TaskStatus.RanToCompletion)
                        {
                            result.Success = false;
                            result.Exception =
                                $"Server error (HTTP {taskWithResponse.Result?.StatusCode}: {taskWithResponse.Exception?.InnerException} : {taskWithResponse.Exception?.Message}).";
                        }
                        else if (taskWithResponse.Result.IsSuccessStatusCode)
                        {
                            //var jsonString = taskWithResponse.Result.Content.ReadAsStringAsync();
                            //jsonString.Wait();
                            //var data = JsonConvert.DeserializeObject<T>(jsonString.Result);
                            taskWithResponse.Wait();     // TODO use async/await?
                            result.Success = true;
                            //result.Data = data;
                        }
                    }
                }))
                {
                    response.Wait();
                }
            }
            catch (Exception ex)
            {
                result.Success   = false;
                result.Exception = ex.ToString();
            }
            return(result);
        }