diff options
Diffstat (limited to 'platform/ios/src/MGLAPIClient.m')
-rw-r--r-- | platform/ios/src/MGLAPIClient.m | 18 |
1 files changed, 17 insertions, 1 deletions
diff --git a/platform/ios/src/MGLAPIClient.m b/platform/ios/src/MGLAPIClient.m index 31fd39c83d..fb13113c81 100644 --- a/platform/ios/src/MGLAPIClient.m +++ b/platform/ios/src/MGLAPIClient.m @@ -1,5 +1,6 @@ #import "MGLAPIClient.h" #import "NSBundle+MGLAdditions.h" +#import "NSData+MGLAdditions.h" #import "MGLAccountManager.h" static NSString * const MGLAPIClientUserAgentBase = @"MapboxEventsiOS"; @@ -9,6 +10,7 @@ static NSString * const MGLAPIClientEventsPath = @"events/v2"; static NSString * const MGLAPIClientHeaderFieldUserAgentKey = @"User-Agent"; static NSString * const MGLAPIClientHeaderFieldContentTypeKey = @"Content-Type"; static NSString * const MGLAPIClientHeaderFieldContentTypeValue = @"application/json"; +static NSString * const MGLAPIClientHeaderFieldContentEncodingKey = @"Content-Encoding"; static NSString * const MGLAPIClientHTTPMethodPost = @"POST"; @interface MGLAPIClient () @@ -82,8 +84,22 @@ static NSString * const MGLAPIClientHTTPMethodPost = @"POST"; [request setValue:self.userAgent forHTTPHeaderField:MGLAPIClientHeaderFieldUserAgentKey]; [request setValue:MGLAPIClientHeaderFieldContentTypeValue forHTTPHeaderField:MGLAPIClientHeaderFieldContentTypeKey]; [request setHTTPMethod:MGLAPIClientHTTPMethodPost]; + NSData *jsonData = [self serializedDataForEvents:events]; - [request setHTTPBody:jsonData]; + + // Compressing less than 3 events can have a negative impact on the size. + if (events.count > 2) { + NSData *compressedData = [jsonData mgl_compressedData]; + [request setValue:@"deflate" forHTTPHeaderField:MGLAPIClientHeaderFieldContentEncodingKey]; + [request setHTTPBody:compressedData]; + } + + // Set JSON data if events.count were less than 3 or something went wrong with compressing HTTP body data. + if (!request.HTTPBody) { + [request setValue:nil forHTTPHeaderField:MGLAPIClientHeaderFieldContentEncodingKey]; + [request setHTTPBody:jsonData]; + } + return [request copy]; } |