From 3c9a590a7fd4fa60445ba8d65915a41737877b94 Mon Sep 17 00:00:00 2001 From: qingzhuozhen <84748495+qingzhuozhen@users.noreply.github.com> Date: Thu, 21 Oct 2021 16:17:11 -0700 Subject: [PATCH] feat: add server zone for eu dynamic configuration support (#369) --- Amplitude.xcodeproj/project.pbxproj | 46 +++++++++++++++++ Sources/Amplitude/AMPConfigManager.h | 3 +- Sources/Amplitude/AMPConfigManager.m | 6 ++- Sources/Amplitude/AMPConstants.h | 2 + Sources/Amplitude/AMPConstants.m | 2 + Sources/Amplitude/AMPServerZoneUtil.h | 33 +++++++++++++ Sources/Amplitude/AMPServerZoneUtil.m | 63 ++++++++++++++++++++++++ Sources/Amplitude/Amplitude.m | 17 ++++++- Sources/Amplitude/Public/AMPServerZone.h | 39 +++++++++++++++ Sources/Amplitude/Public/Amplitude.h | 16 ++++++ Tests/AmplitudeTests.m | 17 +++++++ Tests/ConfigManagerTests.m | 53 ++++++++++++++++++++ Tests/ServerZoneUtilTests.m | 30 +++++++++++ release.config.js | 2 +- 14 files changed, 324 insertions(+), 5 deletions(-) create mode 100644 Sources/Amplitude/AMPServerZoneUtil.h create mode 100644 Sources/Amplitude/AMPServerZoneUtil.m create mode 100644 Sources/Amplitude/Public/AMPServerZone.h create mode 100644 Tests/ConfigManagerTests.m create mode 100644 Tests/ServerZoneUtilTests.m diff --git a/Amplitude.xcodeproj/project.pbxproj b/Amplitude.xcodeproj/project.pbxproj index ca7a685c..9f5566e8 100644 --- a/Amplitude.xcodeproj/project.pbxproj +++ b/Amplitude.xcodeproj/project.pbxproj @@ -148,6 +148,24 @@ 3E2411F526F9A4E100793829 /* PlanTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 3E2411F026F9A4C600793829 /* PlanTests.m */; }; 3E2411F626F9A4E200793829 /* PlanTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 3E2411F026F9A4C600793829 /* PlanTests.m */; }; 3E2411F726F9A4E400793829 /* PlanTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 3E2411F026F9A4C600793829 /* PlanTests.m */; }; + 3EF608C92720E74D00133703 /* AMPServerZone.h in Headers */ = {isa = PBXBuildFile; fileRef = 3EF608C82720E74D00133703 /* AMPServerZone.h */; settings = {ATTRIBUTES = (Public, ); }; }; + 3EF608CA2720E74D00133703 /* AMPServerZone.h in Headers */ = {isa = PBXBuildFile; fileRef = 3EF608C82720E74D00133703 /* AMPServerZone.h */; settings = {ATTRIBUTES = (Public, ); }; }; + 3EF608CB2720E74D00133703 /* AMPServerZone.h in Headers */ = {isa = PBXBuildFile; fileRef = 3EF608C82720E74D00133703 /* AMPServerZone.h */; settings = {ATTRIBUTES = (Public, ); }; }; + 3EF608CC2720E74D00133703 /* AMPServerZone.h in Headers */ = {isa = PBXBuildFile; fileRef = 3EF608C82720E74D00133703 /* AMPServerZone.h */; settings = {ATTRIBUTES = (Public, ); }; }; + 3EF608D32720F52400133703 /* AMPServerZoneUtil.h in Headers */ = {isa = PBXBuildFile; fileRef = 3EF608D22720F52400133703 /* AMPServerZoneUtil.h */; }; + 3EF608D42720F52400133703 /* AMPServerZoneUtil.h in Headers */ = {isa = PBXBuildFile; fileRef = 3EF608D22720F52400133703 /* AMPServerZoneUtil.h */; }; + 3EF608D52720F52400133703 /* AMPServerZoneUtil.h in Headers */ = {isa = PBXBuildFile; fileRef = 3EF608D22720F52400133703 /* AMPServerZoneUtil.h */; }; + 3EF608D62720F52400133703 /* AMPServerZoneUtil.h in Headers */ = {isa = PBXBuildFile; fileRef = 3EF608D22720F52400133703 /* AMPServerZoneUtil.h */; }; + 3EF608D82720F64500133703 /* AMPServerZoneUtil.m in Sources */ = {isa = PBXBuildFile; fileRef = 3EF608D72720F64500133703 /* AMPServerZoneUtil.m */; }; + 3EF608D92720F64500133703 /* AMPServerZoneUtil.m in Sources */ = {isa = PBXBuildFile; fileRef = 3EF608D72720F64500133703 /* AMPServerZoneUtil.m */; }; + 3EF608DA2720F64500133703 /* AMPServerZoneUtil.m in Sources */ = {isa = PBXBuildFile; fileRef = 3EF608D72720F64500133703 /* AMPServerZoneUtil.m */; }; + 3EF608DB2720F64500133703 /* AMPServerZoneUtil.m in Sources */ = {isa = PBXBuildFile; fileRef = 3EF608D72720F64500133703 /* AMPServerZoneUtil.m */; }; + 3EF608DD27211AC000133703 /* ServerZoneUtilTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 3EF608DC27211AC000133703 /* ServerZoneUtilTests.m */; }; + 3EF608DE27211AC000133703 /* ServerZoneUtilTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 3EF608DC27211AC000133703 /* ServerZoneUtilTests.m */; }; + 3EF608DF27211AC000133703 /* ServerZoneUtilTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 3EF608DC27211AC000133703 /* ServerZoneUtilTests.m */; }; + 3EF608E127211F8A00133703 /* ConfigManagerTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 3EF608E027211F8A00133703 /* ConfigManagerTests.m */; }; + 3EF608E227211F8A00133703 /* ConfigManagerTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 3EF608E027211F8A00133703 /* ConfigManagerTests.m */; }; + 3EF608E327211F8A00133703 /* ConfigManagerTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 3EF608E027211F8A00133703 /* ConfigManagerTests.m */; }; 4A9960EED620179960CF4513 /* libPods-shared-Amplitude_macOSTests.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 38CA8513C0B7F3E7F086F5EB /* libPods-shared-Amplitude_macOSTests.a */; }; 759E939025FBF3DC00BF7C3D /* AMPConfigManager.m in Sources */ = {isa = PBXBuildFile; fileRef = 1279F8C525244D78003DCE07 /* AMPConfigManager.m */; }; 759E939125FBF3DC00BF7C3D /* AMPConstants.m in Sources */ = {isa = PBXBuildFile; fileRef = 1279F8E625244D78003DCE07 /* AMPConstants.m */; }; @@ -290,6 +308,11 @@ 3E2411E626F9A40100793829 /* AMPPlan.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AMPPlan.h; sourceTree = ""; }; 3E2411EB26F9A46500793829 /* AMPPlan.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AMPPlan.m; sourceTree = ""; }; 3E2411F026F9A4C600793829 /* PlanTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PlanTests.m; sourceTree = ""; }; + 3EF608C82720E74D00133703 /* AMPServerZone.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AMPServerZone.h; sourceTree = ""; }; + 3EF608D22720F52400133703 /* AMPServerZoneUtil.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AMPServerZoneUtil.h; sourceTree = ""; }; + 3EF608D72720F64500133703 /* AMPServerZoneUtil.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AMPServerZoneUtil.m; sourceTree = ""; }; + 3EF608DC27211AC000133703 /* ServerZoneUtilTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = ServerZoneUtilTests.m; sourceTree = ""; }; + 3EF608E027211F8A00133703 /* ConfigManagerTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = ConfigManagerTests.m; sourceTree = ""; }; 4B8740756F56A696F6D77342 /* Pods-shared-Amplitude_iOSTests.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-shared-Amplitude_iOSTests.debug.xcconfig"; path = "Target Support Files/Pods-shared-Amplitude_iOSTests/Pods-shared-Amplitude_iOSTests.debug.xcconfig"; sourceTree = ""; }; 4FD7B47E70B1D44AE90B896D /* libPods-shared-Amplitude_iOSTests.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = "libPods-shared-Amplitude_iOSTests.a"; sourceTree = BUILT_PRODUCTS_DIR; }; 69C2CB9AE6CCFC3C4C3D7E7E /* Pods-shared-Amplitude_macOSTests.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-shared-Amplitude_macOSTests.debug.xcconfig"; path = "Target Support Files/Pods-shared-Amplitude_macOSTests/Pods-shared-Amplitude_macOSTests.debug.xcconfig"; sourceTree = ""; }; @@ -426,6 +449,8 @@ 1279FA692525949D003DCE07 /* ISPPinnedNSURLConnectionDelegate.m */, 1279FA6C2525949D003DCE07 /* ISPPinnedNSURLSessionDelegate.h */, 1279FA6B2525949D003DCE07 /* ISPPinnedNSURLSessionDelegate.m */, + 3EF608D22720F52400133703 /* AMPServerZoneUtil.h */, + 3EF608D72720F64500133703 /* AMPServerZoneUtil.m */, ); path = Amplitude; sourceTree = ""; @@ -463,6 +488,8 @@ 12C9732024108DFF00E9CDDB /* AmplitudeTVOSTests.m */, 12C9731624108DFF00E9CDDB /* InvalidCertificationAuthority.der */, 12C9731F24108DFF00E9CDDB /* Info.plist */, + 3EF608DC27211AC000133703 /* ServerZoneUtilTests.m */, + 3EF608E027211F8A00133703 /* ConfigManagerTests.m */, ); path = Tests; sourceTree = ""; @@ -482,6 +509,7 @@ 1279F8C125244D78003DCE07 /* AMPIdentify.h */, 1279F8DA25244D78003DCE07 /* AMPRevenue.h */, 1279F8E925244D78003DCE07 /* AMPTrackingOptions.h */, + 3EF608C82720E74D00133703 /* AMPServerZone.h */, ); path = Public; sourceTree = ""; @@ -554,11 +582,13 @@ 1213D89C24176E4700300E98 /* AmplitudeFramework.h in Headers */, 1279F91225244E8E003DCE07 /* Amplitude+SSLPinning.h in Headers */, 1279FA7B2525949D003DCE07 /* ISPPinnedNSURLSessionDelegate.h in Headers */, + 3EF608CA2720E74D00133703 /* AMPServerZone.h in Headers */, 1279F91825244E8E003DCE07 /* Amplitude.h in Headers */, 1279F91525244E8E003DCE07 /* AMPConstants.h in Headers */, 1279F91925244E8E003DCE07 /* AMPTrackingOptions.h in Headers */, 1279FA752525949D003DCE07 /* ISPPinnedNSURLConnectionDelegate.h in Headers */, 1279F91425244E8E003DCE07 /* AmplitudePrivate.h in Headers */, + 3EF608D42720F52400133703 /* AMPServerZoneUtil.h in Headers */, 1279F92325244E8E003DCE07 /* AMPRevenue.h in Headers */, 1279F91325244E8E003DCE07 /* AMPURLSession.h in Headers */, 1279F91025244E8E003DCE07 /* AMPDeviceInfo.h in Headers */, @@ -580,11 +610,13 @@ 1213D89B24176E4700300E98 /* AmplitudeFramework.h in Headers */, 1279F8F425244E8D003DCE07 /* Amplitude+SSLPinning.h in Headers */, 1279FA7A2525949D003DCE07 /* ISPPinnedNSURLSessionDelegate.h in Headers */, + 3EF608C92720E74D00133703 /* AMPServerZone.h in Headers */, 1279F8FA25244E8D003DCE07 /* Amplitude.h in Headers */, 1279F8F725244E8D003DCE07 /* AMPConstants.h in Headers */, 1279F8FB25244E8D003DCE07 /* AMPTrackingOptions.h in Headers */, 1279FA742525949D003DCE07 /* ISPPinnedNSURLConnectionDelegate.h in Headers */, 1279F8F625244E8D003DCE07 /* AmplitudePrivate.h in Headers */, + 3EF608D32720F52400133703 /* AMPServerZoneUtil.h in Headers */, 1279F90525244E8D003DCE07 /* AMPRevenue.h in Headers */, 1279F8F525244E8D003DCE07 /* AMPURLSession.h in Headers */, 1279F8F225244E8D003DCE07 /* AMPDeviceInfo.h in Headers */, @@ -606,11 +638,13 @@ 1279F93025244E8F003DCE07 /* Amplitude+SSLPinning.h in Headers */, 1279FA7C2525949D003DCE07 /* ISPPinnedNSURLSessionDelegate.h in Headers */, 1279F93625244E8F003DCE07 /* Amplitude.h in Headers */, + 3EF608CB2720E74D00133703 /* AMPServerZone.h in Headers */, 1279F93325244E8F003DCE07 /* AMPConstants.h in Headers */, 1279F93725244E8F003DCE07 /* AMPTrackingOptions.h in Headers */, 1279FA762525949D003DCE07 /* ISPPinnedNSURLConnectionDelegate.h in Headers */, 1279F93225244E8F003DCE07 /* AmplitudePrivate.h in Headers */, 759E943125FBFEA200BF7C3D /* AMPBackgroundNotifier.h in Headers */, + 3EF608D52720F52400133703 /* AMPServerZoneUtil.h in Headers */, 1279F94125244E8F003DCE07 /* AMPRevenue.h in Headers */, 1279F93125244E8F003DCE07 /* AMPURLSession.h in Headers */, 1279F92E25244E8F003DCE07 /* AMPDeviceInfo.h in Headers */, @@ -632,11 +666,13 @@ 759E93AA25FBF44500BF7C3D /* Amplitude.h in Headers */, 759E93AE25FBF44500BF7C3D /* AMPTrackingOptions.h in Headers */, 759E93AD25FBF44500BF7C3D /* AMPRevenue.h in Headers */, + 3EF608CC2720E74D00133703 /* AMPServerZone.h in Headers */, 759E93A925FBF44500BF7C3D /* AMPIdentify.h in Headers */, 759E93B225FBF44500BF7C3D /* ISPCertificatePinning.h in Headers */, 759E93B425FBF44500BF7C3D /* ISPPinnedNSURLSessionDelegate.h in Headers */, 759E93A625FBF44500BF7C3D /* AMPConstants.h in Headers */, 759E93DC25FBF6F700BF7C3D /* AMPBackgroundNotifier.h in Headers */, + 3EF608D62720F52400133703 /* AMPServerZoneUtil.h in Headers */, 759E93B325FBF44500BF7C3D /* ISPPinnedNSURLConnectionDelegate.h in Headers */, 759E93AC25FBF44500BF7C3D /* AmplitudePrivate.h in Headers */, 759E93B025FBF44500BF7C3D /* AMPURLSession.h in Headers */, @@ -989,6 +1025,7 @@ 1279F91D25244E8E003DCE07 /* AMPTrackingOptions.m in Sources */, 1279FA782525949D003DCE07 /* ISPPinnedNSURLSessionDelegate.m in Sources */, 3E2411ED26F9A46500793829 /* AMPPlan.m in Sources */, + 3EF608D92720F64500133703 /* AMPServerZoneUtil.m in Sources */, 1279F91C25244E8E003DCE07 /* AMPRevenue.m in Sources */, 1279F91F25244E8E003DCE07 /* AMPURLSession.m in Sources */, ); @@ -1009,7 +1046,9 @@ 3E2411F626F9A4E200793829 /* PlanTests.m in Sources */, 12C973BD241244E400E9CDDB /* SessionTests.m in Sources */, 12C973AD241244B700E9CDDB /* AmplitudeTests.m in Sources */, + 3EF608E227211F8A00133703 /* ConfigManagerTests.m in Sources */, 12C973C2241244F000E9CDDB /* TrackingOptionsTest.m in Sources */, + 3EF608DE27211AC000133703 /* ServerZoneUtilTests.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -1020,6 +1059,7 @@ 759E941925FBFE7900BF7C3D /* AMPBackgroundNotifier.m in Sources */, 1279F8FE25244E8D003DCE07 /* AMPRevenue.m in Sources */, 1279F90825244E8D003DCE07 /* AMPDatabaseHelper.m in Sources */, + 3EF608D82720F64500133703 /* AMPServerZoneUtil.m in Sources */, 3E2411EC26F9A46500793829 /* AMPPlan.m in Sources */, 1279F8FC25244E8D003DCE07 /* AMPUtils.m in Sources */, 1279F8F125244E8D003DCE07 /* AMPConfigManager.m in Sources */, @@ -1052,7 +1092,9 @@ 3E2411F526F9A4E100793829 /* PlanTests.m in Sources */, 12C973BC241244E300E9CDDB /* SessionTests.m in Sources */, 12C973AC241244B700E9CDDB /* AmplitudeTests.m in Sources */, + 3EF608E127211F8A00133703 /* ConfigManagerTests.m in Sources */, 12C973BF241244EF00E9CDDB /* TrackingOptionsTest.m in Sources */, + 3EF608DD27211AC000133703 /* ServerZoneUtilTests.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -1074,6 +1116,7 @@ 1279F93B25244E8F003DCE07 /* AMPTrackingOptions.m in Sources */, 1279FA792525949D003DCE07 /* ISPPinnedNSURLSessionDelegate.m in Sources */, 3E2411EE26F9A46500793829 /* AMPPlan.m in Sources */, + 3EF608DA2720F64500133703 /* AMPServerZoneUtil.m in Sources */, 1279F93A25244E8F003DCE07 /* AMPRevenue.m in Sources */, 1279F93D25244E8F003DCE07 /* AMPURLSession.m in Sources */, ); @@ -1090,10 +1133,12 @@ 12C973B3241244BF00E9CDDB /* AMPDatabaseHelperTests.m in Sources */, 12C973AB241244AF00E9CDDB /* Amplitude+Test.m in Sources */, 12C973B4241244BF00E9CDDB /* RevenueTests.m in Sources */, + 3EF608E327211F8A00133703 /* ConfigManagerTests.m in Sources */, 12C973C7241244F100E9CDDB /* BaseTestCase.m in Sources */, 12C973BE241244E500E9CDDB /* SessionTests.m in Sources */, 12C973AE241244B800E9CDDB /* AmplitudeTests.m in Sources */, 12C973C5241244F100E9CDDB /* TrackingOptionsTest.m in Sources */, + 3EF608DF27211AC000133703 /* ServerZoneUtilTests.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -1115,6 +1160,7 @@ 759E939A25FBF3DC00BF7C3D /* AMPUtils.m in Sources */, 759E939B25FBF3DC00BF7C3D /* ISPCertificatePinning.m in Sources */, 3E2411EF26F9A46500793829 /* AMPPlan.m in Sources */, + 3EF608DB2720F64500133703 /* AMPServerZoneUtil.m in Sources */, 759E939C25FBF3DC00BF7C3D /* ISPPinnedNSURLConnectionDelegate.m in Sources */, 759E939D25FBF3DC00BF7C3D /* ISPPinnedNSURLSessionDelegate.m in Sources */, ); diff --git a/Sources/Amplitude/AMPConfigManager.h b/Sources/Amplitude/AMPConfigManager.h index f3fb5544..56036634 100644 --- a/Sources/Amplitude/AMPConfigManager.h +++ b/Sources/Amplitude/AMPConfigManager.h @@ -22,6 +22,7 @@ // #import +#import "AMPServerZone.h" NS_ASSUME_NONNULL_BEGIN @@ -30,7 +31,7 @@ NS_ASSUME_NONNULL_BEGIN @property (nonatomic, strong, readonly) NSString *ingestionEndpoint; + (instancetype)sharedInstance; -- (void)refresh:(void(^)(void))completionHandler; +- (void)refresh:(void(^)(void))completionHandler serverZone:(AMPServerZone)serverZone; @end diff --git a/Sources/Amplitude/AMPConfigManager.m b/Sources/Amplitude/AMPConfigManager.m index b0559792..8b61a6df 100644 --- a/Sources/Amplitude/AMPConfigManager.m +++ b/Sources/Amplitude/AMPConfigManager.m @@ -23,6 +23,8 @@ #import "AMPConfigManager.h" #import "AMPConstants.h" +#import "AMPServerZone.h" +#import "AMPServerZoneUtil.h" @interface AMPConfigManager () @@ -48,8 +50,8 @@ - (instancetype)init { return self; } -- (void)refresh:(void(^)(void))completionHandler { - NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:kAMPDyanmicConfigUrl]]; +- (void)refresh:(void(^)(void))completionHandler serverZone:(AMPServerZone)serverZone { + NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:[AMPServerZoneUtil getDynamicConfigApi:serverZone]]]; NSURLSession *session = [NSURLSession sharedSession]; NSURLSessionDataTask *task = [session dataTaskWithRequest:request diff --git a/Sources/Amplitude/AMPConstants.h b/Sources/Amplitude/AMPConstants.h index 3755389d..e1cc0540 100644 --- a/Sources/Amplitude/AMPConstants.h +++ b/Sources/Amplitude/AMPConstants.h @@ -31,8 +31,10 @@ extern NSString *const kAMPPlatform; extern NSString *const kAMPOSName; extern NSString *const kAMPEventLogDomain; extern NSString *const kAMPEventLogUrl; +extern NSString *const kAMPEventLogEuUrl; extern NSString *const kAMPContentTypeHeader; extern NSString *const kAMPDyanmicConfigUrl; +extern NSString *const kAMPDyanmicConfigEuUrl; extern NSString *const kAMPDefaultInstance; extern const int kAMPApiVersion; extern const int kAMPDBVersion; diff --git a/Sources/Amplitude/AMPConstants.m b/Sources/Amplitude/AMPConstants.m index b6fa75ef..37edb7eb 100644 --- a/Sources/Amplitude/AMPConstants.m +++ b/Sources/Amplitude/AMPConstants.m @@ -29,8 +29,10 @@ NSString *const kAMPUnknownVersion = @"unknown-version"; NSString *const kAMPEventLogDomain = @"api2.amplitude.com"; NSString *const kAMPEventLogUrl = @"https://api2.amplitude.com/"; +NSString *const kAMPEventLogEuUrl = @"https://api.eu.amplitude.com/"; NSString *const kAMPContentTypeHeader = @"application/x-www-form-urlencoded"; NSString *const kAMPDyanmicConfigUrl = @"https://regionconfig.amplitude.com/"; +NSString *const kAMPDyanmicConfigEuUrl = @"https://regionconfig.eu.amplitude.com/"; NSString *const kAMPDefaultInstance = @"$default_instance"; const int kAMPApiVersion = 3; const int kAMPDBVersion = 3; diff --git a/Sources/Amplitude/AMPServerZoneUtil.h b/Sources/Amplitude/AMPServerZoneUtil.h new file mode 100644 index 00000000..4453ca0c --- /dev/null +++ b/Sources/Amplitude/AMPServerZoneUtil.h @@ -0,0 +1,33 @@ +// +// AMPServerZoneUtil.h +// Copyright (c) 2021 Amplitude Inc. (https://amplitude.com/) +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. +// + +#import +#import "AMPServerZone.h" + +@interface AMPServerZoneUtil : NSObject + ++ (NSString *)getEventLogApi:(AMPServerZone)serverZone; + ++ (NSString *)getDynamicConfigApi:(AMPServerZone)serverZone; + +@end diff --git a/Sources/Amplitude/AMPServerZoneUtil.m b/Sources/Amplitude/AMPServerZoneUtil.m new file mode 100644 index 00000000..d32ae2d7 --- /dev/null +++ b/Sources/Amplitude/AMPServerZoneUtil.m @@ -0,0 +1,63 @@ +// +// AMPServerZoneUtil.m +// Copyright (c) 2021 Amplitude Inc. (https://amplitude.com/) +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. +// + +#import +#import "AMPServerZone.h" +#import "AMPServerZoneUtil.h" +#import "AMPConstants.h" + +@implementation AMPServerZoneUtil + ++ (NSString *)getEventLogApi:(AMPServerZone)serverZone { + NSString *eventLogUrl = kAMPEventLogUrl; + switch (serverZone) { + case EU: + eventLogUrl = kAMPEventLogEuUrl; + break; + case US: + eventLogUrl = kAMPEventLogUrl; + break; + + default: + break; + } + return eventLogUrl; +} + ++ (NSString *)getDynamicConfigApi:(AMPServerZone)serverZone { + NSString *dynamicConfigUrl = kAMPDyanmicConfigUrl; + switch (serverZone) { + case EU: + dynamicConfigUrl = kAMPDyanmicConfigEuUrl; + break; + case US: + dynamicConfigUrl = kAMPDyanmicConfigUrl; + break; + + default: + break; + } + return dynamicConfigUrl; +} + +@end diff --git a/Sources/Amplitude/Amplitude.m b/Sources/Amplitude/Amplitude.m index 08dc1891..2e725d05 100644 --- a/Sources/Amplitude/Amplitude.m +++ b/Sources/Amplitude/Amplitude.m @@ -60,6 +60,8 @@ #import "AMPRevenue.h" #import "AMPTrackingOptions.h" #import "AMPPlan.h" +#import "AMPServerZone.h" +#import "AMPServerZoneUtil.h" #import #import @@ -135,6 +137,7 @@ @implementation Amplitude { NSString *_serverUrl; NSString *_token; AMPPlan *_plan; + AMPServerZone _serverZone; } #pragma clang diagnostic push @@ -197,6 +200,7 @@ - (instancetype)initWithInstanceName:(NSString *)instanceName { _backoffUpload = NO; _offline = NO; _serverUrl = kAMPEventLogUrl; + _serverZone = US; self.libraryName = kAMPLibrary; self.libraryVersion = kAMPVersion; self.contentTypeHeader = kAMPContentTypeHeader; @@ -835,7 +839,7 @@ - (void)refreshDynamicConfig { return; } strongSelf->_serverUrl = [AMPConfigManager sharedInstance].ingestionEndpoint; - }]; + } serverZone:_serverZone]; } } @@ -1432,6 +1436,17 @@ - (void)setPlan:(AMPPlan *)plan { _plan = plan; } +- (void)setServerZone:(AMPServerZone)serverZone { + [self setServerZone:serverZone updateServerUrl:YES]; +} + +- (void)setServerZone:(AMPServerZone)serverZone updateServerUrl:(BOOL)updateServerUrl { + _serverZone = serverZone; + if (updateServerUrl) { + [self setServerUrl:[AMPServerZoneUtil getEventLogApi:serverZone]]; + } +} + #pragma mark - Getters for device data - (NSString *)getAdSupportID { NSString *result = nil; diff --git a/Sources/Amplitude/Public/AMPServerZone.h b/Sources/Amplitude/Public/AMPServerZone.h new file mode 100644 index 00000000..ba7a8d2a --- /dev/null +++ b/Sources/Amplitude/Public/AMPServerZone.h @@ -0,0 +1,39 @@ +// +// AMPServerZone.h +// Copyright (c) 2021 Amplitude Inc. (https://amplitude.com/) +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. +// +#import +/** + * AMPServerZone is for Data Residency and handling server zone related properties. + * The server zones now are US and EU. + * + * For usage like sending data to Amplitude's EU servers, you need to configure the serverZone + * property after initializing the client with setServerZone method. + */ +typedef NS_ENUM(NSInteger, AMPServerZone); + +#ifndef AMPServerZone_h +#define AMPServerZone_h +typedef NS_ENUM(NSInteger, AMPServerZone) { + US, + EU +}; +#endif diff --git a/Sources/Amplitude/Public/Amplitude.h b/Sources/Amplitude/Public/Amplitude.h index e0ad177e..fc973f55 100644 --- a/Sources/Amplitude/Public/Amplitude.h +++ b/Sources/Amplitude/Public/Amplitude.h @@ -26,6 +26,7 @@ #import "AMPRevenue.h" #import "AMPTrackingOptions.h" #import "AMPPlan.h" +#import "AMPServerZone.h" NS_ASSUME_NONNULL_BEGIN @@ -626,6 +627,9 @@ typedef void (^AMPInitCompletionBlock)(void); /** Sends events to a different URL other than kAMPEventLogUrl. Used for proxy servers + + We now have a new method setServerZone. To send data to Amplitude's EU servers, recommend to use setServerZone + method like [client setServerZone:EU] */ - (void)setServerUrl:(NSString *)serverUrl; @@ -638,6 +642,18 @@ typedef void (^AMPInitCompletionBlock)(void); - (void)setPlan:(AMPPlan *)plan; +/** + * Set Amplitude Server Zone, switch to zone related configuration, including dynamic configuration and server url. + * To send data to Amplitude's EU servers, you need to configure the serverZone to EU like [client setServerZone:EU] + */ +- (void)setServerZone:(AMPServerZone)serverZone; + +/** + * Set Amplitude Server Zone, switch to zone related configuration, including dynamic configuration and server url. + * If updateServerUrl is true, including server url as well. Recommend to keep updateServerUrl to be true for alignment. + */ +- (void)setServerZone:(AMPServerZone)serverZone updateServerUrl:(BOOL)updateServerUrl; + /**----------------------------------------------------------------------------- * @name Other Methods * ----------------------------------------------------------------------------- diff --git a/Tests/AmplitudeTests.m b/Tests/AmplitudeTests.m index 7f8cbf4b..9120e6ec 100644 --- a/Tests/AmplitudeTests.m +++ b/Tests/AmplitudeTests.m @@ -16,6 +16,7 @@ #import "AMPUtils.h" #import "AMPTrackingOptions.h" #import "AMPPlan.h" +#import "AMPServerZone.h" @interface Amplitude (Tests) @@ -1106,4 +1107,20 @@ - (void)testSetPlan { XCTAssertEqualObjects(version, planValue[@"version"]); } +- (void)testSetServerZone { + Amplitude *client = [Amplitude instanceWithName:@"eu_zone"]; + XCTAssertEqualObjects(kAMPEventLogUrl, [client valueForKey:@"serverUrl"]); + [client initializeApiKey:@"eu_api_ket"]; + [client setServerZone:EU]; + XCTAssertEqualObjects(kAMPEventLogEuUrl, [client valueForKey:@"serverUrl"]); +} + +- (void)testSetServerZoneWithoutUpdateServerUrl { + Amplitude *client = [Amplitude instanceWithName:@"eu_zone_2"]; + XCTAssertEqualObjects(kAMPEventLogUrl, [client valueForKey:@"serverUrl"]); + [client initializeApiKey:@"eu_api_ket"]; + [client setServerZone:EU updateServerUrl:NO]; + XCTAssertEqualObjects(kAMPEventLogUrl, [client valueForKey:@"serverUrl"]); +} + @end diff --git a/Tests/ConfigManagerTests.m b/Tests/ConfigManagerTests.m new file mode 100644 index 00000000..be67dbb6 --- /dev/null +++ b/Tests/ConfigManagerTests.m @@ -0,0 +1,53 @@ +// +// ConfigManagerTests.m +// Amplitude +// +// Created by Qingzhuo Zhen on 10/20/21. +// Copyright © 2021 Amplitude. All rights reserved. +// + +#import +#import +#import "AMPConfigManager.h" +#import "AMPServerZone.h" +#import "AMPConstants.h" + +@interface ConfigManagerTests : XCTestCase + +@end + +@implementation ConfigManagerTests + +id _sharedSessionMock; + +- (void)setUp { + [super setUp]; + _sharedSessionMock = [OCMockObject partialMockForObject:[NSURLSession sharedSession]]; +} + +- (void)tearDown { + [_sharedSessionMock stopMocking]; + [super tearDown]; +} + +- (void)setupAsyncResponse: (NSMutableDictionary*) serverResponse { + [[[_sharedSessionMock stub] andDo:^(NSInvocation *invocation) { + void (^handler)(NSURLResponse*, NSData*, NSError*); + [invocation getArgument:&handler atIndex:3]; + handler(serverResponse[@"data"], serverResponse[@"response"], serverResponse[@"error"]); + }] dataTaskWithRequest:OCMOCK_ANY completionHandler:OCMOCK_ANY]; +} + +- (void)testDynamicConfigurationWithEU { + NSMutableDictionary *serverResponse = [NSMutableDictionary dictionaryWithDictionary: + @{ @"response" : [[NSHTTPURLResponse alloc] initWithURL:[NSURL URLWithString:@"/"] statusCode:200 HTTPVersion:nil headerFields:@{}], + @"data" : [@"{\"ingestionEndpoint\": \"api.eu.amplitude.com\"}" dataUsingEncoding:NSUTF8StringEncoding] + }]; + + [self setupAsyncResponse:serverResponse]; + [[AMPConfigManager sharedInstance] refresh:^{ + XCTAssertEqualObjects(kAMPEventLogEuUrl, [[AMPConfigManager sharedInstance].ingestionEndpoint stringByAppendingString:@"/"]); + } serverZone:EU]; +} + +@end diff --git a/Tests/ServerZoneUtilTests.m b/Tests/ServerZoneUtilTests.m new file mode 100644 index 00000000..c3831576 --- /dev/null +++ b/Tests/ServerZoneUtilTests.m @@ -0,0 +1,30 @@ +// +// ServerPlanUtilTests.m +// Amplitude +// +// Created by Qingzhuo Zhen on 10/20/21. +// Copyright © 2021 Amplitude. All rights reserved. +// + +#import +#import "AMPServerZone.h" +#import "AMPServerZoneUtil.h" +#import "AMPConstants.h" + +@interface ServerZoneUtilTests : XCTestCase + +@end + +@implementation ServerZoneUtilTests { } + +- (void) testGetEventLogApi { + XCTAssertEqualObjects(kAMPEventLogUrl, [AMPServerZoneUtil getEventLogApi:US]); + XCTAssertEqualObjects(kAMPEventLogEuUrl, [AMPServerZoneUtil getEventLogApi:EU]); +} + +- (void) testGetDynamicConfigApi { + XCTAssertEqualObjects(kAMPDyanmicConfigUrl, [AMPServerZoneUtil getDynamicConfigApi:US]); + XCTAssertEqualObjects(kAMPDyanmicConfigEuUrl, [AMPServerZoneUtil getDynamicConfigApi:EU]); +} + +@end diff --git a/release.config.js b/release.config.js index b5305895..068cce2e 100644 --- a/release.config.js +++ b/release.config.js @@ -1,5 +1,5 @@ module.exports = { - "branches": ["main", "add-observe-support"], + "branches": ["main", "main-lts"], "plugins": [ ["@semantic-release/commit-analyzer", { "preset": "angular",