Skip to content

Commit 37b5e8b

Browse files
Merge branch 'master' into feature/mob-2544-change-header-name
# Conflicts: # swift-sdk/Constants.swift
2 parents 4acd8e7 + 4d0c197 commit 37b5e8b

File tree

5 files changed

+50
-6
lines changed

5 files changed

+50
-6
lines changed

swift-sdk/Constants.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,11 @@ public enum Const {
6767

6868
static let unassigned = 300.5
6969
}
70+
71+
enum ProcessorTypeName {
72+
static let online = "Online"
73+
static let offline = "Offline"
74+
}
7075
}
7176

7277
public protocol JsonKeyValueRepresentable {
@@ -186,6 +191,7 @@ public enum JsonKey: String, JsonKeyRepresentable {
186191
static let sdkPlatform = "SDK-Platform"
187192
static let authorization = "Authorization"
188193
static let sentAt = "Sent-At"
194+
static let requestProcessor = "SDK-Request-Processor"
189195
}
190196

191197
public enum Body {

swift-sdk/Internal/IterableAPICallRequest.swift

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,24 @@ struct IterableAPICallRequest {
1313
let deviceMetadata: DeviceMetadata
1414
let iterableRequest: IterableRequest
1515

16-
func convertToURLRequest(currentDate: Date) -> URLRequest? {
16+
enum ProcessorType {
17+
case offline
18+
case online
19+
}
20+
21+
func convertToURLRequest(currentDate: Date, processorType: ProcessorType = .online) -> URLRequest? {
1722
switch iterableRequest {
1823
case let .get(getRequest):
1924
return IterableRequestUtil.createGetRequest(forApiEndPoint: endPoint,
2025
path: getRequest.path,
21-
headers: createIterableHeaders(currentDate: currentDate),
26+
headers: createIterableHeaders(currentDate: currentDate,
27+
processorType: processorType),
2228
args: getRequest.args)
2329
case let .post(postRequest):
2430
return IterableRequestUtil.createPostRequest(forApiEndPoint: endPoint,
2531
path: postRequest.path,
26-
headers: createIterableHeaders(currentDate: currentDate),
32+
headers: createIterableHeaders(currentDate: currentDate,
33+
processorType: processorType),
2734
args: postRequest.args,
2835
body: postRequest.body)
2936
}
@@ -46,12 +53,13 @@ struct IterableAPICallRequest {
4653
iterableRequest: iterableRequest.addingBodyField(key: key, value: value))
4754
}
4855

49-
private func createIterableHeaders(currentDate: Date) -> [String: String] {
56+
private func createIterableHeaders(currentDate: Date, processorType: ProcessorType) -> [String: String] {
5057
var headers = [JsonKey.contentType.jsonKey: JsonValue.applicationJson.jsonStringValue,
5158
JsonKey.Header.sdkPlatform: JsonValue.iOS.jsonStringValue,
5259
JsonKey.Header.sdkVersion: IterableAPI.sdkVersion,
5360
JsonKey.Header.apiKey: apiKey,
5461
JsonKey.Header.sentAt: Self.format(sentAt: currentDate),
62+
JsonKey.Header.requestProcessor: Self.name(for: processorType)
5563
]
5664

5765
if let authToken = auth.authToken {
@@ -64,6 +72,15 @@ struct IterableAPICallRequest {
6472
private static func format(sentAt: Date) -> String {
6573
return "\(IterableUtil.secondsFromEpoch(for: sentAt))"
6674
}
75+
76+
private static func name(for processorType: ProcessorType) -> String {
77+
switch processorType {
78+
case .online:
79+
return Const.ProcessorTypeName.online
80+
case .offline:
81+
return Const.ProcessorTypeName.offline
82+
}
83+
}
6784
}
6885

6986
extension IterableAPICallRequest: Codable {}

swift-sdk/Internal/IterableAPICallTaskProcessor.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ struct IterableAPICallTaskProcessor: IterableTaskProcessor {
2222
let iterableRequest = decodedIterableRequest.addingBodyField(key: JsonKey.Body.createdAt,
2323
value: IterableUtil.secondsFromEpoch(for: task.scheduledAt))
2424

25-
guard let urlRequest = iterableRequest.convertToURLRequest(currentDate: dateProvider.currentDate) else {
25+
guard let urlRequest = iterableRequest.convertToURLRequest(currentDate: dateProvider.currentDate, processorType: .offline) else {
2626
return IterableTaskError.createErroredFuture(reason: "could not convert to url request")
2727
}
2828

tests/swift-sdk-swift-tests/RequestCreatorTests.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,26 @@ class RequestCreatorTests: XCTestCase {
345345
TestUtils.validateMatch(keyPath: KeyPath(JsonKey.preferUserId), value: true, inDictionary: body)
346346
}
347347

348+
func testProcessorTypeOfflineInHeader() throws {
349+
let eventName = "dsfsdf"
350+
351+
let iterableRequest = try createRequestCreator().createTrackEventRequest(eventName, dataFields: nil).get()
352+
let apiCallRequest = IterableAPICallRequest(apiKey: apiKey,
353+
endPoint: Endpoint.api,
354+
auth: Auth(userId: nil, email: "[email protected]", authToken: nil),
355+
deviceMetadata: deviceMetadata,
356+
iterableRequest: iterableRequest)
357+
358+
let request = apiCallRequest.convertToURLRequest(currentDate: dateProvider.currentDate, processorType: .offline)!
359+
360+
TestUtils.validateHeader(request, apiKey, processorType: .offline)
361+
TestUtils.validate(request: request, requestType: .post, apiEndPoint: Endpoint.api, path: Const.Path.trackEvent)
362+
363+
let body = request.bodyDict
364+
TestUtils.validateMatch(keyPath: KeyPath(.eventName), value: eventName, inDictionary: body)
365+
TestUtils.validateNil(keyPath: KeyPath(.dataFields), inDictionary: body)
366+
}
367+
348368
private let apiKey = "zee-api-key"
349369

350370
private let email = "[email protected]"

tests/swift-sdk-swift-tests/TestUtils.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ struct TestUtils {
6868
}
6969
}
7070

71-
static func validateHeader(_ request: URLRequest, _ apiKey: String) {
71+
static func validateHeader(_ request: URLRequest, _ apiKey: String, processorType: IterableAPICallRequest.ProcessorType = .online) {
7272
guard let header = request.allHTTPHeaderFields else {
7373
XCTFail("no header for request")
7474
return
@@ -78,6 +78,7 @@ struct TestUtils {
7878
XCTAssertEqual(header[JsonKey.Header.sdkPlatform], JsonValue.iOS.jsonStringValue)
7979
XCTAssertEqual(header[JsonKey.Header.sdkVersion], IterableAPI.sdkVersion)
8080
XCTAssertEqual(header[JsonKey.Header.apiKey], apiKey)
81+
XCTAssertEqual(header[JsonKey.Header.requestProcessor], processorType == .online ? "Online" : "Offline")
8182
}
8283

8384
static func validateEmailOrUserId(email: String? = nil, userId: String? = nil, inBody body: [String: Any]) {

0 commit comments

Comments
 (0)