Skip to content

Commit 85ef847

Browse files
Add createdAt to request body.
1 parent 198420b commit 85ef847

File tree

7 files changed

+126
-14
lines changed

7 files changed

+126
-14
lines changed

swift-sdk/Constants.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,10 @@ public enum JsonKey: String, JsonKeyRepresentable {
176176
static let sentAt = "sentAt"
177177
}
178178

179+
public enum Body {
180+
static let createdAt = "createdAt"
181+
}
182+
179183
public enum InApp {
180184
static let trigger = "trigger"
181185
static let type = "type"

swift-sdk/Internal/IterableAPICallRequest.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,14 @@ struct IterableAPICallRequest {
3939
}
4040
}
4141

42+
func addingBodyField(key: AnyHashable, value: Any) -> IterableAPICallRequest {
43+
IterableAPICallRequest(apiKey: apiKey,
44+
endPoint: endPoint,
45+
auth: auth,
46+
deviceMetadata: deviceMetadata,
47+
iterableRequest: iterableRequest.addingBodyField(key: key, value: value))
48+
}
49+
4250
private func createIterableHeaders(currentDate: Date) -> [String: String] {
4351
var headers = [JsonKey.contentType.jsonKey: JsonValue.applicationJson.jsonStringValue,
4452
JsonKey.Header.sdkPlatform: JsonValue.iOS.jsonStringValue,

swift-sdk/Internal/IterableAPICallTaskProcessor.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@ struct IterableAPICallTaskProcessor: IterableTaskProcessor {
1919
return IterableTaskError.createErroredFuture(reason: "expecting data")
2020
}
2121

22-
let iterableRequest = try JSONDecoder().decode(IterableAPICallRequest.self, from: data)
22+
let decodedIterableRequest = try JSONDecoder().decode(IterableAPICallRequest.self, from: data)
23+
let iterableRequest = decodedIterableRequest.addingBodyField(key: JsonKey.Body.createdAt,
24+
value: IterableUtil.int(fromDate: task.scheduledAt))
25+
2326
guard let urlRequest = iterableRequest.convertToURLRequest(currentDate: dateProvider.currentDate) else {
2427
return IterableTaskError.createErroredFuture(reason: "could not convert to url request")
2528
}

swift-sdk/Internal/IterableRequest.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,14 @@ extension IterableRequest: Codable {
4545
}
4646
}
4747

48+
func addingBodyField(key: AnyHashable, value: Any) -> IterableRequest {
49+
if case .post(let postRequest) = self {
50+
return .post(postRequest.addingBodyField(key: key, value: value))
51+
} else {
52+
return self
53+
}
54+
}
55+
4856
private static let requestTypeGet = "get"
4957
private static let requestTypePost = "post"
5058
}
@@ -58,6 +66,12 @@ struct PostRequest {
5866
let path: String
5967
let args: [String: String]?
6068
let body: [AnyHashable: Any]?
69+
70+
func addingBodyField(key: AnyHashable, value: Any) -> PostRequest {
71+
var newBody = body ?? [AnyHashable: Any]()
72+
newBody[key] = value
73+
return PostRequest(path: path, args: args, body: newBody)
74+
}
6175
}
6276

6377
extension PostRequest: Codable {

swift-sdk/Internal/IterableRequestUtil.swift

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,35 @@
88
import Foundation
99

1010
struct IterableRequestUtil {
11-
static func createPostRequest(forApiEndPoint apiEndPoint: String, path: String, headers: [String: String]? = nil, args: [String: String]? = nil, body: [AnyHashable: Any]? = nil) -> URLRequest? {
12-
createPostRequest(forApiEndPoint: apiEndPoint, path: path, headers: headers, args: args, body: dictToJsonData(body))
11+
static func createPostRequest(forApiEndPoint apiEndPoint: String,
12+
path: String,
13+
headers: [String: String]? = nil,
14+
args: [String: String]? = nil,
15+
body: [AnyHashable: Any]? = nil) -> URLRequest? {
16+
createPostRequest(forApiEndPoint: apiEndPoint,
17+
path: path,
18+
headers: headers,
19+
args: args,
20+
body: dictToJsonData(body))
1321
}
1422

15-
static func createPostRequest<T: Encodable>(forApiEndPoint apiEndPoint: String, path: String, headers: [String: String]? = nil, args: [String: String]? = nil, body: T) -> URLRequest? {
16-
createPostRequest(forApiEndPoint: apiEndPoint, path: path, headers: headers, args: args, body: try? JSONEncoder().encode(body))
23+
static func createPostRequest<T: Encodable>(forApiEndPoint apiEndPoint: String,
24+
path: String,
25+
headers: [String: String]? = nil,
26+
args: [String: String]? = nil,
27+
body: T) -> URLRequest? {
28+
createPostRequest(forApiEndPoint: apiEndPoint,
29+
path: path,
30+
headers: headers,
31+
args: args,
32+
body: try? JSONEncoder().encode(body))
1733
}
1834

19-
static func createPostRequest(forApiEndPoint apiEndPoint: String, path: String, headers: [String: String]? = nil, args: [String: String]? = nil, body: Data? = nil) -> URLRequest? {
35+
static func createPostRequest(forApiEndPoint apiEndPoint: String,
36+
path: String,
37+
headers: [String: String]? = nil,
38+
args: [String: String]? = nil,
39+
body: Data? = nil) -> URLRequest? {
2040
guard let url = getUrlComponents(forApiEndPoint: apiEndPoint, path: path, args: args)?.url else {
2141
return nil
2242
}
@@ -29,7 +49,10 @@ struct IterableRequestUtil {
2949
return request
3050
}
3151

32-
static func createGetRequest(forApiEndPoint apiEndPoint: String, path: String, headers: [String: String]? = nil, args: [String: String]? = nil) -> URLRequest? {
52+
static func createGetRequest(forApiEndPoint apiEndPoint: String,
53+
path: String,
54+
headers: [String: String]? = nil,
55+
args: [String: String]? = nil) -> URLRequest? {
3356
guard let url = getUrlComponents(forApiEndPoint: apiEndPoint, path: path, args: args)?.url else {
3457
return nil
3558
}

tests/offline-events-tests/TaskProcessorTests.swift

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,42 @@ class TaskProcessorTests: XCTestCase {
180180
wait(for: [expectation1], timeout: 5.0)
181181
}
182182

183-
private func createSampleTask() throws -> IterableTask? {
183+
func testCreatedAtInBody() throws {
184+
let expectation1 = expectation(description: #function)
185+
let date = Date()
186+
let createdAtTime = Int(date.timeIntervalSince1970 * 1000)
187+
let task = try createSampleTask(scheduledAt: date)!
188+
189+
let networkSession = MockNetworkSession()
190+
networkSession.requestCallback = { request in
191+
if request.bodyDict.contains(where: { $0.key == "createdAt" && ($0.value as! Int) == createdAtTime }) {
192+
expectation1.fulfill()
193+
}
194+
}
195+
196+
// process data
197+
let processor = IterableAPICallTaskProcessor(networkSession: networkSession)
198+
try processor.process(task: task)
199+
.onSuccess { taskResult in
200+
switch taskResult {
201+
case .success(detail: _):
202+
break
203+
case .failureWithNoRetry(detail: _):
204+
XCTFail("not expecting failure with no retry")
205+
case .failureWithRetry(retryAfter: _, detail: _):
206+
XCTFail("not expecting failure with retry")
207+
}
208+
}
209+
.onError { _ in
210+
XCTFail()
211+
}
212+
213+
try persistenceProvider.mainQueueContext().delete(task: task)
214+
try persistenceProvider.mainQueueContext().save()
215+
wait(for: [expectation1], timeout: 5.0)
216+
}
217+
218+
private func createSampleTask(scheduledAt: Date = Date(), requestedAt: Date = Date()) throws -> IterableTask? {
184219
let apiKey = "test-api-key"
185220
let email = "[email protected]"
186221
let eventName = "CustomEvent1"
@@ -206,9 +241,9 @@ class TaskProcessorTests: XCTestCase {
206241
let taskId = IterableUtil.generateUUID()
207242
try persistenceProvider.mainQueueContext().create(task: IterableTask(id: taskId,
208243
type: .apiCall,
209-
scheduledAt: Date(),
244+
scheduledAt: scheduledAt,
210245
data: data,
211-
requestedAt: Date()))
246+
requestedAt: requestedAt))
212247
try persistenceProvider.mainQueueContext().save()
213248

214249
return try persistenceProvider.mainQueueContext().findTask(withId: taskId)

tests/offline-events-tests/TaskRunnerTests.swift

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,34 @@ class TaskRunnerTests: XCTestCase {
240240
wait(for: [expectation1], timeout: 5.0)
241241
}
242242

243-
private func scheduleSampleTask(notificationCenter: NotificationCenterProtocol) throws -> String {
243+
func testCreatedAtInBody() throws {
244+
let date = Date()
245+
let createdAtTime = Int(date.timeIntervalSince1970 * 1000)
246+
let dateProvider = MockDateProvider()
247+
dateProvider.currentDate = date
248+
let expectation1 = expectation(description: #function)
249+
let networkSession = MockNetworkSession()
250+
networkSession.requestCallback = { request in
251+
if request.bodyDict.contains(where: { $0.key == "createdAt" && ($0.value as! Int) == createdAtTime }) {
252+
expectation1.fulfill()
253+
}
254+
}
255+
let notificationCenter = MockNotificationCenter()
256+
257+
let taskRunner = IterableTaskRunner(networkSession: networkSession,
258+
persistenceContextProvider: persistenceContextProvider,
259+
notificationCenter: notificationCenter,
260+
timeInterval: 0.5)
261+
taskRunner.start()
262+
263+
let _ = try! self.scheduleSampleTask(notificationCenter: notificationCenter, dateProvider: dateProvider)
264+
verifyTaskIsExecuted(notificationCenter, withinInterval: 1.0)
265+
266+
taskRunner.stop()
267+
wait(for: [expectation1], timeout: 5.0)
268+
}
269+
270+
private func scheduleSampleTask(notificationCenter: NotificationCenterProtocol, dateProvider: DateProviderProtocol = SystemDateProvider()) throws -> String {
244271
let apiKey = "zee-api-key"
245272
let eventName = "CustomEvent1"
246273
let dataFields = ["var1": "val1", "var2": "val2"]
@@ -292,11 +319,9 @@ class TaskRunnerTests: XCTestCase {
292319
appPackageName: Bundle.main.appPackageName ?? "")
293320

294321
private lazy var persistenceContextProvider: IterablePersistenceContextProvider = {
295-
let provider = CoreDataPersistenceContextProvider(dateProvider: dateProvider)!
322+
let provider = CoreDataPersistenceContextProvider()!
296323
return provider
297324
}()
298-
299-
private let dateProvider = MockDateProvider()
300325
}
301326

302327
extension TaskRunnerTests: AuthProvider {

0 commit comments

Comments
 (0)