From 91c16b9cee80e2a8afe0cc56d6e6befcd2ffe02c Mon Sep 17 00:00:00 2001 From: mohitanand-cred Date: Thu, 21 Dec 2023 13:11:45 +0530 Subject: [PATCH] added header capability to other network requests --- Sources/FlushRequest.swift | 2 +- Sources/MixpanelInstance.swift | 62 ++++++++++++++++++++++++++-------- Sources/Network.swift | 13 ++++--- 3 files changed, 57 insertions(+), 20 deletions(-) diff --git a/Sources/FlushRequest.swift b/Sources/FlushRequest.swift index c6c0fc3a..f14485cc 100644 --- a/Sources/FlushRequest.swift +++ b/Sources/FlushRequest.swift @@ -33,7 +33,7 @@ class FlushRequest: Network { return nil } - let resourceHeaders: [String: String] = headers.merging(["Content-Type": "application/json"]) {(_,new) in new} + let resourceHeaders: [String: String] = ["Content-Type": "application/json"].merging(headers) {(_,new) in new } let ipString = useIP ? "1" : "0" var resourceQueryItems: [URLQueryItem] = [URLQueryItem(name: "ip", value: ipString)] diff --git a/Sources/MixpanelInstance.swift b/Sources/MixpanelInstance.swift index a072fca4..b574e793 100644 --- a/Sources/MixpanelInstance.swift +++ b/Sources/MixpanelInstance.swift @@ -33,7 +33,7 @@ public protocol MixpanelProxyServerDelegate: AnyObject { - returns: return ServerProxyResource to give custom headers and query params. */ - func mixpanelResourceForProxyServer(_ mixpanel: MixpanelInstance) -> ServerProxyResource? + func mixpanelResourceForProxyServer(_ name: String) -> ServerProxyResource? } /** @@ -172,7 +172,7 @@ open class MixpanelInstance: CustomDebugStringConvertible, FlushDelegate, AEDele } /// The a MixpanelProxyServerDelegate object that gives config control over Proxy Server's network activity. - open weak var proxyServerDelegate: MixpanelProxyServerDelegate? + open weak var proxyServerDelegate: MixpanelProxyServerDelegate? = nil open var debugDescription: String { @@ -208,7 +208,9 @@ open class MixpanelInstance: CustomDebugStringConvertible, FlushDelegate, AEDele if (superProperties["$lib_version"] != nil) { trackProps["$lib_version"] = self.superProperties["$lib_version"] as! String } - Network.sendHttpEvent(serverURL: self.serverURL, eventName: "Toggle SDK Logging", apiToken: "metrics-1", distinctId: apiToken, properties: trackProps) + // add headers + let headers = self.proxyServerDelegate?.mixpanelResourceForProxyServer(name)?.headers ?? [:] + Network.sendHttpEvent(serverURL: self.serverURL, headers: headers, eventName: "Toggle SDK Logging", apiToken: "metrics-1", distinctId: apiToken, properties: trackProps) #endif } } @@ -278,12 +280,11 @@ open class MixpanelInstance: CustomDebugStringConvertible, FlushDelegate, AEDele optOutTrackingByDefault: optOutTrackingByDefault, useUniqueDistinctId: useUniqueDistinctId, superProperties: superProperties, - serverURL: proxyServerConfig.serverUrl) - self.proxyServerDelegate = proxyServerConfig.delegate + serverURL: proxyServerConfig.serverUrl, + proxyServerDelegate: proxyServerConfig.delegate) } - - init( + convenience init( apiToken: String?, flushInterval: Double, name: String, @@ -292,6 +293,29 @@ open class MixpanelInstance: CustomDebugStringConvertible, FlushDelegate, AEDele useUniqueDistinctId: Bool = false, superProperties: Properties? = nil, serverURL: String? = nil + ) { + self.init(apiToken: apiToken, + flushInterval: flushInterval, + name: name, + trackAutomaticEvents: trackAutomaticEvents, + optOutTrackingByDefault: optOutTrackingByDefault, + useUniqueDistinctId: useUniqueDistinctId, + superProperties: superProperties, + serverURL: serverURL, + proxyServerDelegate: nil) + } + + + private init( + apiToken: String?, + flushInterval: Double, + name: String, + trackAutomaticEvents: Bool, + optOutTrackingByDefault: Bool = false, + useUniqueDistinctId: Bool = false, + superProperties: Properties? = nil, + serverURL: String? = nil, + proxyServerDelegate: MixpanelProxyServerDelegate? = nil ) { if let apiToken = apiToken, !apiToken.isEmpty { self.apiToken = apiToken @@ -301,9 +325,13 @@ open class MixpanelInstance: CustomDebugStringConvertible, FlushDelegate, AEDele self.serverURL = serverURL BasePath.namedBasePaths[name] = serverURL } + self.proxyServerDelegate = proxyServerDelegate #if DEBUG + //add headers here + let headers = proxyServerDelegate?.mixpanelResourceForProxyServer(name)?.headers ?? [:] MixpanelInstance.didDebugInit( serverURL: self.serverURL, + headers: headers, distinctId: self.apiToken, libName: superProperties?.get(key: "mp_lib", defaultValue: nil), libVersion: superProperties?.get(key: "$lib_version", defaultValue: nil) @@ -619,7 +647,7 @@ open class MixpanelInstance: CustomDebugStringConvertible, FlushDelegate, AEDele } #endif #endif // os(iOS) - private class func didDebugInit(serverURL: String, distinctId: String, libName: String?, libVersion: String?) { + private class func didDebugInit(serverURL: String, headers: [String: String], distinctId: String, libName: String?, libVersion: String?) { if distinctId.count == 32 { let debugInitCount = UserDefaults.standard.integer(forKey: InternalKeys.mpDebugInitCountKey) + 1 var properties: Properties = ["Debug Launch Count": debugInitCount] @@ -629,14 +657,15 @@ open class MixpanelInstance: CustomDebugStringConvertible, FlushDelegate, AEDele if let libVersion = libVersion { properties["$lib_version"] = libVersion } - Network.sendHttpEvent(serverURL: serverURL, eventName: "SDK Debug Launch", apiToken: "metrics-1", distinctId: distinctId, properties: properties) { (_) in } - checkIfImplemented(serverURL: serverURL, distinctId: distinctId, properties: properties) + // add headers + Network.sendHttpEvent(serverURL: serverURL, headers: headers, eventName: "SDK Debug Launch", apiToken: "metrics-1", distinctId: distinctId, properties: properties) { (_) in } + checkIfImplemented(serverURL: serverURL, headers: headers, distinctId: distinctId, properties: properties) UserDefaults.standard.set(debugInitCount, forKey: InternalKeys.mpDebugInitCountKey) UserDefaults.standard.synchronize() } } - private class func checkIfImplemented(serverURL: String, distinctId: String, properties: Properties) { + private class func checkIfImplemented(serverURL: String, headers: [String: String], distinctId: String, properties: Properties) { let hasImplemented: Bool = UserDefaults.standard.bool(forKey: InternalKeys.mpDebugImplementedKey) if !hasImplemented { var completed = 0 @@ -655,8 +684,9 @@ open class MixpanelInstance: CustomDebugStringConvertible, FlushDelegate, AEDele "Aliased": hasAliased, "Used People": hasUsedPeople, ]) {(_,new) in new} + // add headers Network.sendHttpEvent( - serverURL: serverURL, + serverURL: serverURL, headers: headers, eventName: "SDK Implemented", apiToken: "metrics-1", distinctId: distinctId, @@ -955,8 +985,10 @@ extension MixpanelInstance { } let defaultsKey = "trackedKey" if !UserDefaults.standard.bool(forKey: defaultsKey) { - trackingQueue.async { [apiToken, defaultsKey, serverURL] in - Network.sendHttpEvent(serverURL: serverURL, eventName: "Integration", apiToken: "85053bf24bba75239b16a601d9387e17", distinctId: apiToken, updatePeople: false) { [defaultsKey] (success) in + trackingQueue.async { [apiToken, defaultsKey, serverURL, name] in + // add headers + let headers = self.proxyServerDelegate?.mixpanelResourceForProxyServer(name)?.headers ?? [:] + Network.sendHttpEvent(serverURL: serverURL, headers: headers, eventName: "Integration", apiToken: "85053bf24bba75239b16a601d9387e17", distinctId: apiToken, updatePeople: false) { [defaultsKey] (success) in if success { UserDefaults.standard.set(true, forKey: defaultsKey) UserDefaults.standard.synchronize() @@ -1051,7 +1083,7 @@ extension MixpanelInstance { if hasOptedOutTracking() { return } - let proxyServerResource = proxyServerDelegate?.mixpanelResourceForProxyServer(self) + let proxyServerResource = proxyServerDelegate?.mixpanelResourceForProxyServer(name) let headers: [String: String] = proxyServerResource?.headers ?? [:] let queryItems = proxyServerResource?.queryItems ?? [] diff --git a/Sources/Network.swift b/Sources/Network.swift index 6b6a92d7..7e7774ea 100644 --- a/Sources/Network.swift +++ b/Sources/Network.swift @@ -58,8 +58,8 @@ public struct ServerProxyResource { self.headers = headers } - let queryItems: [URLQueryItem]? - let headers: [String: String] + public let queryItems: [URLQueryItem]? + public let headers: [String: String] } class Network { @@ -139,6 +139,7 @@ class Network { } class func sendHttpEvent(serverURL: String, + headers: [String: String], eventName: String, apiToken: String, distinctId: String, @@ -165,10 +166,12 @@ class Network { let requestBody = "ip=1&data=\(requestData)" .data(using: String.Encoding.utf8) + let resourceHeaders: [String: String] = ["Accept-Encoding": "gzip"].merging(headers) {(_,new) in new } + let resource = Network.buildResource(path: FlushType.events.rawValue, method: .post, requestBody: requestBody, - headers: ["Accept-Encoding": "gzip"], + headers: resourceHeaders, parse: responseParser) Network.apiRequest(base: serverURL, @@ -191,11 +194,13 @@ class Network { if updatePeople { let engageData = JSONHandler.encodeAPIData([["$token": apiToken, "$distinct_id": distinctId, "$add": [eventName: 1]] as [String : Any]]) if let engageData = engageData { + let resourceHeaders: [String: String] = ["Accept-Encoding": "gzip"].merging(headers) {(_,new) in new } + let engageBody = "ip=1&data=\(engageData)".data(using: String.Encoding.utf8) let engageResource = Network.buildResource(path: FlushType.people.rawValue, method: .post, requestBody: engageBody, - headers: ["Accept-Encoding": "gzip"], + headers: resourceHeaders, parse: responseParser) Network.apiRequest(base: serverURL, resource: engageResource) { _, _, _ in } success: { _, _ in } }