Просмотр исходного кода

Unfreeze HTTPMethod (#2901)

* Make HTTPMethod a struct.

* Make HTTPMethod case-sensitive.
Jon Shier 6 лет назад
Родитель
Сommit
45a1d70400
2 измененных файлов с 19 добавлено и 12 удалено
  1. 18 11
      Source/HTTPMethod.swift
  2. 1 1
      Source/ParameterEncoder.swift

+ 18 - 11
Source/HTTPMethod.swift

@@ -22,26 +22,33 @@
 //  THE SOFTWARE.
 //
 
-/// HTTP method definitions.
+/// Type representing HTTP methods. Raw `String` value is stored and compared case-sensitively, so
+/// `HTTPMethod.get != HTTPMethod(rawValue: "get")`.
 ///
 /// See https://tools.ietf.org/html/rfc7231#section-4.3
-public enum HTTPMethod: String {
+public struct HTTPMethod: RawRepresentable, Equatable, Hashable {
     /// `CONNECT` method.
-    case connect = "CONNECT"
+    public static let connect = HTTPMethod(rawValue: "CONNECT")
     /// `DELETE` method.
-    case delete  = "DELETE"
+    public static let delete = HTTPMethod(rawValue: "DELETE")
     /// `GET` method.
-    case get     = "GET"
+    public static let get = HTTPMethod(rawValue: "GET")
     /// `HEAD` method.
-    case head    = "HEAD"
+    public static let head = HTTPMethod(rawValue: "HEAD")
     /// `OPTIONS` method.
-    case options = "OPTIONS"
+    public static let options = HTTPMethod(rawValue: "OPTIONS")
     /// `PATCH` method.
-    case patch   = "PATCH"
+    public static let patch = HTTPMethod(rawValue: "PATCH")
     /// `POST` method.
-    case post    = "POST"
+    public static let post = HTTPMethod(rawValue: "POST")
     /// `PUT` method.
-    case put     = "PUT"
+    public static let put = HTTPMethod(rawValue: "PUT")
     /// `TRACE` method.
-    case trace   = "TRACE"
+    public static let trace = HTTPMethod(rawValue: "TRACE")
+
+    public let rawValue: String
+
+    public init(rawValue: String) {
+        self.rawValue = rawValue
+    }
 }

+ 1 - 1
Source/ParameterEncoder.swift

@@ -153,7 +153,7 @@ open class URLEncodedFormParameterEncoder: ParameterEncoder {
             throw AFError.parameterEncoderFailed(reason: .missingRequiredComponent(.url))
         }
 
-        guard let rawMethod = request.httpMethod, let method = HTTPMethod(rawValue: rawMethod) else {
+        guard let method = request.method else {
             let rawValue = request.httpMethod ?? "nil"
             throw AFError.parameterEncoderFailed(reason: .missingRequiredComponent(.httpMethod(rawValue: rawValue)))
         }