refit/Refit/Attributes.cs

549 строки
18 KiB
C#
Исходник Обычный вид История

using System.Net.Http;
2013-07-22 23:23:37 +04:00
namespace Refit
{
/// <summary>
/// HttpMethodAttribute.
/// </summary>
/// <seealso cref="System.Attribute" />
/// <remarks>
/// Initializes a new instance of the <see cref="HttpMethodAttribute"/> class.
/// </remarks>
/// <param name="path">The path.</param>
public abstract class HttpMethodAttribute(string path) : Attribute
2013-07-22 23:23:37 +04:00
{
/// <summary>
/// Gets the method.
/// </summary>
/// <value>
/// The method.
/// </value>
2018-01-14 22:58:24 +03:00
public abstract HttpMethod Method { get; }
/// <summary>
/// Gets or sets the path.
/// </summary>
/// <value>
/// The path.
/// </value>
public virtual string Path { get; protected set; } = path;
2013-07-22 23:23:37 +04:00
}
/// <summary>
/// Send the request with HTTP method 'GET'.
/// </summary>
/// <remarks>
/// Initializes a new instance of the <see cref="GetAttribute"/> class.
/// </remarks>
/// <param name="path">The path.</param>
2013-07-22 23:23:37 +04:00
[AttributeUsage(AttributeTargets.Method)]
#pragma warning disable CA1813 // Avoid unsealed attributes
public class GetAttribute(string path) : HttpMethodAttribute(path)
2013-07-22 23:23:37 +04:00
{
/// <summary>
/// Gets the method.
/// </summary>
/// <value>
/// The method.
/// </value>
public override HttpMethod Method => HttpMethod.Get;
2013-07-22 23:23:37 +04:00
}
/// <summary>
/// Send the request with HTTP method 'POST'.
/// </summary>
/// <remarks>
/// Initializes a new instance of the <see cref="PostAttribute"/> class.
/// </remarks>
/// <param name="path">The path.</param>
2013-07-22 23:23:37 +04:00
[AttributeUsage(AttributeTargets.Method)]
public class PostAttribute(string path) : HttpMethodAttribute(path)
2013-07-22 23:23:37 +04:00
{
/// <summary>
/// Gets the method.
/// </summary>
/// <value>
/// The method.
/// </value>
public override HttpMethod Method => HttpMethod.Post;
2013-07-22 23:23:37 +04:00
}
/// <summary>
/// Send the request with HTTP method 'PUT'.
/// </summary>
/// <remarks>
/// Initializes a new instance of the <see cref="PutAttribute"/> class.
/// </remarks>
/// <param name="path">The path.</param>
2013-07-22 23:23:37 +04:00
[AttributeUsage(AttributeTargets.Method)]
public class PutAttribute(string path) : HttpMethodAttribute(path)
2013-07-22 23:23:37 +04:00
{
/// <summary>
/// Gets the method.
/// </summary>
/// <value>
/// The method.
/// </value>
public override HttpMethod Method => HttpMethod.Put;
2013-07-22 23:23:37 +04:00
}
/// <summary>
/// Send the request with HTTP method 'DELETE'.
/// </summary>
/// <remarks>
/// Initializes a new instance of the <see cref="DeleteAttribute"/> class.
/// </remarks>
/// <param name="path">The path.</param>
2013-07-22 23:23:37 +04:00
[AttributeUsage(AttributeTargets.Method)]
public class DeleteAttribute(string path) : HttpMethodAttribute(path)
2013-07-22 23:23:37 +04:00
{
/// <summary>
/// Gets the method.
/// </summary>
/// <value>
/// The method.
/// </value>
public override HttpMethod Method => HttpMethod.Delete;
2013-07-22 23:23:37 +04:00
}
2015-01-15 15:06:43 +03:00
/// <summary>
/// Send the request with HTTP method 'PATCH'.
/// </summary>
/// <remarks>
/// Initializes a new instance of the <see cref="PatchAttribute"/> class.
/// </remarks>
/// <param name="path">The path.</param>
2015-01-15 15:06:43 +03:00
[AttributeUsage(AttributeTargets.Method)]
public class PatchAttribute(string path) : HttpMethodAttribute(path)
2015-01-15 15:06:43 +03:00
{
/// <summary>
/// Gets the method.
/// </summary>
/// <value>
/// The method.
/// </value>
public override HttpMethod Method => new("PATCH");
2015-01-15 15:06:43 +03:00
}
2018-01-14 22:58:24 +03:00
/// <summary>
/// Send the request with HTTP method 'OPTION'.
/// </summary>
/// <remarks>
/// Initializes a new instance of the <see cref="OptionsAttribute"/> class.
/// </remarks>
/// <param name="path">The path.</param>
[AttributeUsage(AttributeTargets.Method)]
public class OptionsAttribute(string path) : HttpMethodAttribute(path)
{
/// <summary>
/// Gets the method.
/// </summary>
/// <value>
/// The method.
/// </value>
public override HttpMethod Method => new("OPTIONS");
}
/// <summary>
/// Send the request with HTTP method 'HEAD'.
/// </summary>
/// <remarks>
/// Initializes a new instance of the <see cref="HeadAttribute"/> class.
/// </remarks>
/// <param name="path">The path.</param>
2013-07-22 23:23:37 +04:00
[AttributeUsage(AttributeTargets.Method)]
public class HeadAttribute(string path) : HttpMethodAttribute(path)
2013-07-22 23:23:37 +04:00
{
/// <summary>
/// Gets the method.
/// </summary>
/// <value>
/// The method.
/// </value>
public override HttpMethod Method => HttpMethod.Head;
2013-07-22 23:23:37 +04:00
}
/// <summary>
/// Send the request as multipart.
/// </summary>
/// <remarks>
/// Currently, multipart methods only support the following parameter types: <see cref="string"/>, <see cref="byte"/> array, <see cref="System.IO.Stream"/>, <see cref="System.IO.FileInfo"/>.
/// </remarks>
/// <remarks>
/// Initializes a new instance of the <see cref="MultipartAttribute"/> class.
/// </remarks>
/// <param name="boundaryText">The boundary text.</param>
2015-05-17 11:18:30 +03:00
[AttributeUsage(AttributeTargets.Method)]
public class MultipartAttribute(string boundaryText = "----MyGreatBoundary") : Attribute
{
/// <summary>
/// Gets the boundary text.
/// </summary>
/// <value>
/// The boundary text.
/// </value>
public string BoundaryText { get; private set; } = boundaryText;
}
2015-05-17 11:18:30 +03:00
/// <summary>
/// Defines methods to serialize HTTP requests' bodies.
/// </summary>
2018-01-14 22:58:24 +03:00
public enum BodySerializationMethod
{
/// <summary>
/// Encodes everything using the ContentSerializer in RefitSettings except for strings. Strings are set as-is
/// </summary>
Default,
/// <summary>
/// Json encodes everything, including strings
/// </summary>
[Obsolete("Use BodySerializationMethod.Serialized instead", false)]
2018-01-14 22:58:24 +03:00
Json,
/// <summary>
/// Form-UrlEncode's the values
/// </summary>
UrlEncoded,
/// <summary>
2020-11-22 04:34:01 +03:00
/// Encodes everything using the ContentSerializer in RefitSettings
/// </summary>
Serialized
2013-07-22 23:23:37 +04:00
}
/// <summary>
/// Set a parameter to be sent as the HTTP request's body.
/// </summary>
/// <remarks>
/// There are four behaviors when sending a parameter as the request body:<br/>
/// - If the type is/implements <see cref="System.IO.Stream"/>, the content will be streamed via <see cref="StreamContent"/>.<br/>
/// - If the type is <see cref="string"/>, it will be used directly as the content unless <c>[Body(BodySerializationMethod.Json)]</c> is set
/// which will send it as a <see cref="StringContent"/>.<br/>
/// - If the parameter has the attribute <c>[Body(BodySerializationMethod.UrlEncoded)]</c>, the content will be URL-encoded.<br/>
/// - For all other types, the object will be serialized using the content serializer specified in the request's <see cref="RefitSettings"/>.
/// </remarks>
2013-07-22 23:23:37 +04:00
[AttributeUsage(AttributeTargets.Parameter)]
public class BodyAttribute : Attribute
2013-07-22 23:23:37 +04:00
{
/// <summary>
/// Initializes a new instance of the <see cref="BodyAttribute"/> class.
/// </summary>
2023-11-27 01:50:46 +03:00
public BodyAttribute() { }
2018-02-16 23:16:46 +03:00
/// <summary>
/// Initializes a new instance of the <see cref="BodyAttribute"/> class.
/// </summary>
/// <param name="buffered">if set to <c>true</c> [buffered].</param>
public BodyAttribute(bool buffered) => Buffered = buffered;
2018-02-16 23:16:46 +03:00
/// <summary>
/// Initializes a new instance of the <see cref="BodyAttribute"/> class.
/// </summary>
/// <param name="serializationMethod">The serialization method.</param>
/// <param name="buffered">if set to <c>true</c> [buffered].</param>
2018-02-16 23:16:46 +03:00
public BodyAttribute(BodySerializationMethod serializationMethod, bool buffered)
2013-07-22 23:23:37 +04:00
{
SerializationMethod = serializationMethod;
Buffered = buffered;
2013-07-22 23:23:37 +04:00
}
2018-01-14 22:58:24 +03:00
/// <summary>
/// Initializes a new instance of the <see cref="BodyAttribute"/> class.
/// </summary>
/// <param name="serializationMethod">The serialization method.</param>
2023-11-27 01:50:46 +03:00
public BodyAttribute(
BodySerializationMethod serializationMethod = BodySerializationMethod.Default
)
2018-02-16 23:16:46 +03:00
{
SerializationMethod = serializationMethod;
}
/// <summary>
/// Gets or sets the buffered.
/// </summary>
/// <value>
/// The buffered.
/// </value>
public bool? Buffered { get; }
/// <summary>
/// Gets or sets the serialization method.
/// </summary>
/// <value>
/// The serialization method.
/// </value>
public BodySerializationMethod SerializationMethod { get; } =
2023-11-27 01:50:46 +03:00
BodySerializationMethod.Default;
2013-07-22 23:23:37 +04:00
}
/// <summary>
/// Override the key that will be sent in the query string.
/// </summary>
/// <remarks>
/// Initializes a new instance of the <see cref="AliasAsAttribute"/> class.
/// </remarks>
/// <param name="name">The name.</param>
2014-10-11 02:51:06 +04:00
[AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Property)]
public class AliasAsAttribute(string name) : Attribute
2013-07-22 23:23:37 +04:00
{
/// <summary>
/// Gets or sets the name.
/// </summary>
/// <value>
/// The name.
/// </value>
public string Name { get; protected set; } = name;
2013-07-22 23:23:37 +04:00
}
/// <summary>
/// Initializes a new instance of the <see cref="AttachmentNameAttribute"/> class.
/// </summary>
/// <param name="name">The name.</param>
2018-01-14 22:58:24 +03:00
[AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Property)]
2023-11-27 01:50:46 +03:00
[Obsolete(
"Use Refit.StreamPart, Refit.ByteArrayPart, Refit.FileInfoPart or if necessary, inherit from Refit.MultipartItem",
false
)]
public class AttachmentNameAttribute(string name) : Attribute
{
/// <summary>
/// Gets or sets the name.
/// </summary>
/// <value>
/// The name.
/// </value>
public string Name { get; protected set; } = name;
}
2020-11-30 13:17:54 +03:00
/// <summary>
/// Allows you to provide a Dictionary of headers to be added to the request.
2020-11-30 13:17:54 +03:00
/// </summary>
2020-11-26 13:26:12 +03:00
[AttributeUsage(AttributeTargets.Parameter)]
public class HeaderCollectionAttribute : Attribute { }
2020-11-26 13:26:12 +03:00
/// <summary>
/// Add multiple headers to the request.
/// </summary>
/// <remarks>
/// Initializes a new instance of the <see cref="HeadersAttribute"/> class.
/// </remarks>
/// <param name="headers">The headers.</param>
[AttributeUsage(AttributeTargets.Interface | AttributeTargets.Method)]
public class HeadersAttribute(params string[] headers) : Attribute
{
/// <summary>
/// Gets the headers.
/// </summary>
/// <value>
/// The headers.
/// </value>
public string[] Headers { get; } = headers ?? [];
}
/// <summary>
/// Add a header to the request.
/// </summary>
/// <remarks>
/// Initializes a new instance of the <see cref="HeaderAttribute"/> class.
/// </remarks>
/// <param name="header">The header.</param>
[AttributeUsage(AttributeTargets.Parameter)]
public class HeaderAttribute(string header) : Attribute
{
/// <summary>
/// Gets the header.
/// </summary>
/// <value>
/// The header.
/// </value>
public string Header { get; } = header;
}
2020-11-24 12:33:16 +03:00
/// <summary>
/// Used to store the value in HttpRequestMessage.Properties for further processing in a custom DelegatingHandler.
/// If a string is supplied to the constructor then it will be used as the key in the HttpRequestMessage.Properties dictionary.
/// If no key is specified then the key will be defaulted to the name of the parameter.
/// </summary>
2020-11-22 04:34:01 +03:00
[AttributeUsage(AttributeTargets.Parameter)]
public class PropertyAttribute : Attribute
2020-11-22 04:34:01 +03:00
{
/// <summary>
/// Initializes a new instance of the <see cref="PropertyAttribute"/> class.
/// </summary>
public PropertyAttribute() { }
/// <summary>
/// Initializes a new instance of the <see cref="PropertyAttribute"/> class.
/// </summary>
/// <param name="key">The key.</param>
public PropertyAttribute(string key)
2020-11-22 04:34:01 +03:00
{
Key = key;
2020-11-22 04:34:01 +03:00
}
2020-11-24 12:33:16 +03:00
/// <summary>
/// Specifies the key under which to store the value on the HttpRequestMessage.Properties dictionary.
/// </summary>
2020-11-26 00:52:29 +03:00
public string? Key { get; }
2020-11-22 04:34:01 +03:00
}
/// <summary>
/// Add the Authorize header to the request with the value of the associated parameter.
/// </summary>
/// <remarks>
/// Default authorization scheme: Bearer
/// </remarks>
/// <remarks>
/// Initializes a new instance of the <see cref="AuthorizeAttribute"/> class.
/// </remarks>
/// <param name="scheme">The scheme.</param>
[AttributeUsage(AttributeTargets.Parameter)]
public class AuthorizeAttribute(string scheme = "Bearer") : Attribute
{
/// <summary>
/// Gets the scheme.
/// </summary>
/// <value>
/// The scheme.
/// </value>
public string Scheme { get; } = scheme;
}
/// <summary>
/// Associated value will be added to the request Uri as query-string, using a delimiter to split the values. (default: '.')
/// </summary>
2018-01-28 23:05:38 +03:00
[AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Property)] // Property is to allow for form url encoded data
public class QueryAttribute : Attribute
{
CollectionFormat? collectionFormat;
/// <summary>
/// Initializes a new instance of the <see cref="QueryAttribute"/> class.
/// </summary>
2018-01-14 22:58:24 +03:00
public QueryAttribute() { }
/// <summary>
/// Initializes a new instance of the <see cref="QueryAttribute"/> class.
/// </summary>
/// <param name="delimiter">The delimiter.</param>
public QueryAttribute(string delimiter)
{
Delimiter = delimiter;
}
/// <summary>
/// Initializes a new instance of the <see cref="QueryAttribute"/> class.
/// </summary>
/// <param name="delimiter">The delimiter.</param>
/// <param name="prefix">The prefix.</param>
public QueryAttribute(string delimiter, string prefix)
{
Delimiter = delimiter;
Prefix = prefix;
}
/// <summary>
/// Initializes a new instance of the <see cref="QueryAttribute"/> class.
/// </summary>
/// <param name="delimiter">The delimiter.</param>
/// <param name="prefix">The prefix.</param>
/// <param name="format">The format.</param>
2018-01-28 23:05:38 +03:00
public QueryAttribute(string delimiter, string prefix, string format)
{
Delimiter = delimiter;
Prefix = prefix;
Format = format;
}
/// <summary>
/// Initializes a new instance of the <see cref="QueryAttribute"/> class.
/// </summary>
/// <param name="collectionFormat">The collection format.</param>
2018-03-29 14:20:24 +03:00
public QueryAttribute(CollectionFormat collectionFormat)
{
CollectionFormat = collectionFormat;
}
/// <summary>
/// Used to customize the name of either the query parameter pair or of the form field when form encoding.
/// </summary>
/// <seealso cref="Prefix"/>
2018-01-14 22:58:24 +03:00
public string Delimiter { get; protected set; } = ".";
/// <summary>
2020-11-22 04:34:01 +03:00
/// Used to customize the name of the encoded value.
/// </summary>
/// <remarks>
/// Gets combined with <see cref="Delimiter"/> in the format <code>var name = $"{Prefix}{Delimiter}{originalFieldName}"</code>
/// where <c>originalFieldName</c> is the name of the object property or method parameter.
/// </remarks>
/// <example>
/// <code>
/// class Form
/// {
/// [Query("-", "dontlog")]
/// public string password { get; }
/// }
/// </code>
/// will result in the encoded form having a field named <c>dontlog-password</c>.
/// </example>
2020-11-26 00:52:29 +03:00
public string? Prefix { get; protected set; }
2018-01-28 23:05:38 +03:00
#pragma warning disable CA1019 // Define accessors for attribute arguments
/// <summary>
/// Used to customize the formatting of the encoded value.
/// </summary>
/// <example>
/// <code>
/// interface IServerApi
/// {
/// [Get("/expenses")]
/// Task addExpense([Query(Format="0.00")] double expense);
/// }
/// </code>
/// Calling <c>serverApi.addExpense(5)</c> will result in a URI of <c>{baseUri}/expenses?expense=5.00</c>.
/// </example>
2020-11-26 00:52:29 +03:00
public string? Format { get; set; }
2018-03-29 14:20:24 +03:00
/// <summary>
/// Specifies how the collection should be encoded.
/// </summary>
public CollectionFormat CollectionFormat
{
// Cannot make property nullable due to Attribute restrictions
get => collectionFormat.GetValueOrDefault();
set => collectionFormat = value;
}
#pragma warning restore CA1019 // Define accessors for attribute arguments
/// <summary>
/// Gets a value indicating whether this instance is collection format specified.
/// </summary>
/// <value>
/// <c>true</c> if this instance is collection format specified; otherwise, <c>false</c>.
/// </value>
public bool IsCollectionFormatSpecified => collectionFormat.HasValue;
}
2020-01-11 19:16:04 +03:00
/// <summary>
/// QueryUriFormatAttribute.
/// </summary>
/// <seealso cref="System.Attribute" />
/// <remarks>
/// Initializes a new instance of the <see cref="QueryUriFormatAttribute"/> class.
/// </remarks>
/// <param name="uriFormat">The URI format.</param>
2020-01-11 19:16:04 +03:00
[AttributeUsage(AttributeTargets.Method)]
public class QueryUriFormatAttribute(UriFormat uriFormat) : Attribute
2020-01-11 19:16:04 +03:00
{
/// <summary>
/// Specifies how the Query Params should be encoded.
/// </summary>
public UriFormat UriFormat { get; } = uriFormat;
2020-01-11 19:16:04 +03:00
}
#pragma warning restore CA1813 // Avoid unsealed attributes
}