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