Esempio n. 1
0
        private static async Task <OutcomeResponse <T> > GetOutcome <T>(string serviceUrl, string consumerKey,
                                                                        string consumerSecret, string contentType) where T : class
        {
            try
            {
                var request = (HttpWebRequest)WebRequest.Create(serviceUrl);
                request.Method            = "GET";
                request.Accept            = contentType;
                request.AllowAutoRedirect = true;

                SignRequest(request, null, consumerKey, consumerSecret);

                return(await Task.Factory.StartNew(() =>
                {
                    var outcomeResponse = new OutcomeResponse <T>();
                    HttpWebResponse response = null;
                    try
                    {
                        response = (HttpWebResponse)request.GetResponse();
                        outcomeResponse.StatusCode = response.StatusCode;
                        if (response.StatusCode == HttpStatusCode.OK)
                        {
                            outcomeResponse.Outcome = response.DeserializeObject <T>();
                        }
                    }
                    catch (WebException ex)
                    {
                        response = (HttpWebResponse)ex.Response;
                        outcomeResponse.StatusCode = response.StatusCode;
                    }
                    catch (Exception)
                    {
                        outcomeResponse.StatusCode = HttpStatusCode.InternalServerError;
                    }
                    finally
                    {
#if DEBUG
                        outcomeResponse.HttpRequest = request.ToFormattedRequestString();
                        if (response != null)
                        {
                            outcomeResponse.HttpResponse = response.ToFormattedResponseString(
                                outcomeResponse.Outcome == null
                                ? null
                                : outcomeResponse.Outcome.ToJsonString());
                        }
#endif
                    }
                    return outcomeResponse;
                }));
            }
            catch (Exception)
            {
                return(new OutcomeResponse <T> {
                    StatusCode = HttpStatusCode.InternalServerError
                });
            }
        }
Esempio n. 2
0
        private static async Task <OutcomeResponse <T> > PostOutcome <T>(T outcome, string serviceUrl,
                                                                         string consumerKey, string consumerSecret, string contentType) where T : class
        {
            try
            {
                var request = (HttpWebRequest)WebRequest.Create(serviceUrl);
                request.Method      = "POST";
                request.ContentType = contentType;

                var body = Encoding.UTF8.GetBytes(outcome.ToJsonLdString());
                using (var stream = request.GetRequestStream())
                {
                    await stream.WriteAsync(body, 0, body.Length);
                }

                SignRequest(request, body, consumerKey, consumerSecret);

                return(await Task.Factory.StartNew(() =>
                {
                    var outcomeResponse = new OutcomeResponse <T>();
                    HttpWebResponse response = null;
                    try
                    {
                        response = (HttpWebResponse)request.GetResponse();
                        outcomeResponse.StatusCode = response.StatusCode;
                        if (response.StatusCode == HttpStatusCode.Created)
                        {
                            outcomeResponse.Outcome = response.DeserializeObject <T>();
                        }
                    }
                    catch (WebException ex)
                    {
                        response = (HttpWebResponse)ex.Response;
                        outcomeResponse.StatusCode = response.StatusCode;
                    }
                    catch (Exception)
                    {
                        outcomeResponse.StatusCode = HttpStatusCode.InternalServerError;
                    }
                    finally
                    {
#if DEBUG
                        outcomeResponse.HttpRequest = request.ToFormattedRequestString();
                        if (response != null)
                        {
                            outcomeResponse.HttpResponse = response.ToFormattedResponseString(
                                outcomeResponse.Outcome == null
                                ? null
                                : outcomeResponse.Outcome.ToJsonString());
                        }
#endif
                    }
                    return outcomeResponse;
                }));
            }
            catch (Exception)
            {
                return(new OutcomeResponse <T>
                {
                    StatusCode = HttpStatusCode.InternalServerError
                });
            }
        }