/
RequestMethod.cs
164 lines (149 loc) · 5.96 KB
/
RequestMethod.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
namespace Azure.Core
{
/// <summary>
/// Represents HTTP methods sent as part of a <see cref="Request"/>.
/// </summary>
public readonly struct RequestMethod : IEquatable<RequestMethod>
{
/// <summary>
/// Gets the HTTP method.
/// </summary>
public string Method { get; }
/// <summary>
/// Gets <see cref="RequestMethod"/> instance for GET method.
/// </summary>
public static RequestMethod Get { get; } = new RequestMethod("GET");
/// <summary>
/// Gets <see cref="RequestMethod"/> instance for POST method.
/// </summary>
public static RequestMethod Post { get; } = new RequestMethod("POST");
/// <summary>
/// Gets <see cref="RequestMethod"/> instance for PUT method.
/// </summary>
public static RequestMethod Put { get; } = new RequestMethod("PUT");
/// <summary>
/// Gets <see cref="RequestMethod"/> instance for PATCH method.
/// </summary>
public static RequestMethod Patch { get; } = new RequestMethod("PATCH");
/// <summary>
/// Gets <see cref="RequestMethod"/> instance for DELETE method.
/// </summary>
public static RequestMethod Delete { get; } = new RequestMethod("DELETE");
/// <summary>
/// Gets <see cref="RequestMethod"/> instance for HEAD method.
/// </summary>
public static RequestMethod Head { get; } = new RequestMethod("HEAD");
/// <summary>
/// Gets <see cref="RequestMethod"/> instance for OPTIONS method.
/// </summary>
public static RequestMethod Options { get; } = new RequestMethod("OPTIONS");
/// <summary>
/// Gets <see cref="RequestMethod"/> instance for TRACE method.
/// </summary>
public static RequestMethod Trace { get; } = new RequestMethod("TRACE");
/// <summary>
/// Creates an instance of <see cref="RequestMethod"/> with provided method. Method must be all uppercase.
/// Prefer <see cref="Parse"/> if <paramref name="method"/> can be one of predefined method names.
/// </summary>
/// <param name="method">The method to use.</param>
public RequestMethod(string method)
{
Argument.AssertNotNull(method, nameof(method));
Method = method.ToUpperInvariant();
}
/// <summary>
/// Parses string to it's <see cref="RequestMethod"/> representation.
/// </summary>
/// <param name="method">The method string to parse.</param>
public static RequestMethod Parse(string method)
{
Argument.AssertNotNull(method, nameof(method));
// Fast-path common values
if (method.Length == 3)
{
if (string.Equals(method, "GET", StringComparison.OrdinalIgnoreCase))
{
return Get;
}
if (string.Equals(method, "PUT", StringComparison.OrdinalIgnoreCase))
{
return Put;
}
}
else if (method.Length == 4)
{
if (string.Equals(method, "POST", StringComparison.OrdinalIgnoreCase))
{
return Post;
}
if (string.Equals(method, "HEAD", StringComparison.OrdinalIgnoreCase))
{
return Head;
}
}
else
{
if (string.Equals(method, "PATCH", StringComparison.OrdinalIgnoreCase))
{
return Patch;
}
if (string.Equals(method, "DELETE", StringComparison.OrdinalIgnoreCase))
{
return Delete;
}
if (string.Equals(method, "OPTIONS", StringComparison.OrdinalIgnoreCase))
{
return Options;
}
if (string.Equals(method, "TRACE", StringComparison.OrdinalIgnoreCase))
{
return Trace;
}
}
return new RequestMethod(method);
}
/// <inheritdoc />
public bool Equals(RequestMethod other)
{
return string.Equals(Method, other.Method, StringComparison.Ordinal);
}
/// <inheritdoc />
public override bool Equals(object? obj)
{
return obj is RequestMethod other && Equals(other);
}
/// <inheritdoc />
public override int GetHashCode()
{
return Method.GetHashCodeOrdinal();
}
/// <summary>
/// Compares equality of two <see cref="RequestMethod"/> instances.
/// </summary>
/// <param name="left">The method to compare.</param>
/// <param name="right">The method to compare against.</param>
/// <returns><c>true</c> if <see cref="Method"/> values are equal for <paramref name="left"/> and <paramref name="right"/>, otherwise <c>false</c>.</returns>
public static bool operator ==(RequestMethod left, RequestMethod right)
{
return left.Equals(right);
}
/// <summary>
/// Compares inequality of two <see cref="RequestMethod"/> instances.
/// </summary>
/// <param name="left">The method to compare.</param>
/// <param name="right">The method to compare against.</param>
/// <returns><c>true</c> if <see cref="Method"/> values are equal for <paramref name="left"/> and <paramref name="right"/>, otherwise <c>false</c>.</returns>
public static bool operator !=(RequestMethod left, RequestMethod right)
{
return !left.Equals(right);
}
/// <inheritdoc />
public override string ToString()
{
return Method ?? "<null>";
}
}
}