Esempio n. 1
0
        protected async Task <string> GetResponseFromEndpoint(string link, string postBody = null)
        {
            Context.BuilderDelegates.RequestSend(new RequestSendEventArgs(link, postBody));
            if (Context.StatisticsEnabled)
            {
                Context.Statistics.RequestsMade++;
            }

            HttpResponseMessage response;

            if (postBody == null)
            {
                response = await Context.HttpClient.GetAsync(link).ConfigureAwait(false);
            }
            else
            {
                var httpContent = new StringContent(postBody, Encoding.UTF8, "application/json");
                response = await Context.HttpClient.PostAsync(link, httpContent).ConfigureAwait(false);
            }

            var content = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

            var eventArgs = new ResponseReceiveEventArgs(content, response.StatusCode, response.ReasonPhrase);

            if (response.StatusCode != HttpStatusCode.OK)
            {
                switch (response.StatusCode)
                {
                case HttpStatusCode.NotFound:
                {
                    return(null);
                }

                case HttpStatusCode.BadRequest:
                {
                    throw new ApiBadRequestException(content);
                }

                default:
                {
                    throw new ApiUnavailableException($"Status code: {(int)response.StatusCode}");
                }
                }
            }

            Context.BuilderDelegates.ResponseReceived(eventArgs);
            if (Context.StatisticsEnabled)
            {
                Context.Statistics.ResponsesReceived++;
            }

            return(content);
        }
Esempio n. 2
0
 private static void OddityOnResponseReceive(object sender, ResponseReceiveEventArgs e)
 {
     Console.WriteLine($"Response received! Status code: {e.StatusCode}");
     Console.WriteLine($"Raw content: {e.Response}");
     Console.WriteLine();
 }
Esempio n. 3
0
 private void ResponseReceived(ResponseReceiveEventArgs args)
 {
     OnResponseReceive?.Invoke(this, args);
 }
Esempio n. 4
0
 private void Oddity_OnResponseReceive(object sender, ResponseReceiveEventArgs e)
 {
     _logger.Info($"Oddity response received ({e.Response?.Length} chars, status {e.StatusCode})");
 }