Skip to content

Commit 73c2656

Browse files
committed
Merge pull request #64 from akisute/master
Added "object" and "URLResponse" to default error, allow users to create more 'rogue' URL path
2 parents dfba488 + c86e023 commit 73c2656

File tree

4 files changed

+570
-6
lines changed

4 files changed

+570
-6
lines changed

APIKit.xcodeproj/project.pbxproj

+6
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
7FEC5A191A96FE2600B1D3C0 /* ResponseBodyParserTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7FEC5A181A96FE2600B1D3C0 /* ResponseBodyParserTests.swift */; };
3232
7FEC5A1A1A96FE2600B1D3C0 /* APIKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 7F45FCDD1A94D02C006863BB /* APIKit.framework */; };
3333
84B5C6BC1B42CD430032068D /* APIError.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7F5FA6B41B3C58210090B0AF /* APIError.swift */; };
34+
C52CE4F81B984C9B00B7AB59 /* RequestCreateTaskInURLSessionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = C52CE4F71B984C9B00B7AB59 /* RequestCreateTaskInURLSessionTests.swift */; settings = {ASSET_TAGS = (); }; };
35+
C52CE4F91B984C9B00B7AB59 /* RequestCreateTaskInURLSessionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = C52CE4F71B984C9B00B7AB59 /* RequestCreateTaskInURLSessionTests.swift */; settings = {ASSET_TAGS = (); }; };
3436
CD5115251B1FFBA900514240 /* Result.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = CD5115241B1FFBA900514240 /* Result.framework */; };
3537
CD5115261B1FFBA900514240 /* Result.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = CD5115241B1FFBA900514240 /* Result.framework */; };
3638
CD5115271B1FFBA900514240 /* Result.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = CD5115241B1FFBA900514240 /* Result.framework */; };
@@ -104,6 +106,7 @@
104106
7FEC5A141A96FE2600B1D3C0 /* APIKitTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = APIKitTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
105107
7FEC5A171A96FE2600B1D3C0 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
106108
7FEC5A181A96FE2600B1D3C0 /* ResponseBodyParserTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ResponseBodyParserTests.swift; sourceTree = "<group>"; };
109+
C52CE4F71B984C9B00B7AB59 /* RequestCreateTaskInURLSessionTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RequestCreateTaskInURLSessionTests.swift; sourceTree = "<group>"; };
107110
CD5115241B1FFBA900514240 /* Result.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; path = Result.framework; sourceTree = BUILT_PRODUCTS_DIR; };
108111
CD51152D1B1FFCC700514240 /* OHHTTPStubs.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; path = OHHTTPStubs.framework; sourceTree = BUILT_PRODUCTS_DIR; };
109112
/* End PBXFileReference section */
@@ -199,6 +202,7 @@
199202
children = (
200203
7F1B190A1AA2CA1300C7AFCF /* APITests.swift */,
201204
19C16B531B83327A001D77CC /* RequestTests.swift */,
205+
C52CE4F71B984C9B00B7AB59 /* RequestCreateTaskInURLSessionTests.swift */,
202206
7F30A8551A975BD600A8C136 /* RequestBodyBuilderTests.swift */,
203207
7FEC5A181A96FE2600B1D3C0 /* ResponseBodyParserTests.swift */,
204208
7FEC5A161A96FE2600B1D3C0 /* Supporting Files */,
@@ -392,6 +396,7 @@
392396
files = (
393397
7F1B190C1AA2CA1300C7AFCF /* APITests.swift in Sources */,
394398
19C16B551B83327A001D77CC /* RequestTests.swift in Sources */,
399+
C52CE4F91B984C9B00B7AB59 /* RequestCreateTaskInURLSessionTests.swift in Sources */,
395400
7F0869A01A9787AF001AD3E1 /* RequestBodyBuilderTests.swift in Sources */,
396401
7F0869A11A9787AF001AD3E1 /* ResponseBodyParserTests.swift in Sources */,
397402
);
@@ -431,6 +436,7 @@
431436
files = (
432437
7F1B190B1AA2CA1300C7AFCF /* APITests.swift in Sources */,
433438
19C16B541B83327A001D77CC /* RequestTests.swift in Sources */,
439+
C52CE4F81B984C9B00B7AB59 /* RequestCreateTaskInURLSessionTests.swift in Sources */,
434440
7FEC5A191A96FE2600B1D3C0 /* ResponseBodyParserTests.swift in Sources */,
435441
7F30A8561A975BD600A8C136 /* RequestBodyBuilderTests.swift in Sources */,
436442
);

APIKit/Request.swift

+6-5
Original file line numberDiff line numberDiff line change
@@ -66,21 +66,23 @@ public extension Request {
6666
}
6767

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

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

7879
let URLRequest = NSMutableURLRequest()
7980

8081
switch method {
8182
case .GET, .HEAD, .DELETE:
82-
components.percentEncodedQuery = URLEncodedSerialization.stringFromDictionary(parameters)
83-
83+
if parameters.count > 0 {
84+
components.percentEncodedQuery = URLEncodedSerialization.stringFromDictionary(parameters)
85+
}
8486
default:
8587
do {
8688
URLRequest.HTTPBody = try requestBodyBuilder.buildBodyFromObject(parameters)
@@ -89,7 +91,6 @@ public extension Request {
8991
}
9092
}
9193

92-
components.path = ((components.path ?? "") as NSString).stringByAppendingPathComponent(path)
9394
URLRequest.URL = components.URL
9495
URLRequest.HTTPMethod = method.rawValue
9596
URLRequest.setValue(requestBodyBuilder.contentTypeHeader, forHTTPHeaderField: "Content-Type")

APIKitTests/APITests.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ class APITests: XCTestCase {
124124
case .UnacceptableStatusCode(let statusCode, let error as NSError):
125125
XCTAssert(statusCode == 400)
126126
XCTAssert(error.domain == "APIKitErrorDomain")
127-
127+
XCTAssertNotNil(error.userInfo)
128128
default:
129129
XCTFail()
130130
}

0 commit comments

Comments
 (0)