using System;
using System.Net.Http;
namespace RestEase
{
///
/// Response containing both the HttpResponseMessage and deserialized response
///
/// Type of deserialized response
public class Response : IDisposable
{
private readonly Func contentDeserializer;
private bool contentDeserialized;
private T deserializedContent = default!;
///
/// Gets the raw HttpResponseMessage
///
public HttpResponseMessage ResponseMessage { get; private set; }
///
/// Gets the string content of the response, if there is a response
///
public string? StringContent { get; private set; }
///
/// Gets the deserialized response
///
/// The deserialized content
public T GetContent()
{
if (!this.contentDeserialized)
{
this.deserializedContent = this.contentDeserializer();
this.contentDeserialized = true;
}
return this.deserializedContent;
}
///
/// Initialises a new instance of the class
///
/// String content read from the response
/// HttpResponseMessage received
/// Func which will deserialize the content into a T
public Response(string? content, HttpResponseMessage response, Func contentDeserializer)
{
this.StringContent = content;
this.ResponseMessage = response;
this.contentDeserializer = contentDeserializer;
}
///
/// Disposes the underlying
///
public void Dispose()
{
this.ResponseMessage.Dispose();
}
}
}