Skip to content

Commit 7d72709

Browse files
committed
- generic body parsing removed, if the response errored
- test adjustments
1 parent 21b9c82 commit 7d72709

File tree

6 files changed

+21
-15
lines changed

6 files changed

+21
-15
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
language: objective-c
2-
osx_image: xcode9.1
2+
osx_image: xcode9.4
33
xcode_project: RESTAPI.xcodeproj
44
xcode_scheme: RESTAPI
55
xcode_sdk: iphonesimulator10.0

Cartfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
github "SwiftyJSON/SwiftyJSON" "master"
1+
github "SwiftyJSON/SwiftyJSON"

Cartfile.resolved

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
github "SwiftyJSON/SwiftyJSON" "3c83ad485a11f53244e077d9b7625fbb2deee937"
1+
github "SwiftyJSON/SwiftyJSON" "4.1.0"
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>IDEDidComputeMac32BitWarning</key>
6+
<true/>
7+
</dict>
8+
</plist>

RESTAPI/API+JSONParseable.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,11 @@ public extension API {
6767
}
6868

6969
//MARK: - Private part
70-
fileprivate func parseableRequest<T: JSONParseable>(_ method: String, endpoint: String, query: [String: Queryable]? = nil,
70+
private func parseableRequest<T: JSONParseable>(_ method: String, endpoint: String, query: [String: Queryable]? = nil,
7171
data: ValidRequestData? = nil,
7272
completion: @escaping (_ error: APIError?, _ object: T?) -> ()) {
7373
dataTask(clientURLRequest(endpoint, query: query, params: data), method: method) { err ,data in
74-
if let validData = data {
74+
if let validData = data, err == nil {
7575
completion(err, T(withJSON: validData))
7676
}
7777
else {
@@ -80,11 +80,11 @@ public extension API {
8080
}
8181
}
8282

83-
fileprivate func parseableRequest<T: JSONParseable>(_ method: String, endpoint: String, query: [String: Queryable]? = nil,
83+
private func parseableRequest<T: JSONParseable>(_ method: String, endpoint: String, query: [String: Queryable]? = nil,
8484
data: ValidRequestData? = nil,
8585
completion: @escaping (_ error: APIError?, _ object: [T]?) -> ()) {
8686
dataTask(clientURLRequest(endpoint, query: query, params: data), method: method) { err ,data in
87-
if let arrayData = data?.array {
87+
if let arrayData = data?.array, err == nil {
8888
let JSONData = arrayData.map() {element in
8989
return T(withJSON: element)
9090
}

RESTAPITests/RESTAPITests.swift

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,12 @@ import SwiftyJSON
1313
//MARK: - ExamplePostModel
1414
struct ExamplePostModel {
1515
var body: String
16-
var id: Int
16+
var id: Int?
1717
var title: String
1818
var userId: Int
1919

20-
init(withBody body: String, id: Int, title: String, userId: Int) {
20+
init(withBody body: String, title: String, userId: Int) {
2121
self.body = body
22-
self.id = id
2322
self.title = title
2423
self.userId = userId
2524
}
@@ -38,7 +37,7 @@ extension ExamplePostModel: JSONParseable {
3837
extension ExamplePostModel: JSONConvertible {
3938

4039
var parameterValue: [String: Any] {
41-
return ["body": body, "id": id, "title": title, "userId": userId]
40+
return ["body": body, "title": title, "userId": userId]
4241
}
4342
}
4443

@@ -66,16 +65,15 @@ class RESTAPITests: XCTestCase {
6665

6766
func testPostAndPatch() {
6867
let expectation = self.expectation(description: "post&patch")
69-
let example = ExamplePostModel(withBody: "something", id: 1, title: "Some title", userId: 9)
68+
let example = ExamplePostModel(withBody: "something", title: "Some title", userId: 9)
7069

7170
testServerApi.post("/posts", data: example){ (error, responsePost: ExamplePostModel?) in
72-
XCTAssertEqual(responsePost?.id, example.id)
73-
guard let responsePost = responsePost else {
71+
guard let _ = responsePost?.id else {
7472
XCTAssert(false)
7573
return
7674
}
7775

78-
self.testServerApi.patch("/posts/\(responsePost.id)", data: ["title": "Other title"]) { (error, responsePost: ExamplePostModel?) in
76+
self.testServerApi.patch("/posts/1", data: ["title": "Other title"]) { (error, responsePost: ExamplePostModel?) in
7977
XCTAssertEqual(responsePost?.title, "Other title")
8078
expectation.fulfill()
8179
}

0 commit comments

Comments
 (0)