Skip to content

Commit

Permalink
responseDecodable(decoding: Foo.self)
Browse files Browse the repository at this point in the history
Since otherwise type has to be inferred from the chain, which leads to weird compile errors.
  • Loading branch information
mxcl committed Mar 5, 2018
1 parent f206d31 commit 68ac9fc
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 13 deletions.
6 changes: 5 additions & 1 deletion PMKAlamofire.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@
isa = PBXProject;
attributes = {
LastSwiftUpdateCheck = 0800;
LastUpgradeCheck = 0900;
LastUpgradeCheck = 0930;
ORGANIZATIONNAME = "Max Howell";
TargetAttributes = {
63C7FFA61D5BEE09003BAE60 = {
Expand Down Expand Up @@ -240,13 +240,15 @@
CLANG_WARN_BOOL_CONVERSION = YES;
CLANG_WARN_COMMA = YES;
CLANG_WARN_CONSTANT_CONVERSION = YES;
CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES;
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_DOCUMENTATION_COMMENTS = YES;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
Expand Down Expand Up @@ -305,13 +307,15 @@
CLANG_WARN_BOOL_CONVERSION = YES;
CLANG_WARN_COMMA = YES;
CLANG_WARN_CONSTANT_CONVERSION = YES;
CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES;
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_DOCUMENTATION_COMMENTS = YES;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0900"
LastUpgradeVersion = "0930"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand All @@ -26,7 +26,6 @@
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
language = ""
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
<TestableReference
Expand Down Expand Up @@ -64,7 +63,6 @@
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
language = ""
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
Expand Down
24 changes: 24 additions & 0 deletions Sources/Alamofire+Promise.swift
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,30 @@ extension Alamofire.DataRequest {
}
}
}

/**
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<T: Decodable>(_ type: T.Type, queue: DispatchQueue? = nil, decoder: JSONDecoder = JSONDecoder()) -> Promise<T> {
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
}

Expand Down
38 changes: 29 additions & 9 deletions Tests/TestAlamofire.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,36 @@ class AlamofireTests: XCTestCase {
override func tearDown() {
OHHTTPStubs.removeAllStubs()
}
#if swift(>=3.2)
struct KeyValuesService: Decodable {

#if swift(>=3.2)
private struct Fixture: Decodable {
let key1: String
let key2: [String]
}

func getFeed() -> Promise<KeyValuesService> {
return Alamofire.request("http://example.com", method: .get).responseDecodable(queue: nil)
func testDecodable1() {

func getFixture() -> Promise<Fixture> {
return Alamofire.request("http://example.com", method: .get).responseDecodable(queue: nil)
}

let json: NSDictionary = ["key1": "value1", "key2": ["value2A", "value2B"]]

OHHTTPStubs.stubRequests(passingTest: { $0.url!.host == "example.com" }) { _ in
return OHHTTPStubsResponse(jsonObject: json, statusCode: 200, headers: nil)
}

let ex = expectation(description: "")

getFixture().done { fixture in
XCTAssert(fixture.key1 == "value1", "Value1 found")
ex.fulfill()
}
waitForExpectations(timeout: 1)

}

func testDecodable() {

func testDecodable2() {
let json: NSDictionary = ["key1": "value1", "key2": ["value2A", "value2B"]]

OHHTTPStubs.stubRequests(passingTest: { $0.url!.host == "example.com" }) { _ in
Expand All @@ -43,12 +61,14 @@ class AlamofireTests: XCTestCase {

let ex = expectation(description: "")

getFeed().done { keyValueService in
XCTAssert(keyValueService.key1=="value1", "Value1 found")
firstly {
Alamofire.request("http://example.com", method: .get).responseDecodable(Fixture.self)
}.done { fixture in
XCTAssert(fixture.key1 == "value1", "Value1 found")
ex.fulfill()
}
waitForExpectations(timeout: 1)

}
#endif
#endif
}

0 comments on commit 68ac9fc

Please sign in to comment.