using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Reflection;
using System.Threading;
namespace RestEase.Implementation
{
///
/// Class containing information to construct a request from.
/// An instance of this is created per request by the generated interface implementation
///
public class RequestInfo : IRequestInfo
{
///
/// Gets the HttpMethod which should be used to make the request
///
public HttpMethod Method { get; }
///
/// Gets or sets the path which should be prepended to if any
///
public string? BasePath { get; set; }
///
/// Gets the relative path to the resource to request
///
public string Path { get; }
///
/// Gets or sets the CancellationToken used to cancel the request
///
public CancellationToken CancellationToken { get; set; } = CancellationToken.None;
///
/// Gets or sets a value indicating whether to suppress the exception on invalid status codes
///
public bool AllowAnyStatusCode { get; set; }
private List? _queryParams;
///
/// Gets the query parameters to append to the request URI
///
public IEnumerable QueryParams => this._queryParams ?? Enumerable.Empty();
private List? _rawQueryParameterInfos;
///
/// Gets the raw query parameters
///
public IEnumerable RawQueryParameters => this._rawQueryParameterInfos ?? Enumerable.Empty();
private List? _pathParams;
///
/// Gets the parameters which should be substituted into placeholders in the Path
///
public IEnumerable PathParams => this._pathParams ?? Enumerable.Empty();
private List? _pathProperties;
///
/// Gets the values from properties which should be substituted into placeholders in the Path
///
public IEnumerable PathProperties => this._pathProperties ?? Enumerable.Empty();
private List? _queryProperties;
///
/// Gets the values from properties which should be added to all query strings
///
public IEnumerable QueryProperties => this._queryProperties ?? Enumerable.Empty();
private List? _httpRequestMessageProperties;
///
/// Gets the values from properties which should be added as request properties
///
public IEnumerable HttpRequestMessageProperties => this._httpRequestMessageProperties ?? Enumerable.Empty();
///
/// Gets or sets the headers which were applied to the interface
///
public IEnumerable>? ClassHeaders { get; set; }
private List? _propertyHeaders;
///
/// Gets the headers which were applied using properties
///
public IEnumerable PropertyHeaders => this._propertyHeaders ?? Enumerable.Empty();
private List>? _methodHeaders;
///
/// Gets the headers which were applied to the method being called
///
public IEnumerable> MethodHeaders => this._methodHeaders ?? Enumerable.Empty>();
private List? _headerParams;
///
/// Gets the headers which were passed to the method as parameters
///
public IEnumerable HeaderParams => this._headerParams ?? Enumerable.Empty();
///
/// Gets information the [Body] method parameter, if it exists
///
public BodyParameterInfo? BodyParameterInfo { get; private set; }
///
/// Gets the MethodInfo of the interface method which was invoked
///
public MethodInfo MethodInfo { get; }
///
/// Initialises a new instance of the class
///
/// HttpMethod to use when making the request
/// Relative path to request
public RequestInfo(HttpMethod method, string path) : this(method, path, null!)
{
}
///
/// Initialises a new instance of the class
///
/// HttpMethod to use when making the request
/// Relative path to request
/// MethodInfo for the method which was invoked
public RequestInfo(HttpMethod method, string path, MethodInfo methodInfo)
{
this.Method = method;
this.Path = path;
this.MethodInfo = methodInfo;
}
///
/// Add a query parameter
///
/// value may be an IEnumerable, in which case each value is added separately
/// Type of the value to add
/// Method to use to serialize the value
/// Name of the name/value pair
/// Value of the name/value pair
/// Format string to use
public void AddQueryParameter(QuerySerializationMethod serializationMethod, string name, T value, string? format = null)
{
if (this._queryParams == null)
this._queryParams = new List();
this._queryParams.Add(new QueryParameterInfo(serializationMethod, name, value, format));
}
///
/// Add a collection of query parameter values under the same name
///
/// Type of the value to add
/// Method to use to serialize the value
/// Name of the name/values pair
/// Values of the name/values pairs
/// Format string to use
public void AddQueryCollectionParameter(QuerySerializationMethod serializationMethod, string name, IEnumerable values, string? format = null)
{
if (this._queryParams == null)
this._queryParams = new List();
this._queryParams.Add(new QueryCollectionParameterInfo(serializationMethod, name, values, format));
}
///
/// Add a query map to the query parameters list, where the type of value is scalar
///
/// Type of key in the query map
/// Type of value in the query map
/// Method to use to serialize the value
/// Query map to add
public void AddQueryMap(QuerySerializationMethod serializationMethod, IDictionary queryMap)
{
if (queryMap == null)
return;
if (this._queryParams == null)
this._queryParams = new List();
foreach (var kvp in queryMap)
{
if (kvp.Key == null)
continue;
// Backwards compat: if it's a dictionary of object, see if it's ienumerable.
// If it is, treat it as an ienumerable