Skip to content

Commit

Permalink
feat: include API response headers in 'Last Response' (#526)
Browse files Browse the repository at this point in the history
The default HTTP client stores 'Last Request' and 'Last Response' information for access to more data about the raw API request/response. The response object did not contain the headers until now.
  • Loading branch information
childish-sambino authored Jun 29, 2020
1 parent 2c7bec5 commit 43652c9
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/Twilio/Http/Net35/WebRequestClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public override Response MakeRequest(Request request)
{
IHttpWebResponseWrapper response = httpRequest.GetResponse();
string content = GetResponseContent(response);
this.LastResponse = new Response(response.StatusCode, content);
this.LastResponse = new Response(response.StatusCode, content, response.Headers);
}
catch (WebExceptionWrapper e)
{
Expand All @@ -48,7 +48,7 @@ public override Response MakeRequest(Request request)
}
// Non 2XX status code
IHttpWebResponseWrapper errorResponse = e.Response;
this.LastResponse = new Response(errorResponse.StatusCode, GetResponseContent(errorResponse));
this.LastResponse = new Response(errorResponse.StatusCode, GetResponseContent(errorResponse), errorResponse.Headers);
}
return this.LastResponse;
}
Expand Down
6 changes: 6 additions & 0 deletions src/Twilio/Http/Net35/WebRequestWrappers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public interface IHttpWebResponseWrapper
{
HttpStatusCode StatusCode { get; set; }
Stream GetResponseStream();
WebHeaderCollection Headers { get; }
}

public class HttpWebRequestWrapper : IHttpWebRequestWrapper
Expand Down Expand Up @@ -125,6 +126,11 @@ public Stream GetResponseStream()
{
return this._httpWebResponse.GetResponseStream();
}

public WebHeaderCollection Headers
{
get { return this._httpWebResponse.Headers; }
}
}

public class WebExceptionWrapper : Exception
Expand Down
17 changes: 15 additions & 2 deletions src/Twilio/Http/Response.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
using System.Net;

#if NET35
using Headers = System.Net.WebHeaderCollection;
#else
using Headers = System.Net.Http.Headers.HttpResponseHeaders;
#endif

namespace Twilio.Http
{
/// <summary>
Expand All @@ -18,14 +24,21 @@ public class Response
public string Content { get; }

/// <summary>
/// Create a new Response
/// Headers
/// </summary>
public Headers Headers { get; }

/// <summary>
/// Create a new Response
/// </summary>
/// <param name="statusCode">HTTP status code</param>
/// <param name="content">Content string</param>
public Response (HttpStatusCode statusCode, string content)
/// <param name="headers">Headers</param>
public Response(HttpStatusCode statusCode, string content, Headers headers = null)
{
StatusCode = statusCode;
Content = content;
Headers = headers;
}
}
}
8 changes: 4 additions & 4 deletions src/Twilio/Http/SystemNetHttpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ namespace Twilio.Http
/// </summary>
public class SystemNetHttpClient : HttpClient
{
#if NET451
#if NET451
private string PlatVersion = " (.NET Framework 4.5.1+)";
#else
#else
private string PlatVersion = $" ({RuntimeInformation.FrameworkDescription})";
#endif
#endif

private readonly System.Net.Http.HttpClient _httpClient;

Expand Down Expand Up @@ -73,7 +73,7 @@ public override async Task<Response> MakeRequestAsync(Request request)
// Create and return a new Response. Keep a reference to the last
// response for debugging, but don't return it as it may be shared
// among threads.
var response = new Response(httpResponse.StatusCode, await reader.ReadToEndAsync().ConfigureAwait(false));
var response = new Response(httpResponse.StatusCode, await reader.ReadToEndAsync().ConfigureAwait(false), httpResponse.Headers);
this.LastResponse = response;
return response;
}
Expand Down

0 comments on commit 43652c9

Please sign in to comment.