Skip to content

Commit

Permalink
Merge pull request #33 from ishkawa/goodbye-LlamaKit
Browse files Browse the repository at this point in the history
Goodbye LlamaKit
  • Loading branch information
ishkawa committed May 6, 2015
2 parents d8e4113 + 337cdcc commit 210223b
Show file tree
Hide file tree
Showing 16 changed files with 105 additions and 90 deletions.
3 changes: 0 additions & 3 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
[submodule "Carthage/Checkouts/LlamaKit"]
path = Carthage/Checkouts/LlamaKit
url = https://github.com/LlamaKit/LlamaKit.git
[submodule "Carthage/Checkouts/Assertions"]
path = Carthage/Checkouts/Assertions
url = https://github.com/antitypical/Assertions.git
Expand Down
4 changes: 2 additions & 2 deletions APIKit.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "APIKit"
s.version = "0.5.1"
s.version = "0.8.0"
s.summary = "A networking library for building type safe web API client in Swift."
s.homepage = "https://github.com/ishkawa/APIKit"

Expand All @@ -27,5 +27,5 @@ Pod::Spec.new do |s|
LICENSE
}

s.dependency "LlamaKit", "~> 0.6.0"
s.dependency "Result", "~> 0.4"
end
60 changes: 40 additions & 20 deletions APIKit.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

33 changes: 16 additions & 17 deletions APIKit/API.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import Foundation

#if APIKIT_DYNAMIC_FRAMEWORK || COCOAPODS
import LlamaKit
import Result
import Box
#endif

public let APIKitErrorDomain = "APIKitErrorDomain"
Expand Down Expand Up @@ -46,7 +47,7 @@ public class API {
default:
switch requestBodyBuilder.buildBodyFromObject(parameters) {
case .Success(let box):
request.HTTPBody = box.unbox
request.HTTPBody = box.value

case .Failure(let box):
return nil
Expand Down Expand Up @@ -80,30 +81,28 @@ public class API {
task.request = Box(request)
task.completionHandler = { data, URLResponse, connectionError in
if let error = connectionError {
dispatch_async(mainQueue, { handler(failure(error)) })
dispatch_async(mainQueue, { handler(.failure(error)) })
return
}

let statusCode = (URLResponse as? NSHTTPURLResponse)?.statusCode ?? 0
if !contains(self.acceptableStatusCodes, statusCode) {
let error: NSError = {
switch self.responseBodyParser.parseData(data) {
case .Success(let box): return self.responseErrorFromObject(box.unbox)
case .Failure(let box): return box.unbox
}
}()

dispatch_async(mainQueue) { handler(failure(error)) }
let error = self.responseBodyParser.parseData(data).analysis(
ifSuccess: { self.responseErrorFromObject($0) },
ifFailure: { $0 }
)

dispatch_async(mainQueue) { handler(.failure(error)) }
return
}

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

Expand All @@ -116,7 +115,7 @@ public class API {
} else {
let userInfo = [NSLocalizedDescriptionKey: "failed to build request."]
let error = NSError(domain: APIKitErrorDomain, code: 0, userInfo: userInfo)
dispatch_async(mainQueue, { handler(failure(error)) })
dispatch_async(mainQueue, { handler(.failure(error)) })

return nil
}
Expand All @@ -132,10 +131,10 @@ public class API {
var request: T?
switch task {
case let x as NSURLSessionDataTask:
request = x.request?.unbox as? T
request = x.request?.value as? T

case let x as NSURLSessionDownloadTask:
request = x.request?.unbox as? T
request = x.request?.value as? T

default:
break
Expand Down Expand Up @@ -214,7 +213,7 @@ private extension NSURLSessionDataTask {

private var completionHandler: ((NSData, NSURLResponse?, NSError?) -> Void)? {
get {
return (objc_getAssociatedObject(self, &dataTaskCompletionHandlerKey) as? Box<(NSData, NSURLResponse?, NSError?) -> Void>)?.unbox
return (objc_getAssociatedObject(self, &dataTaskCompletionHandlerKey) as? Box<(NSData, NSURLResponse?, NSError?) -> Void>)?.value
}

set {
Expand Down
12 changes: 6 additions & 6 deletions APIKit/RequestBodyBuilder.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Foundation

#if APIKIT_DYNAMIC_FRAMEWORK || COCOAPODS
import LlamaKit
import Result
#endif

public let APIKitRequestBodyBuidlerErrorDomain = "APIKitRequestBodyBuidlerErrorDomain"
Expand Down Expand Up @@ -30,17 +30,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)
return failure(error)
return .failure(error)
}

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

case .URL(let encoding):
return try({ error in
return try { error in
return URLEncodedSerialization.dataFromObject(object, encoding: encoding, error: error)
})
}

case .Custom(let (_, buildBodyFromObject)):
return buildBodyFromObject(object)
Expand Down
10 changes: 5 additions & 5 deletions APIKit/ResponseBodyParser.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Foundation

#if APIKIT_DYNAMIC_FRAMEWORK || COCOAPODS
import LlamaKit
import Result
#endif

public enum ResponseBodyParser {
Expand All @@ -25,14 +25,14 @@ public enum ResponseBodyParser {
public func parseData(data: NSData) -> Result<AnyObject, NSError> {
switch self {
case .JSON(let readingOptions):
return try({ error in
return try { error in
return NSJSONSerialization.JSONObjectWithData(data, options: readingOptions, error: error)
})
}

case .URL(let encoding):
return try({ error in
return try { error in
return URLEncodedSerialization.objectFromData(data, encoding: encoding, error: error)
})
}

case .Custom(let (accept, parseData)):
return parseData(data)
Expand Down
10 changes: 5 additions & 5 deletions APIKitTests/APITests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class APITests: XCTestCase {
MockAPI.sendRequest(request) { response in
switch response {
case .Success(let box):
assert(box.unbox, ==, dictionary)
assert(box.value, ==, dictionary)

case .Failure:
XCTFail()
Expand Down Expand Up @@ -84,7 +84,7 @@ class APITests: XCTestCase {
XCTFail()

case .Failure(let box):
let error = box.unbox
let error = box.value
assertEqual(error.domain, error.domain)
assertEqual(error.code, error.code)
}
Expand Down Expand Up @@ -112,7 +112,7 @@ class APITests: XCTestCase {
XCTFail()

case .Failure(let box):
let error = box.unbox
let error = box.value
assertEqual(error.domain, "MockAPIErrorDomain")
assertEqual(error.code, 10000)
}
Expand Down Expand Up @@ -141,7 +141,7 @@ class APITests: XCTestCase {
XCTFail()

case .Failure(let box):
let error = box.unbox
let error = box.value
assert(error.domain, ==, NSCocoaErrorDomain)
assertEqual(error.code, 3840)
}
Expand Down Expand Up @@ -172,7 +172,7 @@ class APITests: XCTestCase {
XCTFail()

case .Failure(let box):
let error = box.unbox
let error = box.value
assert(error.domain, ==, NSURLErrorDomain)
assertEqual(error.code, NSURLErrorCancelled)
}
Expand Down
18 changes: 9 additions & 9 deletions APIKitTests/RequestBodyBuilderTests.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Foundation
import APIKit
import Assertions
import LlamaKit
import Result
import XCTest

class RequestBodyBuilderTests: XCTestCase {
Expand All @@ -16,7 +16,7 @@ class RequestBodyBuilderTests: XCTestCase {

switch builder.buildBodyFromObject(object) {
case .Success(let box):
let dictionary = NSJSONSerialization.JSONObjectWithData(box.unbox, options: nil, error: nil) as? [String: Int]
let dictionary = NSJSONSerialization.JSONObjectWithData(box.value, options: nil, error: nil) as? [String: Int]
assertEqual(dictionary?["foo"], 1)
assertEqual(dictionary?["bar"], 2)
assertEqual(dictionary?["baz"], 3)
Expand All @@ -35,7 +35,7 @@ class RequestBodyBuilderTests: XCTestCase {
XCTFail()

case .Failure(let box):
let error = box.unbox
let error = box.value
assertEqual(error.domain, APIKitRequestBodyBuidlerErrorDomain)
assertEqual(error.code, 0)
}
Expand All @@ -52,7 +52,7 @@ class RequestBodyBuilderTests: XCTestCase {

switch builder.buildBodyFromObject(object) {
case .Success(let box):
let dictionary = URLEncodedSerialization.objectFromData(box.unbox, encoding: NSUTF8StringEncoding, error: nil) as? [String: String]
let dictionary = URLEncodedSerialization.objectFromData(box.value, encoding: NSUTF8StringEncoding, error: nil) as? [String: String]
assertEqual(dictionary?["foo"], "1")
assertEqual(dictionary?["bar"], "2")
assertEqual(dictionary?["baz"], "3")
Expand All @@ -63,20 +63,20 @@ class RequestBodyBuilderTests: XCTestCase {
}

func testCustomHeader() {
let builder = RequestBodyBuilder.Custom(contentTypeHeader: "foo", buildBodyFromObject: { o in success(o as! NSData) })
let builder = RequestBodyBuilder.Custom(contentTypeHeader: "foo", buildBodyFromObject: { o in .success(o as! NSData) })
assertEqual(builder.contentTypeHeader, "foo")
}

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

switch builder.buildBodyFromObject(string) {
case .Success(let box):
assertEqual(box.unbox, expectedData)
assertEqual(box.value, expectedData)

case .Failure:
XCTFail()
Expand All @@ -87,15 +87,15 @@ class RequestBodyBuilderTests: XCTestCase {
let string = "foo"
let expectedError = NSError()
let builder = RequestBodyBuilder.Custom(contentTypeHeader: "", buildBodyFromObject: { object in
return failure(expectedError)
return .failure(expectedError)
})

switch builder.buildBodyFromObject(string) {
case .Success:
XCTFail()

case .Failure(let box):
assertEqual(box.unbox, expectedError)
assertEqual(box.value, expectedError)
}
}
}
18 changes: 9 additions & 9 deletions APIKitTests/ResponseBodyParserTests.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Foundation
import APIKit
import Assertions
import LlamaKit
import Result
import XCTest

class ResponseBodyParserTests: XCTestCase {
Expand All @@ -17,7 +17,7 @@ class ResponseBodyParserTests: XCTestCase {

switch parser.parseData(data) {
case .Success(let box):
let dictionary = box.unbox as? [String: Int]
let dictionary = box.value as? [String: Int]
assertEqual(dictionary?["foo"], 1)
assertEqual(dictionary?["bar"], 2)
assertEqual(dictionary?["baz"], 3)
Expand All @@ -37,7 +37,7 @@ class ResponseBodyParserTests: XCTestCase {
XCTFail()

case .Failure(let box):
let error = box.unbox
let error = box.value
assert(error.domain, ==, NSCocoaErrorDomain)
assertEqual(error.code, 3840)
}
Expand All @@ -55,7 +55,7 @@ class ResponseBodyParserTests: XCTestCase {

switch parser.parseData(data) {
case .Success(let box):
let dictionary = box.unbox as? [String: String]
let dictionary = box.value as? [String: String]
assertEqual(dictionary?["foo"], "1")
assertEqual(dictionary?["bar"], "2")
assertEqual(dictionary?["baz"], "3")
Expand All @@ -66,20 +66,20 @@ class ResponseBodyParserTests: XCTestCase {
}

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

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

switch parser.parseData(data) {
case .Success(let box):
let dictionary = box.unbox as? [String: Int]
let dictionary = box.value as? [String: Int]
assertEqual(dictionary, expectedDictionary)

case .Failure:
Expand All @@ -91,15 +91,15 @@ class ResponseBodyParserTests: XCTestCase {
let expectedError = NSError()
let data = NSData()
let parser = ResponseBodyParser.Custom(acceptHeader: "", parseData: { data in
return failure(expectedError)
return .failure(expectedError)
})

switch parser.parseData(data) {
case .Success:
XCTFail()

case .Failure(let box):
assertEqual(box.unbox, expectedError)
assertEqual(box.value, expectedError)
}
}
}
2 changes: 1 addition & 1 deletion Cartfile
Original file line number Diff line number Diff line change
@@ -1 +1 @@
github "LlamaKit/LlamaKit" ~> 0.6.0
github "antitypical/Result" ~> 0.4
3 changes: 2 additions & 1 deletion Cartfile.resolved
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
github "antitypical/Assertions" "19bac03828dcb2f9b9ecb7f829e09bb3900886e5"
github "LlamaKit/LlamaKit" "v0.6.0"
github "robrix/Box" "1.2.1"
github "AliSoftware/OHHTTPStubs" "4.0.1"
github "antitypical/Result" "0.4"
1 change: 0 additions & 1 deletion Carthage/Checkouts/LlamaKit
Submodule LlamaKit deleted from e28d7f
1 change: 0 additions & 1 deletion DemoApp/GitHub.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import Foundation
import APIKit
import LlamaKit

class GitHub: API {
override class var baseURL: NSURL {
Expand Down
Loading

0 comments on commit 210223b

Please sign in to comment.