Skip to content

Commit 54d9e98

Browse files
committed
Merge pull request #2 from ikesyo/result-helper-function
Use helper functions for Result
2 parents 6057b2d + c3735c0 commit 54d9e98

6 files changed

+26
-40
lines changed

APIKit/APIKit.swift

+8-10
Original file line numberDiff line numberDiff line change
@@ -94,27 +94,25 @@ public class API {
9494
dispatch_async(mainQueue, { handler(.Failure(Box(error))) })
9595
return
9696
}
97-
98-
switch self.responseBodyParser().parseData(data) {
99-
case .Failure(let box):
100-
dispatch_async(mainQueue, { handler(.Failure(Box(box.unbox))) })
101-
102-
case .Success(let box):
103-
if let response = request.responseFromObject(box.unbox) {
104-
dispatch_async(mainQueue, { handler(.Success(Box(response))) })
97+
98+
let mappedResponse: Result<T.Response, NSError> = self.responseBodyParser().parseData(data).flatMap { rawResponse in
99+
if let response = request.responseFromObject(rawResponse) {
100+
return success(response)
105101
} else {
106102
let userInfo = [NSLocalizedDescriptionKey: "failed to create model object from raw object."]
107103
let error = NSError(domain: APIKitErrorDomain, code: 0, userInfo: userInfo)
108-
dispatch_async(mainQueue, { handler(.Failure(Box(error))) })
104+
return failure(error)
109105
}
106+
110107
}
108+
dispatch_async(mainQueue, { handler(mappedResponse) })
111109
}
112110

113111
task.resume()
114112
} else {
115113
let userInfo = [NSLocalizedDescriptionKey: "failed to build request."]
116114
let error = NSError(domain: APIKitErrorDomain, code: 0, userInfo: userInfo)
117-
dispatch_async(mainQueue, { handler(.Failure(Box(error))) })
115+
dispatch_async(mainQueue, { handler(failure(error)) })
118116
}
119117
}
120118
}

APIKit/RequestBodyBuilder.swift

+6-12
Original file line numberDiff line numberDiff line change
@@ -32,23 +32,17 @@ public enum RequestBodyBuilder {
3232
if !NSJSONSerialization.isValidJSONObject(object) {
3333
let userInfo = [NSLocalizedDescriptionKey: "invalid object for JSON passed."]
3434
let error = NSError(domain: APIKitRequestBodyBuidlerErrorDomain, code: 0, userInfo: userInfo)
35-
result = Result.Failure(Box(error))
35+
result = failure(error)
3636
break
3737
}
38-
39-
var error: NSError?
40-
if let data = NSJSONSerialization.dataWithJSONObject(object, options: writingOptions, error: &error) {
41-
result = Result.Success(Box(data))
42-
} else {
43-
result = Result.Failure(Box(error!))
38+
39+
result = try { error in
40+
return NSJSONSerialization.dataWithJSONObject(object, options: writingOptions, error: error)
4441
}
4542

4643
case .URL(let encoding):
47-
var error: NSError?
48-
if let data = URLEncodedSerialization.dataFromObject(object, encoding: encoding, error: &error) {
49-
result = Result.Success(Box(data))
50-
} else {
51-
result = Result.Failure(Box(error!))
44+
result = try { error in
45+
return URLEncodedSerialization.dataFromObject(object, encoding: encoding, error: error)
5246
}
5347

5448
case .Custom(let (_, buildBodyFromObject)):

APIKit/ResponseBodyParser.swift

+4-10
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,13 @@ public enum ResponseBodyParser {
2727

2828
switch self {
2929
case .JSON(let readingOptions):
30-
var error: NSError?
31-
if let object: AnyObject = NSJSONSerialization.JSONObjectWithData(data, options: readingOptions, error: &error) {
32-
result = Result.Success(Box(object))
33-
} else {
34-
result = Result.Failure(Box(error!))
30+
result = try { error in
31+
return NSJSONSerialization.JSONObjectWithData(data, options: readingOptions, error: error)
3532
}
3633

3734
case .URL(let encoding):
38-
var error: NSError?
39-
if let object: AnyObject = URLEncodedSerialization.objectFromData(data, encoding: encoding, error: &error) {
40-
result = Result.Success(Box(object))
41-
} else {
42-
result = Result.Failure(Box(error!))
35+
result = try { error in
36+
return URLEncodedSerialization.objectFromData(data, encoding: encoding, error: error)
4337
}
4438

4539
case .Custom(let (accept, parseData)):

APIKit/URLEncodedSerialization.swift

+4-4
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ private func unescape(string: String) -> String {
99
}
1010

1111
public class URLEncodedSerialization {
12-
public class func objectFromData(data: NSData, encoding: NSStringEncoding, inout error: NSError?) -> AnyObject? {
12+
public class func objectFromData(data: NSData, encoding: NSStringEncoding, error: NSErrorPointer) -> AnyObject? {
1313
var dictionary: [String: AnyObject]?
1414

1515
if let string = NSString(data: data, encoding: encoding) as? String {
@@ -26,19 +26,19 @@ public class URLEncodedSerialization {
2626

2727
if dictionary == nil {
2828
let userInfo = [NSLocalizedDescriptionKey: "failed to decode urlencoded string."]
29-
error = NSError(domain: APIKitErrorDomain, code: 0, userInfo: userInfo)
29+
error.memory = NSError(domain: APIKitErrorDomain, code: 0, userInfo: userInfo)
3030
}
3131

3232
return dictionary
3333
}
3434

35-
public class func dataFromObject(object: AnyObject, encoding: NSStringEncoding, inout error: NSError?) -> NSData? {
35+
public class func dataFromObject(object: AnyObject, encoding: NSStringEncoding, error: NSErrorPointer) -> NSData? {
3636
let string = stringFromObject(object, encoding: encoding)
3737
let data = string.dataUsingEncoding(encoding, allowLossyConversion: false)
3838

3939
if data == nil {
4040
let userInfo = [NSLocalizedDescriptionKey: "failed to decode urlencoded string."]
41-
error = NSError(domain: APIKitErrorDomain, code: 0, userInfo: userInfo)
41+
error.memory = NSError(domain: APIKitErrorDomain, code: 0, userInfo: userInfo)
4242
}
4343

4444
return data

APIKitTests/RequestBodyBuilderTests.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,15 @@ class RequestBodyBuilderTests: XCTestCase {
6060
}
6161

6262
func testCustomHeader() {
63-
let builder = RequestBodyBuilder.Custom(contentTypeHeader: "foo", buildBodyFromObject: { o in Result.Success(Box(o as NSData)) })
63+
let builder = RequestBodyBuilder.Custom(contentTypeHeader: "foo", buildBodyFromObject: { o in success(o as NSData) })
6464
XCTAssertEqual(builder.contentTypeHeader, "foo")
6565
}
6666

6767
func testCustomSuccess() {
6868
let string = "foo"
6969
let expectedData = string.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!
7070
let builder = RequestBodyBuilder.Custom(contentTypeHeader: "", buildBodyFromObject: { object in
71-
return Result.Success(Box(expectedData))
71+
return success(expectedData)
7272
})
7373

7474
switch builder.buildBodyFromObject(string) {

APIKitTests/ResponseBodyParserTests.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,15 @@ class ResponseBodyParserTests: XCTestCase {
6565
}
6666

6767
func testCustomAcceptHeader() {
68-
let parser = ResponseBodyParser.Custom(acceptHeader: "foo", parseData: { d in Result.Success(Box(d)) })
68+
let parser = ResponseBodyParser.Custom(acceptHeader: "foo", parseData: { d in success(d) })
6969
XCTAssertEqual(parser.acceptHeader, "foo")
7070
}
7171

7272
func testCustomSuccess() {
7373
let expectedDictionary = ["foo": 1]
7474
let data = NSData()
7575
let parser = ResponseBodyParser.Custom(acceptHeader: "", parseData: { data in
76-
return Result.Success(Box(expectedDictionary))
76+
return success(expectedDictionary)
7777
})
7878

7979
switch parser.parseData(data) {

0 commit comments

Comments
 (0)