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); }
private static void OddityOnResponseReceive(object sender, ResponseReceiveEventArgs e) { Console.WriteLine($"Response received! Status code: {e.StatusCode}"); Console.WriteLine($"Raw content: {e.Response}"); Console.WriteLine(); }
private void ResponseReceived(ResponseReceiveEventArgs args) { OnResponseReceive?.Invoke(this, args); }
private void Oddity_OnResponseReceive(object sender, ResponseReceiveEventArgs e) { _logger.Info($"Oddity response received ({e.Response?.Length} chars, status {e.StatusCode})"); }