public ForteException(HttpStatusCode httpStatusCode, ForteError forteError, string message)
     : base(message)
 {
     HttpStatusCode = httpStatusCode;
     ForteError = forteError;
 }
 public ForteException()
 {
     ForteError = new ForteError();
 }
Beispiel #3
0
 public ForteException()
 {
     ForteError = new ForteError();
 }
Beispiel #4
0
 public ForteException(HttpStatusCode httpStatusCode, ForteError forteError, string message)
     : base(message)
 {
     HttpStatusCode = httpStatusCode;
     ForteError     = forteError;
 }
Beispiel #5
0
        internal static string GetResponse(String URL, String method, string requestBody, string strUser, string strPasswd, string authAccountID)
        {
            HttpResponseMessage response;

            using (var client = new HttpClient())
            {
                string authheadertext = Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(string.Format("{0}:{1}", strUser, strPasswd)));
                client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", authheadertext);
                client.DefaultRequestHeaders.Add("X-Forte-Auth-Account-Id", authAccountID);

                StringContent content = new System.Net.Http.StringContent(requestBody, Encoding.UTF8, "application/json");
                try
                {
                    switch ((method).ToUpper())
                    {
                    default:
                    case "GET":
                        response = client.GetAsync(URL).Result;
                        break;

                    case "POST":
                        response = client.PostAsync(URL, content).Result;
                        break;

                    case "PUT":
                        response = client.PutAsync(URL, content).Result;
                        break;

                    case "DELETE":
                        response = client.DeleteAsync(URL).Result;
                        break;
                    }
                }
                catch (AggregateException webException)
                {
                    if (webException.Data != null)
                    {
                        var forteError = new ForteError();
                        forteError.ErrorType = "AggregateException";
                        forteError.Message   = webException.InnerException.ToString();
                        var statusCode = HttpStatusCode.Forbidden;
                        throw new ForteException(statusCode, forteError, forteError.Message);
                    }

                    throw;
                }
                catch (WebException webException)
                {
                    if (webException.Response != null)
                    {
                        var statusCode = ((HttpWebResponse)webException.Response).StatusCode;

                        var forteError = new ForteError();

                        if (webException.Response.ResponseUri.ToString().Contains("oauth"))
                        {
                            forteError = Mapper <ForteError> .MapFromJson(ReadStream(webException.Response.GetResponseStream()));
                        }
                        else
                        {
                            forteError = Mapper <ForteError> .MapFromJson(ReadStream(webException.Response.GetResponseStream()), "error");
                        }

                        throw new ForteException(statusCode, forteError, forteError.Message);
                    }

                    throw;
                }
                catch (Exception ex)
                {
                    throw ex;
                }


                if (response != null && response.IsSuccessStatusCode)
                {
                    var resultMessage = response.Content.ReadAsStringAsync().Result;

                    switch ((method).ToUpper())
                    {
                    case "POST":
                        resultMessage = "HttpStatus Code: " + response.StatusCode
                                        + "\r\n"
                                        + "Record Successfully."
                                        + "\r\n"
                                        + "||"
                                        + resultMessage;
                        break;

                    case "PUT":
                        resultMessage = "HttpStatus Code: " + response.StatusCode
                                        + "\r\n"
                                        + "Record Successfully Updated."
                                        + "\r\n"
                                        + "||"
                                        + resultMessage;

                        break;

                    case "DELETE":
                        resultMessage = "HttpStatus Code: " + response.StatusCode
                                        + "\r\n"
                                        + "Record Successfully Deleted."
                                        + "\r\n"
                                        + "||"
                                        + resultMessage;

                        break;

                    default:
                        break;
                    }
                    return(resultMessage);
                }
                else if (response != null && !(response.IsSuccessStatusCode))
                {
                    var resultMessage = "HttpStatus Code: " + response.StatusCode
                                        + "||" + "#ERROR# "
                                        + response.Content.ReadAsStringAsync().Result;
                    return(resultMessage);
                }
                else
                {
                    return("#ERROR# " + response.ToString());
                }
            }
        }