using System;
using System.Net.Http;
namespace RestEase
{
///
/// Base class for all request attributes
///
public abstract class RequestAttributeBase : Attribute
{
///
/// Gets the HTTP method to use (Get/Post/etc)
///
public HttpMethod Method { get; }
///
/// Gets or sets the path to request. This is relative to the base path configured when RestService.For was called, and can contain placeholders
///
public string? Path { get; set; }
internal RequestAttributeBase(HttpMethod method)
{
this.Method = method;
}
internal RequestAttributeBase(HttpMethod method, string path)
{
this.Method = method;
this.Path = path;
}
}
///
/// Attribute for custom HTTP methods which aren't represented by other subclasses of RequestAttributeBase
///
[AttributeUsage(AttributeTargets.Method, Inherited = false, AllowMultiple = false)]
public sealed class RequestAttribute : RequestAttributeBase
{
///
/// Initialises a new instance of the class, with the given HttpMethod.
///
///
/// Use this if there isn't a subclass for the HTTP method you want to use
///
/// HTTP Method to use, e.g. "PATCH"
public RequestAttribute(string httpMethod)
: base(new HttpMethod(httpMethod))
{
}
///
/// Initialises a new instance of the class, with the given HttpMethod and relative path.
///
///
/// Use this if there isn't a subclass for the HTTP method you want to use
///
/// HTTP Method to use, e.g. "PATCH"
/// Relative path to use
public RequestAttribute(string httpMethod, string path)
: base(new HttpMethod(httpMethod), path)
{
}
}
///
/// Delete request
///
[AttributeUsage(AttributeTargets.Method, Inherited = false, AllowMultiple = false)]
public sealed class DeleteAttribute : RequestAttributeBase
{
///
/// Initialises a new instance of the class
///
public DeleteAttribute() : base(HttpMethod.Delete) { }
///
/// Initialises a new instance of the class, with the given relative path
///
/// Relative path to use
public DeleteAttribute(string path) : base(HttpMethod.Delete, path) { }
}
///
/// Get request
///
[AttributeUsage(AttributeTargets.Method, Inherited = false, AllowMultiple = false)]
public sealed class GetAttribute : RequestAttributeBase
{
///
/// Initialises a new instance of the class
///
public GetAttribute() : base(HttpMethod.Get) { }
///
/// Initialises a new instance of the class, with the given relative path
///
/// Relative path to use
public GetAttribute(string path) : base(HttpMethod.Get, path) { }
}
///
/// Head request
///
[AttributeUsage(AttributeTargets.Method, Inherited = false, AllowMultiple = false)]
public sealed class HeadAttribute : RequestAttributeBase
{
///
/// Initialises a new instance of the class
///
public HeadAttribute() : base(HttpMethod.Head) { }
///
/// Initialises a new instance of the class, with the given relative path
///
/// Relative path to use
public HeadAttribute(string path) : base(HttpMethod.Head, path) { }
}
///
/// Options request
///
[AttributeUsage(AttributeTargets.Method, Inherited = false, AllowMultiple = false)]
public sealed class OptionsAttribute : RequestAttributeBase
{
///
/// Initialises a new instance of the class
///
public OptionsAttribute() : base(HttpMethod.Options) { }
///
/// Initialises a new instance of the class, with the given relative path
///
/// Relative path to use
public OptionsAttribute(string path) : base(HttpMethod.Options, path) { }
}
///
/// Post request
///
[AttributeUsage(AttributeTargets.Method, Inherited = false, AllowMultiple = false)]
public sealed class PostAttribute : RequestAttributeBase
{
///
/// Initialises a new instance of the class
///
public PostAttribute() : base(HttpMethod.Post) { }
///
/// Initialises a new instance of the class, with the given relative path
///
/// Relative path to use
public PostAttribute(string path) : base(HttpMethod.Post, path) { }
}
///
/// Put request
///
[AttributeUsage(AttributeTargets.Method, Inherited = false, AllowMultiple = false)]
public sealed class PutAttribute : RequestAttributeBase
{
///
/// Initialises a new instance of the class
///
public PutAttribute() : base(HttpMethod.Put) { }
///
/// Initialises a new instance of the class, with the given relativ epath
///
/// Relative path to use
public PutAttribute(string path) : base(HttpMethod.Put, path) { }
}
///
/// Trace request
///
[AttributeUsage(AttributeTargets.Method, Inherited = false, AllowMultiple = false)]
public sealed class TraceAttribute : RequestAttributeBase
{
///
/// Initialises a new instance of the class
///
public TraceAttribute() : base(HttpMethod.Trace) { }
///
/// Initialises a new instance of the class, with the given relative path
///
/// Relative path to use
public TraceAttribute(string path) : base(HttpMethod.Trace, path) { }
}
///
/// Patch request
///
[AttributeUsage(AttributeTargets.Method, Inherited = false, AllowMultiple = false)]
public sealed class PatchAttribute : RequestAttributeBase
{
///
/// Gets a static instance of corresponding to a PATCH request
///
public static HttpMethod PatchMethod { get; } = new HttpMethod("PATCH");
///
/// Initialises a new instance of the class
///
public PatchAttribute() : base(PatchMethod) { }
///
/// Initialises a new instance of the class, with the given relativ epath
///
/// Relative path to use
public PatchAttribute(string path) : base(PatchMethod, path) { }
}
}