Skip to content

Commit

Permalink
feat: request timeout config (merge #293)
Browse files Browse the repository at this point in the history
  • Loading branch information
YoloMao committed Nov 14, 2023
1 parent a9d6c78 commit 3425430
Show file tree
Hide file tree
Showing 11 changed files with 40 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ - (void)testDefaultConfiguration_Autotracker {
XCTAssertEqual(configuration.encryptEnabled, NO);
XCTAssertEqual(configuration.impressionScale, 0);
XCTAssertEqualObjects(configuration.dataSourceId, nil);
XCTAssertEqualObjects(configuration.networkConfig, nil);
}

- (void)testSetConfiguration_Autotracker {
Expand All @@ -145,6 +146,9 @@ - (void)testSetConfiguration_Autotracker {
config.encryptEnabled = YES;
config.impressionScale = 0.5;
config.dataSourceId = @"12345";
GrowingNetworkConfig *networkConfig = [GrowingNetworkConfig config];
networkConfig.requestTimeoutInSec = 0.3f;
config.networkConfig = networkConfig;
[GrowingRealAutotracker trackerWithConfiguration:config launchOptions:nil];

GrowingAutotrackConfiguration *configuration =
Expand All @@ -163,6 +167,8 @@ - (void)testSetConfiguration_Autotracker {
XCTAssertEqual(configuration.encryptEnabled, YES);
XCTAssertEqual(configuration.impressionScale, 0.5);
XCTAssertEqualObjects(configuration.dataSourceId, @"12345");
XCTAssertNotNil(configuration.networkConfig);
XCTAssertEqual(configuration.networkConfig.requestTimeoutInSec, 0.3f);
}

- (void)testDefaultConfiguration_Tracker {
Expand All @@ -183,6 +189,7 @@ - (void)testDefaultConfiguration_Tracker {
XCTAssertEqualObjects(configuration.urlScheme, nil);
XCTAssertEqual(configuration.encryptEnabled, NO);
XCTAssertEqualObjects(configuration.dataSourceId, nil);
XCTAssertEqualObjects(configuration.networkConfig, nil);
}

- (void)testSetConfiguration_Tracker {
Expand All @@ -200,6 +207,9 @@ - (void)testSetConfiguration_Tracker {
config.urlScheme = @"growing.tracker";
config.encryptEnabled = YES;
config.dataSourceId = @"12345";
GrowingNetworkConfig *networkConfig = [GrowingNetworkConfig config];
networkConfig.requestTimeoutInSec = 0.3f;
config.networkConfig = networkConfig;
[GrowingRealTracker trackerWithConfiguration:config launchOptions:nil];

GrowingTrackConfiguration *configuration = GrowingConfigurationManager.sharedInstance.trackConfiguration;
Expand All @@ -216,6 +226,8 @@ - (void)testSetConfiguration_Tracker {
XCTAssertEqualObjects(configuration.urlScheme, @"growing.tracker");
XCTAssertEqual(configuration.encryptEnabled, YES);
XCTAssertEqualObjects(configuration.dataSourceId, @"12345");
XCTAssertNotNil(configuration.networkConfig);
XCTAssertEqual(configuration.networkConfig.requestTimeoutInSec, 0.3f);
}

- (void)testVersionNameAndVersionCode {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@ - (void)testNSDataGrowingHelper {
NSData *testData = [testString dataUsingEncoding:NSUTF8StringEncoding];
XCTAssertNotNil([testData growingHelper_base64String]);
XCTAssertNotNil([testData growingHelper_utf8String]);
NSString *md5 = [[@"hello world" dataUsingEncoding:NSUTF8StringEncoding] growingHelper_md5String];
XCTAssertEqualObjects(md5, @"5EB63BBBE01EEED093CB22BB8F5ACDC3");
XCTAssertNotNil([testData growingHelper_LZ4String]);
XCTAssertNil([testData growingHelper_jsonObject]);
XCTAssertNil([testData growingHelper_arrayObject]);
Expand Down
3 changes: 0 additions & 3 deletions GrowingTrackerCore/DeepLink/GrowingDeepLinkHandler.m
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@
// limitations under the License.

#import "GrowingTrackerCore/DeepLink/GrowingDeepLinkHandler.h"
#import "GrowingTrackerCore/Helpers/GrowingHelpers.h"
#import "GrowingTrackerCore/Network/Request/GrowingNetworkConfig.h"
#import "GrowingTrackerCore/Thirdparty/Logger/GrowingLogger.h"
#import "GrowingTrackerCore/Utils/GrowingInternalMacros.h"

@interface GrowingDeepLinkHandler ()
Expand Down
2 changes: 2 additions & 0 deletions GrowingTrackerCore/GrowingTrackConfiguration.m
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ - (instancetype)initWithAccountId:(NSString *)accountId {
_idMappingEnabled = NO;
_urlScheme = nil;
_encryptEnabled = NO;
_networkConfig = nil;
_useProtobuf = YES;

// Ads
Expand Down Expand Up @@ -94,6 +95,7 @@ - (id)copyWithZone:(NSZone *)zone {
configuration->_idMappingEnabled = _idMappingEnabled;
configuration->_urlScheme = _urlScheme;
configuration->_encryptEnabled = _encryptEnabled;
configuration->_networkConfig = [_networkConfig copy];
configuration->_useProtobuf = _useProtobuf;

// Ads
Expand Down
11 changes: 9 additions & 2 deletions GrowingTrackerCore/Network/Request/GrowingEventRequest.m
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
#import "GrowingTrackerCore/Manager/GrowingConfigurationManager.h"
#import "GrowingTrackerCore/Network/Request/Adapter/GrowingEventRequestAdapters.h"
#import "GrowingTrackerCore/Network/Request/Adapter/GrowingRequestAdapter.h"
#import "GrowingTrackerCore/Network/Request/GrowingNetworkConfig.h"
#import "GrowingULTimeUtil.h"

@implementation GrowingEventRequest
Expand All @@ -44,7 +43,7 @@ - (GrowingHTTPMethod)method {
}

- (NSURL *)absoluteURL {
NSString *baseUrl = [GrowingNetworkConfig sharedInstance].growingApiHostEnd;
NSString *baseUrl = GrowingConfigurationManager.sharedInstance.trackConfiguration.dataCollectionServerHost;
if (!baseUrl.length) {
return nil;
}
Expand Down Expand Up @@ -75,4 +74,12 @@ - (NSDictionary *)query {
return @{@"stm": stm};
}

- (NSTimeInterval)timeoutInSeconds {
GrowingNetworkConfig *networkConfig = GrowingConfigurationManager.sharedInstance.trackConfiguration.networkConfig;
if (networkConfig && networkConfig.requestTimeoutInSec > 0) {
return networkConfig.requestTimeoutInSec;
}

Check warning on line 81 in GrowingTrackerCore/Network/Request/GrowingEventRequest.m

View check run for this annotation

Codecov / codecov/patch

GrowingTrackerCore/Network/Request/GrowingEventRequest.m#L80-L81

Added lines #L80 - L81 were not covered by tests
return 60.0f;
}

@end
35 changes: 7 additions & 28 deletions GrowingTrackerCore/Network/Request/GrowingNetworkConfig.m
Original file line number Diff line number Diff line change
Expand Up @@ -17,41 +17,20 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#import "GrowingTrackerCore/Network/Request/GrowingNetworkConfig.h"
#import <Foundation/Foundation.h>
#import "GrowingTrackerCore/Helpers/GrowingHelpers.h"
#import "GrowingTrackerCore/Manager/GrowingConfigurationManager.h"
#import "GrowingTrackerCore/Public/GrowingNetworkConfig.h"

@implementation GrowingNetworkConfig

static GrowingNetworkConfig *sharedInstance;

+ (instancetype)sharedInstance {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedInstance = [[self alloc] init];
});
return sharedInstance;
+ (instancetype)config {
return [[GrowingNetworkConfig alloc] init];
}

// 获取url字段
+ (NSString *)absoluteURL {
NSString *baseUrl = [GrowingNetworkConfig sharedInstance].growingApiHostEnd;
if (!baseUrl.length) {
return nil;
}
NSString *absoluteURLString = [baseUrl growingHelper_absoluteURLStringWithPath:self.path andQuery:nil];
return absoluteURLString;
}

+ (NSString *)path {
NSString *accountId = [GrowingConfigurationManager sharedInstance].trackConfiguration.accountId;
NSString *path = [NSString stringWithFormat:@"v3/projects/%@/collect", accountId];
return path;
}

- (NSString *)growingApiHostEnd {
return GrowingConfigurationManager.sharedInstance.trackConfiguration.dataCollectionServerHost;
- (id)copyWithZone:(NSZone *)zone {
GrowingNetworkConfig *config = [[[self class] allocWithZone:zone] init];
config->_requestTimeoutInSec = _requestTimeoutInSec;
return config;
}

@end
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// GrowingConstApi.h
// GrowingNetworkConfig.h
// GrowingAnalytics
//
// Created by GrowingIO on 15/10/27.
Expand All @@ -17,27 +17,12 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef GrowingConstApi_h
#define GrowingConstApi_h

#import <Foundation/Foundation.h>

#define kGrowingEventApiTemplate @"v3/projects/%@/collect?stm=%llu"

#endif /* GrowingConstApi_h */

@interface GrowingNetworkConfig : NSObject

+ (instancetype)sharedInstance;

/// 返回 growingApiHostEnd 拼接的事件上传地址 eg:https://napi.growingio.com/v3/projects/91eaf9b283361032/collect
+ (NSString *)absoluteURL;
@interface GrowingNetworkConfig : NSObject <NSCopying>

/// 返回url path eg:v3/projects/91eaf9b283361032/collect
+ (NSString *)path;
@property (nonatomic, assign) NSTimeInterval requestTimeoutInSec;

/// 返回GrowingTrackConfiguration配置的dataCollectionServerHost,如果没有额外配置该参数的话,默认返回
/// https://napi.growingio.com
- (NSString *)growingApiHostEnd;
+ (instancetype)config;

@end
2 changes: 2 additions & 0 deletions GrowingTrackerCore/Public/GrowingTrackConfiguration.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#import <Foundation/Foundation.h>
#import "GrowingEventFilter.h"
#import "GrowingFieldsIgnore.h"
#import "GrowingNetworkConfig.h"

FOUNDATION_EXPORT NSString *const kGrowingDefaultDataCollectionServerHost;

Expand All @@ -39,6 +40,7 @@ FOUNDATION_EXPORT NSString *const kGrowingDefaultDataCollectionServerHost;
@property (nonatomic, assign) BOOL idMappingEnabled;
@property (nonatomic, copy) NSString *urlScheme;
@property (nonatomic, assign) BOOL encryptEnabled;
@property (nonatomic, copy) GrowingNetworkConfig *networkConfig;
@property (nonatomic, assign) BOOL useProtobuf;

- (instancetype)initWithAccountId:(NSString *)accountId;
Expand Down
1 change: 0 additions & 1 deletion Modules/Advertising/GrowingAdvertising.m
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
#import "GrowingTrackerCore/Helpers/GrowingHelpers.h"
#import "GrowingTrackerCore/Manager/GrowingConfigurationManager.h"
#import "GrowingTrackerCore/Manager/GrowingSession.h"
#import "GrowingTrackerCore/Network/Request/GrowingNetworkConfig.h"
#import "GrowingTrackerCore/Public/GrowingEventNetworkService.h"
#import "GrowingTrackerCore/Public/GrowingServiceManager.h"
#import "GrowingTrackerCore/Thirdparty/Logger/GrowingLogger.h"
Expand Down
7 changes: 4 additions & 3 deletions Modules/MobileDebugger/GrowingMobileDebugger.m
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
#import "GrowingTrackerCore/Manager/GrowingConfigurationManager.h"
#import "GrowingTrackerCore/Menu/GrowingAlert.h"
#import "GrowingTrackerCore/Menu/GrowingStatusBar.h"
#import "GrowingTrackerCore/Network/Request/GrowingNetworkConfig.h"
#import "GrowingTrackerCore/Public/GrowingAnnotationCore.h"
#import "GrowingTrackerCore/Public/GrowingScreenshotService.h"
#import "GrowingTrackerCore/Public/GrowingServiceManager.h"
Expand Down Expand Up @@ -77,10 +76,12 @@ - (instancetype)init {
return self;
}

// 获取url字段
- (NSString *)absoluteURL {
if (!_absoluteURL) {
_absoluteURL = [GrowingNetworkConfig absoluteURL];
GrowingTrackConfiguration *config = GrowingConfigurationManager.sharedInstance.trackConfiguration;
NSString *path = [NSString stringWithFormat:@"v3/projects/%@/collect", config.accountId];
NSString *baseUrl = config.dataCollectionServerHost;
_absoluteURL = [baseUrl growingHelper_absoluteURLStringWithPath:path andQuery:nil];
}
return _absoluteURL;
}
Expand Down
1 change: 0 additions & 1 deletion Modules/WebCircle/GrowingWebCircle.m
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
#import "GrowingTrackerCore/Helpers/GrowingHelpers.h"
#import "GrowingTrackerCore/Manager/GrowingConfigurationManager.h"
#import "GrowingTrackerCore/Menu/GrowingAlert.h"
#import "GrowingTrackerCore/Network/Request/GrowingNetworkConfig.h"
#import "GrowingTrackerCore/Public/GrowingFlutterService.h"
#import "GrowingTrackerCore/Public/GrowingScreenshotService.h"
#import "GrowingTrackerCore/Public/GrowingServiceManager.h"
Expand Down

0 comments on commit 3425430

Please sign in to comment.