Explorar o código

Adding HTTPBinAPIEndpoint enum for routing

Mattt Thompson %!s(int64=11) %!d(string=hai) anos
pai
achega
9b247cd990

+ 44 - 0
Example/HTTPBin.swift

@@ -0,0 +1,44 @@
+// MasterViewController.swift
+//
+// Copyright (c) 2014 Alamofire (http://alamofire.org)
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+// THE SOFTWARE.
+
+import Foundation
+import Alamofire
+
+enum HTTPBinRoute: URLStringConvertible {
+    case Method(Alamofire.Method)
+    case BasicAuth(String, String)
+
+    var URLString: String {
+        let baseURLString = "http://httpbin.org/"
+        let path: String = {
+            switch self {
+            case .Method(let method):
+                return "/\(method.toRaw().lowercaseString)"
+            case .BasicAuth(let user, let password):
+                return "/basic-auth/\(user)/\(password)"
+            }
+        }()
+
+        return baseURLString.stringByAppendingPathComponent(path)
+    }
+}
+

+ 4 - 8
Example/MasterViewController.swift

@@ -57,14 +57,10 @@ class MasterViewController: UITableViewController {
         if let detailViewController = segue.destinationViewController.topViewController as? DetailViewController {
             func requestForSegue(segue: UIStoryboardSegue) -> Request? {
                 switch segue.identifier {
-                    case "GET":
-                        return Alamofire.request(.GET, "http://httpbin.org/get")
-                    case "POST":
-                        return Alamofire.request(.POST, "http://httpbin.org/post")
-                    case "PUT":
-                        return Alamofire.request(.PUT, "http://httpbin.org/put")
-                    case "DELETE":
-                        return Alamofire.request(.DELETE, "http://httpbin.org/delete")
+                    case "GET", "POST", "PUT", "DELETE":
+                        let method = Alamofire.Method.fromRaw(segue.identifier)!
+
+                        return Alamofire.request(method, HTTPBinRoute.Method(method))
                     default:
                         return nil
                 }

+ 4 - 0
iOS Example.xcodeproj/project.pbxproj

@@ -14,6 +14,7 @@
 		F8111E1419A951050040E7D1 /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = F8111E1319A951050040E7D1 /* Images.xcassets */; };
 		F818D0E419CA8CFA006034B1 /* Alamofire.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = F8111E5419A95D7C0040E7D1 /* Alamofire.framework */; };
 		F818D0E619CA8D25006034B1 /* Alamofire.framework in Copy Frameworks */ = {isa = PBXBuildFile; fileRef = F8111E5419A95D7C0040E7D1 /* Alamofire.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
+		F8E6024019CB3C0700A3E7F1 /* HTTPBin.swift in Sources */ = {isa = PBXBuildFile; fileRef = F8E6023F19CB3C0700A3E7F1 /* HTTPBin.swift */; };
 /* End PBXBuildFile section */
 
 /* Begin PBXContainerItemProxy section */
@@ -63,6 +64,7 @@
 		F8111E1119A951050040E7D1 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = "<group>"; };
 		F8111E1319A951050040E7D1 /* Images.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Images.xcassets; sourceTree = "<group>"; };
 		F8111E4E19A95D7C0040E7D1 /* Alamofire.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; path = Alamofire.xcodeproj; sourceTree = "<group>"; };
+		F8E6023F19CB3C0700A3E7F1 /* HTTPBin.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = HTTPBin.swift; sourceTree = "<group>"; };
 /* End PBXFileReference section */
 
 /* Begin PBXFrameworksBuildPhase section */
@@ -100,6 +102,7 @@
 				F8111E0A19A951050040E7D1 /* AppDelegate.swift */,
 				F8111E0C19A951050040E7D1 /* MasterViewController.swift */,
 				F8111E0E19A951050040E7D1 /* DetailViewController.swift */,
+				F8E6023F19CB3C0700A3E7F1 /* HTTPBin.swift */,
 				F8111E1019A951050040E7D1 /* Main.storyboard */,
 				F8111E1319A951050040E7D1 /* Images.xcassets */,
 				F8111E0819A951050040E7D1 /* Supporting Files */,
@@ -220,6 +223,7 @@
 			buildActionMask = 2147483647;
 			files = (
 				F8111E0F19A951050040E7D1 /* DetailViewController.swift in Sources */,
+				F8E6024019CB3C0700A3E7F1 /* HTTPBin.swift in Sources */,
 				F8111E0D19A951050040E7D1 /* MasterViewController.swift in Sources */,
 				F8111E0B19A951050040E7D1 /* AppDelegate.swift in Sources */,
 			);