using System;
using System.Net.Http;
namespace RestEase.Implementation
{
///
/// Class containing information about a desired request body
///
public abstract class BodyParameterInfo
{
///
/// Gets or sets the method to use to serialize the body
///
public BodySerializationMethod SerializationMethod { get; }
///
/// Gets the body to serialize, as an object
///
public object? ObjectValue { get; }
///
/// Initialises a new instance of the class
///
/// Method to use the serialize the body
/// Body to serialize, as an object
protected BodyParameterInfo(BodySerializationMethod serializationMethod, object? objectValue)
{
this.SerializationMethod = serializationMethod;
this.ObjectValue = objectValue;
}
///
/// Serialize the (typed) value using the given serializer
///
/// Serializer to use
/// RequestInfo representing the request
/// to use if the value implements
/// Serialized value
public abstract HttpContent? SerializeValue(RequestBodySerializer serializer, IRequestInfo requestInfo, IFormatProvider? formatProvider);
}
///
/// Class containing information about a desired request body
///
/// Type of the value
public class BodyParameterInfo : BodyParameterInfo
{
///
/// Gets the body to serialize
///
public T Value { get; }
///
/// Initialises a new instance of the class
///
/// Method to use the serialize the body
/// Body to serialize
public BodyParameterInfo(BodySerializationMethod serializationMethod, T value)
: base(serializationMethod, value)
{
this.Value = value;
}
///
/// Serialize the (typed) value using the given serializer
///
/// Serializer to use
/// RequestInfo representing the request
/// to use if the value implements
/// Serialized value
public override HttpContent? SerializeValue(RequestBodySerializer serializer, IRequestInfo requestInfo, IFormatProvider? formatProvider)
{
if (serializer == null)
throw new ArgumentNullException(nameof(serializer));
return serializer.SerializeBody(this.Value, new RequestBodySerializerInfo(requestInfo, formatProvider));
}
}
}