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 = new HttpRequestMessage(HttpMethod.Get, serviceUrl);
                request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue(contentType));

                SignRequest(request, null, consumerKey, consumerSecret);

                var outcomeResponse          = new OutcomeResponse <T>();
                HttpResponseMessage response = null;
                try
                {
#if DEBUG
                    // Capture the request in human readable form for debugging
                    // and inspection while learning
                    outcomeResponse.HttpRequest = await request.ToFormattedRequestStringAsync();
#endif
                    response = await request.GetResponseAsync(allowAutoRedirect : true);

                    outcomeResponse.StatusCode = response.StatusCode;
                    if (response.StatusCode == HttpStatusCode.OK)
                    {
                        outcomeResponse.Outcome = response.DeserializeObject <T>();
                    }
                }
                catch (HttpRequestException)
                {
                    outcomeResponse.StatusCode = response.StatusCode;
                }
                catch (Exception)
                {
                    outcomeResponse.StatusCode = HttpStatusCode.InternalServerError;
                }
                finally
                {
#if DEBUG
                    // Capture the response in human readable form for debugging
                    // and inspection while learning
                    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> > 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. 3
0
        private static async Task <OutcomeResponse <T> > PostOutcomeAsync <T>(T outcome, string serviceUrl,
                                                                              string consumerKey, string consumerSecret, string contentType) where T : class
        {
            try
            {
                var request = new HttpRequestMessage(HttpMethod.Post, serviceUrl);

                var body = Encoding.UTF8.GetBytes(outcome.ToJsonLdString());
                request.Content = new ByteArrayContent(body);
                request.Content.Headers.ContentType = new MediaTypeHeaderValue(contentType);

                SignRequest(request, body, consumerKey, consumerSecret);

                var outcomeResponse = new OutcomeResponse <T>();
                try
                {
#if DEBUG
                    // Capture the request in human readable form for debugging
                    // and inspection while learning
                    outcomeResponse.HttpRequest = await request.ToFormattedRequestStringAsync();
#endif
                    using (var response = await request.GetResponseAsync())
                    {
                        outcomeResponse.StatusCode = response.StatusCode;
                        if (response.StatusCode == HttpStatusCode.Created)
                        {
                            outcomeResponse.Outcome = response.DeserializeObject <T>();
                        }
#if DEBUG
                        // Capture the response in human readable form for debugging
                        // and inspection while learning
                        outcomeResponse.HttpResponse = response.ToFormattedResponseString(
                            outcomeResponse.Outcome?.ToJsonString());
#endif
                    }
                }
                catch (HttpRequestException ex)
                {
                    outcomeResponse.Exception  = ex;
                    outcomeResponse.StatusCode = HttpStatusCode.BadRequest;
                }
                catch (Exception ex)
                {
                    outcomeResponse.Exception  = ex;
                    outcomeResponse.StatusCode = HttpStatusCode.InternalServerError;
                }
                return(outcomeResponse);
            }
            catch (Exception ex)
            {
                return(new OutcomeResponse <T>
                {
                    Exception = ex,
                    StatusCode = HttpStatusCode.InternalServerError
                });
            }
        }
Esempio n. 4
0
        private static async Task <OutcomeResponse> DeleteOutcomeAsync(string serviceUrl, string consumerKey,
                                                                       string consumerSecret, string contentType = null)
        {
            try
            {
                var request = new HttpRequestMessage(HttpMethod.Delete, serviceUrl);
                // Content-Type header is not required. If not specified, all representations of
                // the resource will be deleted.
                // See https://www.imsglobal.org/lti/model/uml/purl.imsglobal.org/vocab/lis/v2/outcomes/LineItem/service.html#DELETE
                if (!string.IsNullOrWhiteSpace(contentType))
                {
                    request.Content = new StringContent(string.Empty);
                    request.Content.Headers.ContentType = new MediaTypeHeaderValue(contentType);
                }

                SignRequest(request, null, consumerKey, consumerSecret);

                var outcomeResponse = new OutcomeResponse();
                try
                {
#if DEBUG
                    // Capture the request in human readable form for debugging
                    // and inspection while learning
                    outcomeResponse.HttpRequest = await request.ToFormattedRequestStringAsync();
#endif
                    using (var response = await request.GetResponseAsync())
                    {
                        outcomeResponse.StatusCode = response.StatusCode;
#if DEBUG
                        // Capture the response in human readable form for debugging
                        // and inspection while learning
                        outcomeResponse.HttpResponse = response.ToFormattedResponseString(null);
#endif
                    }
                }
                catch (HttpRequestException ex)
                {
                    outcomeResponse.Exception  = ex;
                    outcomeResponse.StatusCode = HttpStatusCode.BadRequest;
                }
                catch (Exception ex)
                {
                    outcomeResponse.Exception  = ex;
                    outcomeResponse.StatusCode = HttpStatusCode.InternalServerError;
                }
                return(outcomeResponse);
            }
            catch (Exception ex)
            {
                return(new OutcomeResponse
                {
                    Exception = ex,
                    StatusCode = HttpStatusCode.InternalServerError
                });
            }
        }
Esempio n. 5
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 = new HttpRequestMessage(HttpMethod.Post, serviceUrl);
                request.Content.Headers.ContentType = new MediaTypeHeaderValue(contentType);

                var body = Encoding.UTF8.GetBytes(outcome.ToJsonLdString());
                request.Content = new ByteArrayContent(body);

                SignRequest(request, body, consumerKey, consumerSecret);

                var outcomeResponse          = new OutcomeResponse <T>();
                HttpResponseMessage response = null;
                try
                {
                    response = await request.GetResponseAsync();

                    outcomeResponse.StatusCode = response.StatusCode;
                    if (response.StatusCode == HttpStatusCode.Created)
                    {
                        outcomeResponse.Outcome = response.DeserializeObject <T>();
                    }
                }
                catch (HttpRequestException)
                {
                    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. 6
0
        private static async Task <OutcomeResponse> PutOutcome <T>(T outcome, string serviceUrl, string consumerKey,
                                                                   string consumerSecret, string contentType)
        {
            try
            {
                var request = (HttpWebRequest)WebRequest.Create(serviceUrl);
                request.Method      = "PUT";
                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();
                    HttpWebResponse response = null;
                    try
                    {
                        response = (HttpWebResponse)request.GetResponse();
                        outcomeResponse.StatusCode = response.StatusCode;
                    }
                    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(null);
                        }
#endif
                    }
                    return outcomeResponse;
                }));
            }
            catch (Exception)
            {
                return(new OutcomeResponse {
                    StatusCode = HttpStatusCode.InternalServerError
                });
            }
        }
Esempio n. 7
0
        private static async Task <OutcomeResponse> PutOutcome <T>(T outcome, string serviceUrl, string consumerKey,
                                                                   string consumerSecret, string contentType)
        {
            try
            {
                var request = new HttpRequestMessage(HttpMethod.Put, serviceUrl);

                var body = Encoding.UTF8.GetBytes(outcome.ToJsonLdString());
                request.Content = new ByteArrayContent(body);
                request.Content.Headers.ContentType = new MediaTypeHeaderValue(contentType);

                SignRequest(request, body, consumerKey, consumerSecret);

                var outcomeResponse          = new OutcomeResponse();
                HttpResponseMessage response = null;
                try
                {
#if DEBUG
                    // Capture the request in human readable form for debugging
                    // and inspection while learning
                    outcomeResponse.HttpRequest = await request.ToFormattedRequestStringAsync();
#endif
                    response = await request.GetResponseAsync();

                    outcomeResponse.StatusCode = response.StatusCode;
                }
                catch (HttpRequestException)
                {
                    outcomeResponse.StatusCode = response.StatusCode;
                }
                catch (Exception)
                {
                    outcomeResponse.StatusCode = HttpStatusCode.InternalServerError;
                }
                finally
                {
#if DEBUG
                    // Capture the response in human readable form for debugging
                    // and inspection while learning
                    if (response != null)
                    {
                        outcomeResponse.HttpResponse = response.ToFormattedResponseString(null);
                    }
#endif
                }
                return(outcomeResponse);
            }
            catch (Exception)
            {
                return(new OutcomeResponse {
                    StatusCode = HttpStatusCode.InternalServerError
                });
            }
        }
Esempio n. 8
0
        private static async Task <OutcomeResponse> DeleteOutcome(string serviceUrl, string consumerKey,
                                                                  string consumerSecret, string contentType = null)
        {
            try
            {
                var request = (HttpWebRequest)WebRequest.Create(serviceUrl);
                request.Method = "DELETE";
                if (!string.IsNullOrWhiteSpace(contentType))
                {
                    request.ContentType = contentType;
                }

                SignRequest(request, null, consumerKey, consumerSecret);

                return(await Task.Factory.StartNew(() =>
                {
                    var outcomeResponse = new OutcomeResponse();
                    HttpWebResponse response = null;
                    try
                    {
                        response = (HttpWebResponse)request.GetResponse();
                        outcomeResponse.StatusCode = response.StatusCode;
                    }
                    catch (WebException ex)
                    {
                        response = (HttpWebResponse)ex.Response;
                        outcomeResponse.StatusCode = response.StatusCode;
                    }
                    catch (Exception)
                    {
                        outcomeResponse.StatusCode = HttpStatusCode.InternalServerError;
                    }
#if DEBUG
                    finally
                    {
                        outcomeResponse.HttpRequest = request.ToFormattedRequestString();
                        if (response != null)
                        {
                            outcomeResponse.HttpResponse = response.ToFormattedResponseString(null);
                        }
                    }
#endif
                    return outcomeResponse;
                }));
            }
            catch (Exception)
            {
                return(new OutcomeResponse {
                    StatusCode = HttpStatusCode.InternalServerError
                });
            }
        }
Esempio n. 9
0
        private static async Task <OutcomeResponse> DeleteOutcome(string serviceUrl, string consumerKey,
                                                                  string consumerSecret, string contentType = null)
        {
            try
            {
                var request = new HttpRequestMessage(HttpMethod.Delete, serviceUrl);

                SignRequest(request, null, consumerKey, consumerSecret);

                var outcomeResponse          = new OutcomeResponse();
                HttpResponseMessage response = null;
                try
                {
#if DEBUG
                    // Capture the request in human readable form for debugging
                    // and inspection while learning
                    outcomeResponse.HttpRequest = await request.ToFormattedRequestStringAsync();
#endif
                    response = await request.GetResponseAsync();

                    outcomeResponse.StatusCode = response.StatusCode;
                }
                catch (HttpRequestException)
                {
                    outcomeResponse.StatusCode = response.StatusCode;
                }
                catch (Exception)
                {
                    outcomeResponse.StatusCode = HttpStatusCode.InternalServerError;
                }
#if DEBUG
                finally
                {
                    // Capture the response in human readable form for debugging
                    // and inspection while learning
                    if (response != null)
                    {
                        outcomeResponse.HttpResponse = response.ToFormattedResponseString(null);
                    }
                }
#endif
                return(outcomeResponse);
            }
            catch (Exception)
            {
                return(new OutcomeResponse {
                    StatusCode = HttpStatusCode.InternalServerError
                });
            }
        }
Esempio n. 10
0
        private static async Task <OutcomeResponse> DeleteOutcome(string serviceUrl, string consumerKey,
                                                                  string consumerSecret, string contentType = null)
        {
            try
            {
                var request = new HttpRequestMessage(HttpMethod.Delete, serviceUrl);
                if (!string.IsNullOrWhiteSpace(contentType))
                {
                    request.Content.Headers.ContentType = new MediaTypeHeaderValue(contentType);
                }

                SignRequest(request, null, consumerKey, consumerSecret);

                var outcomeResponse          = new OutcomeResponse();
                HttpResponseMessage response = null;
                try
                {
                    response = await request.GetResponseAsync();

                    outcomeResponse.StatusCode = response.StatusCode;
                }
                catch (HttpRequestException)
                {
                    outcomeResponse.StatusCode = response.StatusCode;
                }
                catch (Exception)
                {
                    outcomeResponse.StatusCode = HttpStatusCode.InternalServerError;
                }
#if DEBUG
                finally
                {
                    outcomeResponse.HttpRequest = request.ToFormattedRequestString();
                    if (response != null)
                    {
                        outcomeResponse.HttpResponse = response.ToFormattedResponseString(null);
                    }
                }
#endif
                return(outcomeResponse);
            }
            catch (Exception)
            {
                return(new OutcomeResponse {
                    StatusCode = HttpStatusCode.InternalServerError
                });
            }
        }