Skip to content

Commit

Permalink
Merge pull request #64 from akisute/master
Browse files Browse the repository at this point in the history
Added "object" and "URLResponse" to default error, allow users to create more 'rogue' URL path
  • Loading branch information
ishkawa committed Sep 4, 2015
2 parents dfba488 + c86e023 commit 73c2656
Show file tree
Hide file tree
Showing 4 changed files with 570 additions and 6 deletions.
6 changes: 6 additions & 0 deletions APIKit.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
7FEC5A191A96FE2600B1D3C0 /* ResponseBodyParserTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7FEC5A181A96FE2600B1D3C0 /* ResponseBodyParserTests.swift */; };
7FEC5A1A1A96FE2600B1D3C0 /* APIKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 7F45FCDD1A94D02C006863BB /* APIKit.framework */; };
84B5C6BC1B42CD430032068D /* APIError.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7F5FA6B41B3C58210090B0AF /* APIError.swift */; };
C52CE4F81B984C9B00B7AB59 /* RequestCreateTaskInURLSessionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = C52CE4F71B984C9B00B7AB59 /* RequestCreateTaskInURLSessionTests.swift */; settings = {ASSET_TAGS = (); }; };
C52CE4F91B984C9B00B7AB59 /* RequestCreateTaskInURLSessionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = C52CE4F71B984C9B00B7AB59 /* RequestCreateTaskInURLSessionTests.swift */; settings = {ASSET_TAGS = (); }; };
CD5115251B1FFBA900514240 /* Result.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = CD5115241B1FFBA900514240 /* Result.framework */; };
CD5115261B1FFBA900514240 /* Result.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = CD5115241B1FFBA900514240 /* Result.framework */; };
CD5115271B1FFBA900514240 /* Result.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = CD5115241B1FFBA900514240 /* Result.framework */; };
Expand Down Expand Up @@ -104,6 +106,7 @@
7FEC5A141A96FE2600B1D3C0 /* APIKitTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = APIKitTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
7FEC5A171A96FE2600B1D3C0 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
7FEC5A181A96FE2600B1D3C0 /* ResponseBodyParserTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ResponseBodyParserTests.swift; sourceTree = "<group>"; };
C52CE4F71B984C9B00B7AB59 /* RequestCreateTaskInURLSessionTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RequestCreateTaskInURLSessionTests.swift; sourceTree = "<group>"; };
CD5115241B1FFBA900514240 /* Result.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; path = Result.framework; sourceTree = BUILT_PRODUCTS_DIR; };
CD51152D1B1FFCC700514240 /* OHHTTPStubs.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; path = OHHTTPStubs.framework; sourceTree = BUILT_PRODUCTS_DIR; };
/* End PBXFileReference section */
Expand Down Expand Up @@ -199,6 +202,7 @@
children = (
7F1B190A1AA2CA1300C7AFCF /* APITests.swift */,
19C16B531B83327A001D77CC /* RequestTests.swift */,
C52CE4F71B984C9B00B7AB59 /* RequestCreateTaskInURLSessionTests.swift */,
7F30A8551A975BD600A8C136 /* RequestBodyBuilderTests.swift */,
7FEC5A181A96FE2600B1D3C0 /* ResponseBodyParserTests.swift */,
7FEC5A161A96FE2600B1D3C0 /* Supporting Files */,
Expand Down Expand Up @@ -392,6 +396,7 @@
files = (
7F1B190C1AA2CA1300C7AFCF /* APITests.swift in Sources */,
19C16B551B83327A001D77CC /* RequestTests.swift in Sources */,
C52CE4F91B984C9B00B7AB59 /* RequestCreateTaskInURLSessionTests.swift in Sources */,
7F0869A01A9787AF001AD3E1 /* RequestBodyBuilderTests.swift in Sources */,
7F0869A11A9787AF001AD3E1 /* ResponseBodyParserTests.swift in Sources */,
);
Expand Down Expand Up @@ -431,6 +436,7 @@
files = (
7F1B190B1AA2CA1300C7AFCF /* APITests.swift in Sources */,
19C16B541B83327A001D77CC /* RequestTests.swift in Sources */,
C52CE4F81B984C9B00B7AB59 /* RequestCreateTaskInURLSessionTests.swift in Sources */,
7FEC5A191A96FE2600B1D3C0 /* ResponseBodyParserTests.swift in Sources */,
7F30A8561A975BD600A8C136 /* RequestBodyBuilderTests.swift in Sources */,
);
Expand Down
11 changes: 6 additions & 5 deletions APIKit/Request.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,21 +66,23 @@ public extension Request {
}

public func errorFromObject(object: AnyObject, URLResponse: NSHTTPURLResponse) -> ErrorType? {
return NSError(domain: "APIKitErrorDomain", code: 0, userInfo: nil)
return NSError(domain: "APIKitErrorDomain", code: 0, userInfo: ["object":object, "URLResponse": URLResponse])
}

// Use Result here because `throws` loses type info of an error (in Swift 2 beta 2)
internal func createTaskInURLSession(URLSession: NSURLSession) -> Result<NSURLSessionDataTask, APIError> {
guard let components = NSURLComponents(URL: baseURL, resolvingAgainstBaseURL: true) else {
let URL = path.isEmpty ? baseURL : baseURL.URLByAppendingPathComponent(path)
guard let components = NSURLComponents(URL: URL, resolvingAgainstBaseURL: true) else {
return .Failure(.InvalidBaseURL(baseURL))
}

let URLRequest = NSMutableURLRequest()

switch method {
case .GET, .HEAD, .DELETE:
components.percentEncodedQuery = URLEncodedSerialization.stringFromDictionary(parameters)

if parameters.count > 0 {
components.percentEncodedQuery = URLEncodedSerialization.stringFromDictionary(parameters)
}
default:
do {
URLRequest.HTTPBody = try requestBodyBuilder.buildBodyFromObject(parameters)
Expand All @@ -89,7 +91,6 @@ public extension Request {
}
}

components.path = ((components.path ?? "") as NSString).stringByAppendingPathComponent(path)
URLRequest.URL = components.URL
URLRequest.HTTPMethod = method.rawValue
URLRequest.setValue(requestBodyBuilder.contentTypeHeader, forHTTPHeaderField: "Content-Type")
Expand Down
2 changes: 1 addition & 1 deletion APIKitTests/APITests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ class APITests: XCTestCase {
case .UnacceptableStatusCode(let statusCode, let error as NSError):
XCTAssert(statusCode == 400)
XCTAssert(error.domain == "APIKitErrorDomain")

XCTAssertNotNil(error.userInfo)
default:
XCTFail()
}
Expand Down
Loading

0 comments on commit 73c2656

Please sign in to comment.