diff --git a/Cartfile b/Cartfile old mode 100644 new mode 100755 index 14c7c12..caf0197 --- a/Cartfile +++ b/Cartfile @@ -1,2 +1,2 @@ -github "mxcl/PromiseKit" ~> 6.8.4 -github "Alamofire/Alamofire" ~> 4.0 +github "mxcl/PromiseKit" ~> 6.13.0 +github "Alamofire/Alamofire" ~> 5.2.1 diff --git a/Cartfile.private b/Cartfile.private old mode 100644 new mode 100755 index 7ad353e..203f6cf --- a/Cartfile.private +++ b/Cartfile.private @@ -1 +1 @@ -github "AliSoftware/OHHTTPStubs" ~> 7.0 +github "AliSoftware/OHHTTPStubs" ~> 8.0.0 diff --git a/Cartfile.resolved b/Cartfile.resolved index 10b37bd..7208e8d 100644 --- a/Cartfile.resolved +++ b/Cartfile.resolved @@ -1,3 +1,3 @@ -github "Alamofire/Alamofire" "4.8.2" -github "AliSoftware/OHHTTPStubs" "7.0.0" -github "mxcl/PromiseKit" "6.8.4" +github "Alamofire/Alamofire" "5.2.1" +github "AliSoftware/OHHTTPStubs" "8.0.0" +github "mxcl/PromiseKit" "6.13.1" diff --git a/PMKAlamofire.xcodeproj/project.pbxproj b/PMKAlamofire.xcodeproj/project.pbxproj old mode 100644 new mode 100755 index 45ae8f7..ce5c702 --- a/PMKAlamofire.xcodeproj/project.pbxproj +++ b/PMKAlamofire.xcodeproj/project.pbxproj @@ -279,7 +279,7 @@ GCC_WARN_UNUSED_FUNCTION = YES; GCC_WARN_UNUSED_VARIABLE = YES; INFOPLIST_FILE = Info.plist; - IPHONEOS_DEPLOYMENT_TARGET = 8.0; + IPHONEOS_DEPLOYMENT_TARGET = 10.0; MACOSX_DEPLOYMENT_TARGET = 10.11; MTL_ENABLE_DEBUG_INFO = YES; ONLY_ACTIVE_ARCH = YES; @@ -287,7 +287,7 @@ SUPPORTED_PLATFORMS = "iphonesimulator iphoneos watchsimulator watchos macosx appletvsimulator appletvos"; SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG; SWIFT_OPTIMIZATION_LEVEL = "-Onone"; - SWIFT_VERSION = 4.0; + SWIFT_VERSION = 5.0; TARGETED_DEVICE_FAMILY = "1,2,3,4"; TVOS_DEPLOYMENT_TARGET = 9.0; VERSIONING_SYSTEM = "apple-generic"; @@ -342,13 +342,13 @@ GCC_WARN_UNUSED_FUNCTION = YES; GCC_WARN_UNUSED_VARIABLE = YES; INFOPLIST_FILE = Info.plist; - IPHONEOS_DEPLOYMENT_TARGET = 8.0; + IPHONEOS_DEPLOYMENT_TARGET = 10.0; MACOSX_DEPLOYMENT_TARGET = 10.11; MTL_ENABLE_DEBUG_INFO = NO; PRODUCT_BUNDLE_IDENTIFIER = org.promisekit.Foundation; SUPPORTED_PLATFORMS = "iphonesimulator iphoneos watchsimulator watchos macosx appletvsimulator appletvos"; SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule"; - SWIFT_VERSION = 4.0; + SWIFT_VERSION = 5.0; TARGETED_DEVICE_FAMILY = "1,2,3,4"; TVOS_DEPLOYMENT_TARGET = 9.0; VALIDATE_PRODUCT = YES; @@ -364,6 +364,7 @@ APPLICATION_EXTENSION_API_ONLY = YES; CLANG_ENABLE_MODULES = YES; CODE_SIGN_IDENTITY = ""; + CURRENT_PROJECT_VERSION = 1.0.7; DEFINES_MODULE = YES; DYLIB_COMPATIBILITY_VERSION = 1; DYLIB_CURRENT_VERSION = 1; @@ -373,6 +374,7 @@ PRODUCT_NAME = "$(TARGET_NAME)"; SWIFT_OPTIMIZATION_LEVEL = "-Onone"; SWIFT_TREAT_WARNINGS_AS_ERRORS = NO; + SWIFT_VERSION = 5.0; }; name = Debug; }; @@ -382,6 +384,7 @@ APPLICATION_EXTENSION_API_ONLY = YES; CLANG_ENABLE_MODULES = YES; CODE_SIGN_IDENTITY = ""; + CURRENT_PROJECT_VERSION = 1.0.7; DEFINES_MODULE = YES; DYLIB_COMPATIBILITY_VERSION = 1; DYLIB_CURRENT_VERSION = 1; @@ -390,6 +393,7 @@ PRODUCT_MODULE_NAME = "${TARGET_NAME}"; PRODUCT_NAME = "$(TARGET_NAME)"; SWIFT_TREAT_WARNINGS_AS_ERRORS = NO; + SWIFT_VERSION = 5.0; }; name = Release; }; diff --git a/Package.swift b/Package.swift index 2e0d331..9dd8471 100644 --- a/Package.swift +++ b/Package.swift @@ -1,10 +1,24 @@ +// swift-tools-version:5.1 + import PackageDescription let package = Package( - name: "PMKAlamofire", - dependencies: [ - .Package(url: "https://github.com/mxcl/PromiseKit.git", majorVersion: 6), - .Package(url: "https://github.com/Alamofire/Alamofire.git", majorVersion: 4) - ], - exclude: ["Tests"] -) + name: "PMKAlamofire", + platforms: [.macOS(.v10_12), + .iOS(.v10), + .tvOS(.v10), + .watchOS(.v3)], + products: [ + .library(name: "PMKAlamofire", targets: ["PMKAlamofire"]) + ], + dependencies: [ + .package(url: "https://github.com/mxcl/PromiseKit.git", from: "6.13.2"), + .package(url: "https://github.com/Alamofire/Alamofire.git", from: "5.2.2") + ], + targets: [ + .target(name: "PMKAlamofire", dependencies: [ + .product(name: "PromiseKit"), + .product(name: "Alamofire"), + ], path: "Sources") + ], + swiftLanguageVersions: [.v5]) \ No newline at end of file diff --git a/Package@swift-4.2.swift b/Package@swift-4.2.swift deleted file mode 100644 index d6ab721..0000000 --- a/Package@swift-4.2.swift +++ /dev/null @@ -1,23 +0,0 @@ -// swift-tools-version:4.2 - -import PackageDescription - -let pkg = Package(name: "PMKAlamofire") -pkg.products = [ - .library(name: "PMKAlamofire", targets: ["PMKAlamofire"]), -] -pkg.dependencies = [ - .package(url: "https://github.com/Alamofire/Alamofire.git", .upToNextMajor(from: "4.0.0")), - .package(url: "https://github.com/mxcl/PromiseKit.git", .upToNextMajor(from: "6.0.0")) -] -pkg.swiftLanguageVersions = [.v3, .v4, .v4_2] - -let target: Target = .target(name: "PMKAlamofire") -target.path = "Sources" -target.exclude = ["Tests"] -target.dependencies = [ - "PromiseKit", - "Alamofire" -] - -pkg.targets = [target] diff --git a/README.markdown b/README.markdown index ced03e0..8bc756c 100644 --- a/README.markdown +++ b/README.markdown @@ -2,8 +2,6 @@ This project adds promises to [Alamofire](https://github.com/Alamofire/Alamofire). -This project supports Swift 3.1, 3.2, 4.0 and 4.1. - ## Usage ```swift diff --git a/Sources/Alamofire+Promise.swift b/Sources/Alamofire+Promise.swift old mode 100644 new mode 100755 index f5f7b59..528a9b6 --- a/Sources/Alamofire+Promise.swift +++ b/Sources/Alamofire+Promise.swift @@ -1,189 +1,176 @@ -@_exported import Alamofire import Foundation #if !PMKCocoaPods +import Alamofire import PromiseKit #endif /** To import the `Alamofire` category: - use_frameworks! - pod "PromiseKit/Alamofire" + use_frameworks! + pod "PromiseKit/Alamofire" And then in your sources: - import PromiseKit + import PromiseKit */ extension Alamofire.DataRequest { - /// Adds a handler to be called once the request has finished. - public func response(_: PMKNamespacer, queue: DispatchQueue? = nil) -> Promise<(URLRequest, HTTPURLResponse, Data)> { - return Promise { seal in - response(queue: queue) { rsp in - if let error = rsp.error { - seal.reject(error) - } else if let a = rsp.request, let b = rsp.response, let c = rsp.data { - seal.fulfill((a, b, c)) - } else { - seal.reject(PMKError.invalidCallingConvention) - } - } - } - } - - /// Adds a handler to be called once the request has finished. - public func responseData(queue: DispatchQueue? = nil) -> Promise<(data: Data, response: PMKAlamofireDataResponse)> { - return Promise { seal in - responseData(queue: queue) { response in - switch response.result { - case .success(let value): - seal.fulfill((value, PMKAlamofireDataResponse(response))) - case .failure(let error): - seal.reject(error) - } - } - } - } - - /// Adds a handler to be called once the request has finished. - public func responseString(queue: DispatchQueue? = nil) -> Promise<(string: String, response: PMKAlamofireDataResponse)> { - return Promise { seal in - responseString(queue: queue) { response in - switch response.result { - case .success(let value): - seal.fulfill((value, PMKAlamofireDataResponse(response))) - case .failure(let error): - seal.reject(error) - } - } - } - } - - /// Adds a handler to be called once the request has finished. - public func responseJSON(queue: DispatchQueue? = nil, options: JSONSerialization.ReadingOptions = .allowFragments) -> Promise<(json: Any, response: PMKAlamofireDataResponse)> { - return Promise { seal in - responseJSON(queue: queue, options: options) { response in - switch response.result { - case .success(let value): - seal.fulfill((value, PMKAlamofireDataResponse(response))) - case .failure(let error): - seal.reject(error) - } - } - } - } - - /// Adds a handler to be called once the request has finished. - public func responsePropertyList(queue: DispatchQueue? = nil, options: PropertyListSerialization.ReadOptions = PropertyListSerialization.ReadOptions()) -> Promise<(plist: Any, response: PMKAlamofireDataResponse)> { - return Promise { seal in - responsePropertyList(queue: queue, options: options) { response in - switch response.result { - case .success(let value): - seal.fulfill((value, PMKAlamofireDataResponse(response))) - case .failure(let error): - seal.reject(error) - } - } - } - } + /// Adds a handler to be called once the request has finished. + public func response(_: PMKNamespacer, queue: DispatchQueue = .main) -> Promise<(URLRequest, HTTPURLResponse, Data)> { + return Promise { seal in + response(queue: queue) { rsp in + if let error = rsp.error { + seal.reject(error) + } else if let a = rsp.request, let b = rsp.response, let c = rsp.data { + seal.fulfill((a, b, c)) + } else { + seal.reject(PMKError.invalidCallingConvention) + } + } + } + } + + /// Adds a handler to be called once the request has finished. + public func responseData(queue: DispatchQueue = .main) -> Promise<(data: Data, response: PMKAlamofireDataResponse)> { + return Promise { seal in + responseData(queue: queue) { response in + switch response.result { + case .success(let value): + seal.fulfill((value, PMKAlamofireDataResponse(response))) + case .failure(let error): + seal.reject(error) + } + } + } + } + + /// Adds a handler to be called once the request has finished. + public func responseString(queue: DispatchQueue = .main) -> Promise<(string: String, response: PMKAlamofireDataResponse)> { + return Promise { seal in + responseString(queue: queue) { response in + switch response.result { + case .success(let value): + seal.fulfill((value, PMKAlamofireDataResponse(response))) + case .failure(let error): + seal.reject(error) + } + } + } + } + + /// Adds a handler to be called once the request has finished. + public func responseJSON(queue: DispatchQueue = .main, options: JSONSerialization.ReadingOptions = .allowFragments) -> Promise<(json: Any, response: PMKAlamofireDataResponse)> { + return Promise { seal in + responseData(queue: queue) { response in + switch response.result { + case .success(let dataValue): + do { + let json = try JSONSerialization.jsonObject(with: dataValue, options: options) + seal.fulfill((json, PMKAlamofireDataResponse(response))) + } catch { + seal.reject(error) + } + case .failure(let error): + seal.reject(error) + } + } + } + } #if swift(>=3.2) - /** - Returns a Promise for a Decodable - Adds a handler to be called once the request has finished. - - - Parameter queue: DispatchQueue, by default nil - - Parameter decoder: JSONDecoder, by default JSONDecoder() - */ - public func responseDecodable(queue: DispatchQueue? = nil, decoder: JSONDecoder = JSONDecoder()) -> Promise { - return Promise { seal in - responseData(queue: queue) { response in - switch response.result { - case .success(let value): - do { - seal.fulfill(try decoder.decode(T.self, from: value)) - } catch { - seal.reject(error) - } - case .failure(let error): - seal.reject(error) - } - } - } - } + /** + Returns a Promise for a Decodable + Adds a handler to be called once the request has finished. + + - Parameter queue: DispatchQueue, by default nil + - Parameter decoder: JSONDecoder, by default JSONDecoder() + */ + public func responseDecodable(queue: DispatchQueue = .main, decoder: JSONDecoder = JSONDecoder()) -> Promise { + return Promise { seal in + responseData(queue: queue) { response in + switch response.result { + case .success(let value): + do { + seal.fulfill(try decoder.decode(T.self, from: value)) + } catch { + seal.reject(error) + } + case .failure(let error): + seal.reject(error) + } + } + } + } - /** - Returns a Promise for a Decodable - Adds a handler to be called once the request has finished. - - - Parameter queue: DispatchQueue, by default nil - - Parameter decoder: JSONDecoder, by default JSONDecoder() - */ - public func responseDecodable(_ type: T.Type, queue: DispatchQueue? = nil, decoder: JSONDecoder = JSONDecoder()) -> Promise { - return Promise { seal in - responseData(queue: queue) { response in - switch response.result { - case .success(let value): - do { - seal.fulfill(try decoder.decode(type, from: value)) - } catch { - seal.reject(error) - } - case .failure(let error): - seal.reject(error) - } - } - } - } + /** + Returns a Promise for a Decodable + Adds a handler to be called once the request has finished. + + - Parameter queue: DispatchQueue, by default nil + - Parameter decoder: JSONDecoder, by default JSONDecoder() + */ + public func responseDecodable(_ type: T.Type, queue: DispatchQueue = .main, decoder: JSONDecoder = JSONDecoder()) -> Promise { + return Promise { seal in + responseData(queue: queue) { response in + switch response.result { + case .success(let value): + do { + seal.fulfill(try decoder.decode(type, from: value)) + } catch { + seal.reject(error) + } + case .failure(let error): + seal.reject(error) + } + } + } + } #endif } extension Alamofire.DownloadRequest { - public func response(_: PMKNamespacer, queue: DispatchQueue? = nil) -> Promise { - return Promise { seal in - response(queue: queue) { response in - if let error = response.error { - seal.reject(error) - } else { - seal.fulfill(response) - } - } - } - } - - /// Adds a handler to be called once the request has finished. - public func responseData(queue: DispatchQueue? = nil) -> Promise> { - return Promise { seal in - responseData(queue: queue) { response in - switch response.result { - case .success: - seal.fulfill(response) - case .failure(let error): - seal.reject(error) - } - } - } - } + public func response(_: PMKNamespacer, queue: DispatchQueue = .main) -> Promise> { + return Promise { seal in + response(queue: queue) { response in + if let error = response.error { + seal.reject(error) + } else { + seal.fulfill(response) + } + } + } + } + + /// Adds a handler to be called once the request has finished. + public func responseData(queue: DispatchQueue = .main) -> Promise> { + return Promise { seal in + responseData(queue: queue) { response in + switch response.result { + case .success: + seal.fulfill(response) + case .failure(let error): + seal.reject(error) + } + } + } + } } /// Alamofire.DataResponse, but without the `result`, since the Promise represents the `Result` public struct PMKAlamofireDataResponse { - public init(_ rawrsp: Alamofire.DataResponse) { - request = rawrsp.request - response = rawrsp.response - data = rawrsp.data - timeline = rawrsp.timeline - } - - /// The URL request sent to the server. - public let request: URLRequest? + public init(_ rawrsp: Alamofire.DataResponse) { + request = rawrsp.request + response = rawrsp.response + data = rawrsp.data + } - /// The server's response to the URL request. - public let response: HTTPURLResponse? + /// The URL request sent to the server. + public let request: URLRequest? - /// The data returned by the server. - public let data: Data? + /// The server's response to the URL request. + public let response: HTTPURLResponse? - /// The timeline of the complete lifecycle of the request. - public let timeline: Timeline + /// The data returned by the server. + public let data: Data? } diff --git a/Tests/TestAlamofire.swift b/Tests/TestAlamofire.swift index 043ee8a..62f4536 100644 --- a/Tests/TestAlamofire.swift +++ b/Tests/TestAlamofire.swift @@ -13,7 +13,7 @@ class AlamofireTests: XCTestCase { let ex = expectation(description: "") - let rq = Alamofire.request("http://example.com", method: .get).responseJSON().done { rsp in + let rq = AF.request("http://example.com", method: .get).responseJSON().done { rsp in XCTAssertEqual(json, rsp.json as? NSDictionary) ex.fulfill() } @@ -33,7 +33,7 @@ class AlamofireTests: XCTestCase { func testDecodable1() { func getFixture() -> Promise { - return Alamofire.request("http://example.com", method: .get).responseDecodable(queue: nil) + return AF.request("http://example.com", method: .get).responseDecodable() } let json: NSDictionary = ["key1": "value1", "key2": ["value2A", "value2B"]] @@ -62,7 +62,7 @@ class AlamofireTests: XCTestCase { let ex = expectation(description: "") firstly { - Alamofire.request("http://example.com", method: .get).responseDecodable(Fixture.self) + AF.request("http://example.com", method: .get).responseDecodable(Fixture.self) }.done { fixture in XCTAssert(fixture.key1 == "value1", "Value1 found") ex.fulfill()