Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use helper functions for Result #2

Merged
merged 2 commits into from
Feb 23, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 8 additions & 10 deletions APIKit/APIKit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -94,27 +94,25 @@ public class API {
dispatch_async(mainQueue, { handler(.Failure(Box(error))) })
return
}

switch self.responseBodyParser().parseData(data) {
case .Failure(let box):
dispatch_async(mainQueue, { handler(.Failure(Box(box.unbox))) })

case .Success(let box):
if let response = request.responseFromObject(box.unbox) {
dispatch_async(mainQueue, { handler(.Success(Box(response))) })

let mappedResponse: Result<T.Response, NSError> = self.responseBodyParser().parseData(data).flatMap { rawResponse in
if let response = request.responseFromObject(rawResponse) {
return success(response)
} else {
let userInfo = [NSLocalizedDescriptionKey: "failed to create model object from raw object."]
let error = NSError(domain: APIKitErrorDomain, code: 0, userInfo: userInfo)
dispatch_async(mainQueue, { handler(.Failure(Box(error))) })
return failure(error)
}

}
dispatch_async(mainQueue, { handler(mappedResponse) })
}

task.resume()
} else {
let userInfo = [NSLocalizedDescriptionKey: "failed to build request."]
let error = NSError(domain: APIKitErrorDomain, code: 0, userInfo: userInfo)
dispatch_async(mainQueue, { handler(.Failure(Box(error))) })
dispatch_async(mainQueue, { handler(failure(error)) })
}
}
}
18 changes: 6 additions & 12 deletions APIKit/RequestBodyBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,17 @@ public enum RequestBodyBuilder {
if !NSJSONSerialization.isValidJSONObject(object) {
let userInfo = [NSLocalizedDescriptionKey: "invalid object for JSON passed."]
let error = NSError(domain: APIKitRequestBodyBuidlerErrorDomain, code: 0, userInfo: userInfo)
result = Result.Failure(Box(error))
result = failure(error)
break
}

var error: NSError?
if let data = NSJSONSerialization.dataWithJSONObject(object, options: writingOptions, error: &error) {
result = Result.Success(Box(data))
} else {
result = Result.Failure(Box(error!))

result = try { error in
return NSJSONSerialization.dataWithJSONObject(object, options: writingOptions, error: error)
}

case .URL(let encoding):
var error: NSError?
if let data = URLEncodedSerialization.dataFromObject(object, encoding: encoding, error: &error) {
result = Result.Success(Box(data))
} else {
result = Result.Failure(Box(error!))
result = try { error in
return URLEncodedSerialization.dataFromObject(object, encoding: encoding, error: error)
}

case .Custom(let (_, buildBodyFromObject)):
Expand Down
14 changes: 4 additions & 10 deletions APIKit/ResponseBodyParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,13 @@ public enum ResponseBodyParser {

switch self {
case .JSON(let readingOptions):
var error: NSError?
if let object: AnyObject = NSJSONSerialization.JSONObjectWithData(data, options: readingOptions, error: &error) {
result = Result.Success(Box(object))
} else {
result = Result.Failure(Box(error!))
result = try { error in
return NSJSONSerialization.JSONObjectWithData(data, options: readingOptions, error: error)
}

case .URL(let encoding):
var error: NSError?
if let object: AnyObject = URLEncodedSerialization.objectFromData(data, encoding: encoding, error: &error) {
result = Result.Success(Box(object))
} else {
result = Result.Failure(Box(error!))
result = try { error in
return URLEncodedSerialization.objectFromData(data, encoding: encoding, error: error)
}

case .Custom(let (accept, parseData)):
Expand Down
8 changes: 4 additions & 4 deletions APIKit/URLEncodedSerialization.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ private func unescape(string: String) -> String {
}

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

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

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

return dictionary
}

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

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

return data
Expand Down
4 changes: 2 additions & 2 deletions APIKitTests/RequestBodyBuilderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,15 @@ class RequestBodyBuilderTests: XCTestCase {
}

func testCustomHeader() {
let builder = RequestBodyBuilder.Custom(contentTypeHeader: "foo", buildBodyFromObject: { o in Result.Success(Box(o as NSData)) })
let builder = RequestBodyBuilder.Custom(contentTypeHeader: "foo", buildBodyFromObject: { o in success(o as NSData) })
XCTAssertEqual(builder.contentTypeHeader, "foo")
}

func testCustomSuccess() {
let string = "foo"
let expectedData = string.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!
let builder = RequestBodyBuilder.Custom(contentTypeHeader: "", buildBodyFromObject: { object in
return Result.Success(Box(expectedData))
return success(expectedData)
})

switch builder.buildBodyFromObject(string) {
Expand Down
4 changes: 2 additions & 2 deletions APIKitTests/ResponseBodyParserTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,15 @@ class ResponseBodyParserTests: XCTestCase {
}

func testCustomAcceptHeader() {
let parser = ResponseBodyParser.Custom(acceptHeader: "foo", parseData: { d in Result.Success(Box(d)) })
let parser = ResponseBodyParser.Custom(acceptHeader: "foo", parseData: { d in success(d) })
XCTAssertEqual(parser.acceptHeader, "foo")
}

func testCustomSuccess() {
let expectedDictionary = ["foo": 1]
let data = NSData()
let parser = ResponseBodyParser.Custom(acceptHeader: "", parseData: { data in
return Result.Success(Box(expectedDictionary))
return success(expectedDictionary)
})

switch parser.parseData(data) {
Expand Down