Ejemplo n.º 1
0
        /// <summary>
        /// The API will verify that the delete actually occured.
        /// </summary>
        /// <param name="url"></param>
        /// <param name="authToken"></param>
        /// <returns></returns>
        public static async Task <ApiResponse <bool> > Delete(string url, string authToken)
        {
            ApiResponse <bool> result = new ApiResponse <bool>();

            result.IsSuccess = false;

            //Auth header
            _httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", authToken);

            try
            {
                // The actual Delete method
                using (var response = await _httpClient.DeleteAsync(url))
                {
                    string content = await response.Content.ReadAsStringAsync();

                    result.ReturnMessage = content;
                    result.StatusCode    = (int)response.StatusCode;
                    result.IsSuccess     = StatusCodeHelper.IsStatusOK((int)response.StatusCode);

                    if (result.IsSuccess)
                    {
                        //Status code 200 means the delete occurred.
                        result.Data = true;
                    }
                    else
                    {
                        //Something went wrong. The request completed but the delete did not occur.
                        //Reasons include:
                        //Unauthorized.
                        //NotFound.
                    }
                }
            }
            catch // (Exception ex)
            {
                //Something went wrong. An exception occurred.
                //TODO: log it.
            }

            return(result);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Perform an asynchronous Post request and convert the response the generic type.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="url"></param>
        /// <param name="authToken"></param>
        /// <param name="postData"></param>
        /// <returns></returns>
        public static async Task <ApiResponse <T> > Post <T>(string url, string authToken, string postData)
        {
            ApiResponse <T> result = new ApiResponse <T>();

            result.IsSuccess = false;

            //Auth header
            _httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", authToken);

            try
            {
                HttpContent httpContent = new StringContent(postData, Encoding.UTF8, "application/json");

                // The actual Get method
                using (var response = await _httpClient.PostAsync(url, httpContent))
                {
                    string content = await response.Content.ReadAsStringAsync();

                    result.ReturnMessage = content;
                    result.StatusCode    = (int)response.StatusCode;
                    result.IsSuccess     = StatusCodeHelper.IsStatusOK((int)response.StatusCode);

                    if (result.IsSuccess)
                    {
                        result.Data = JsonConvert.DeserializeObject <T>(content); // Deserialize json data
                    }
                    else
                    {
                        //Something went wrong. The request completed but it failed.
                        //TODO: log it?
                    }
                }
            }
            catch // (Exception ex)
            {
                //Something went wrong. An exception occurred.
                //TODO: log it.
            }

            return(result);
        }