GDTCORIntegrationTestUploader.m 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. * Copyright 2019 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import "GDTCORTests/Integration/Helpers/GDTCORIntegrationTestUploader.h"
  17. #import <GoogleDataTransport/GDTCORAssert.h>
  18. #import <GoogleDataTransport/GDTCOREvent.h>
  19. #import <GoogleDataTransport/GDTCORRegistrar.h>
  20. #import "GDTCORTests/Integration/Helpers/GDTCORIntegrationTestPrioritizer.h"
  21. #import "GDTCORTests/Integration/TestServer/GDTCORTestServer.h"
  22. @implementation GDTCORIntegrationTestUploader {
  23. /** The current upload task. */
  24. NSURLSessionUploadTask *_currentUploadTask;
  25. /** The server URL to upload to. */
  26. NSURL *_serverURL;
  27. }
  28. - (instancetype)initWithServerURL:(NSURL *)serverURL {
  29. self = [super init];
  30. if (self) {
  31. _serverURL = serverURL;
  32. [[GDTCORRegistrar sharedInstance] registerUploader:self target:kGDTCORIntegrationTestTarget];
  33. }
  34. return self;
  35. }
  36. - (void)uploadPackage:(GDTCORUploadPackage *)package {
  37. GDTCORFatalAssert(!_currentUploadTask,
  38. @"An upload shouldn't be initiated with another in progress.");
  39. NSURL *serverURL = arc4random_uniform(2) ? [_serverURL URLByAppendingPathComponent:@"log"]
  40. : [_serverURL URLByAppendingPathComponent:@"logBatch"];
  41. NSURLSession *session = [NSURLSession sharedSession];
  42. NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:serverURL];
  43. request.HTTPMethod = @"POST";
  44. NSMutableData *uploadData = [[NSMutableData alloc] init];
  45. NSLog(@"Uploading batch of %lu events: ", (unsigned long)[package events].count);
  46. // In real usage, you'd create an instance of whatever request proto your server needs.
  47. for (GDTCOREvent *event in package.events) {
  48. NSData *fileData = [NSData dataWithContentsOfURL:event.fileURL];
  49. GDTCORFatalAssert(fileData, @"An event file shouldn't be empty");
  50. [uploadData appendData:fileData];
  51. }
  52. _currentUploadTask = [session
  53. uploadTaskWithRequest:request
  54. fromData:uploadData
  55. completionHandler:^(NSData *_Nullable data, NSURLResponse *_Nullable response,
  56. NSError *_Nullable error) {
  57. NSLog(@"Batch upload complete.");
  58. // Remove from the prioritizer if there were no errors.
  59. GDTCORFatalAssert(!error, @"There should be no errors uploading events: %@", error);
  60. if (error) {
  61. [package retryDeliveryInTheFuture];
  62. } else {
  63. [package completeDelivery];
  64. }
  65. self->_currentUploadTask = nil;
  66. }];
  67. [_currentUploadTask resume];
  68. }
  69. - (BOOL)readyToUploadTarget:(GDTCORTarget)target conditions:(GDTCORUploadConditions)conditions {
  70. return _currentUploadTask ? NO : YES;
  71. }
  72. @end