Skip to content

Commit 19d79ec

Browse files
committed
Always use POST to update installation.
1 parent a002bcf commit 19d79ec

File tree

3 files changed

+44
-7
lines changed

3 files changed

+44
-7
lines changed

Sources/Storage/BatchRequest.swift

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ class BatchRequest {
2727
return method ?? (isNewborn ? .post : .put)
2828
}
2929

30-
var body: AnyObject {
31-
var body: [String: AnyObject] = [
32-
"__internalId": object.objectId?.value as AnyObject? ?? object.internalId as AnyObject
33-
]
30+
func getBody(internalId: String) -> [String: Any] {
31+
var body: [String: Any] = [:]
32+
33+
body["__internalId"] = internalId
3434

3535
var children: [(String, LCObject)] = []
3636

@@ -65,14 +65,19 @@ class BatchRequest {
6565
body["__children"] = list as AnyObject?
6666
}
6767

68-
return body as AnyObject
68+
return body
6969
}
7070

7171
func jsonValue() throws -> AnyObject {
7272
let method = actualMethod
7373
let path = try HTTPClient.default.getBatchRequestPath(object: object, method: method)
74+
let internalId = object.objectId?.value ?? object.internalId
75+
76+
if let request = try object.preferredBatchRequest(method: method, path: path, internalId: internalId) {
77+
return request as AnyObject
78+
}
7479

75-
var request: [String: AnyObject] = [
80+
var request: [String: Any] = [
7681
"path": path as AnyObject,
7782
"method": method.rawValue as AnyObject
7883
]
@@ -81,7 +86,7 @@ class BatchRequest {
8186
case .get:
8287
break
8388
case .post, .put:
84-
request["body"] = body
89+
request["body"] = getBody(internalId: internalId)
8590

8691
if isNewborn {
8792
request["new"] = true as AnyObject?

Sources/Storage/DataType/Installation.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,29 @@ public final class LCInstallation: LCObject {
8282
#endif
8383
}
8484

85+
override func preferredBatchRequest(method: HTTPClient.Method, path: String, internalId: String) throws -> [String : Any]? {
86+
switch method {
87+
case .post, .put:
88+
var request: [String: Any] = [:]
89+
90+
request["method"] = HTTPClient.Method.post.rawValue
91+
request["path"] = try HTTPClient.default.getBatchRequestPath(object: self, method: .post)
92+
93+
if var body = dictionary.lconValue as? [String: Any] {
94+
body["__internalId"] = internalId
95+
96+
body.removeValue(forKey: "createdAt")
97+
body.removeValue(forKey: "updatedAt")
98+
99+
request["body"] = body
100+
}
101+
102+
return request
103+
default:
104+
return nil
105+
}
106+
}
107+
85108
override func objectDidSave() {
86109
super.objectDidSave()
87110

Sources/Storage/DataType/Object.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,15 @@ open class LCObject: NSObject, LCValue, LCValueExtension, Sequence {
164164
] as AnyObject
165165
}
166166

167+
/**
168+
Get preferred batch request.
169+
170+
If returns nil, it will use the default batch request.
171+
*/
172+
func preferredBatchRequest(method: HTTPClient.Method, path: String, internalId: String) throws -> [String: Any]? {
173+
return nil
174+
}
175+
167176
static func instance() -> LCValue {
168177
return self.init()
169178
}

0 commit comments

Comments
 (0)