GTLRBatchQuery.m 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /* Copyright (c) 2011 Google Inc.
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. //#import <GoogleAPIClientForREST/GTLRBatchQuery.h>
  16. //
  17. //#import <GoogleAPIClientForREST/GTLRService.h>
  18. #import "GTLRBatchQuery.h"
  19. #import "GTLRService.h"
  20. #import "GTLRDefines.h"
  21. #if DEBUG
  22. static void DebugAssertValidBatchQueryItem(GTLRQuery *query) {
  23. GTLR_DEBUG_ASSERT([query isKindOfClass:[GTLRQuery class]],
  24. @"unexpected query class: %@", [query class]);
  25. GTLR_DEBUG_ASSERT(query.uploadParameters == nil,
  26. @"batch may not contain upload: %@", query);
  27. GTLR_DEBUG_ASSERT(!query.hasExecutionParameters,
  28. @"queries added to a batch may not contain executionParameters: %@", query);
  29. GTLR_DEBUG_ASSERT(!query.queryInvalid,
  30. @"batch may not contain query already executed: %@", query);
  31. }
  32. #else
  33. static void DebugAssertValidBatchQueryItem(GTLRQuery *query) { }
  34. #endif
  35. @implementation GTLRBatchQuery {
  36. NSMutableArray<GTLRQuery *> *_queries;
  37. NSMutableDictionary *_requestIDMap;
  38. GTLRServiceExecutionParameters *_executionParameters;
  39. }
  40. @synthesize shouldSkipAuthorization = _shouldSkipAuthorization,
  41. additionalHTTPHeaders = _additionalHTTPHeaders,
  42. additionalURLQueryParameters = _additionalURLQueryParameters,
  43. boundary = _boundary,
  44. loggingName = _loggingName;
  45. + (instancetype)batchQuery {
  46. GTLRBatchQuery *obj = [[self alloc] init];
  47. return obj;
  48. }
  49. + (instancetype)batchQueryWithQueries:(NSArray<GTLRQuery *> *)queries {
  50. GTLRBatchQuery *obj = [self batchQuery];
  51. obj.queries = queries;
  52. #if DEBUG
  53. for (GTLRQuery *query in queries) {
  54. DebugAssertValidBatchQueryItem(query);
  55. }
  56. #endif
  57. return obj;
  58. }
  59. - (id)copyWithZone:(NSZone *)zone {
  60. // Deep copy the list of queries
  61. GTLRBatchQuery *newBatch = [[[self class] allocWithZone:zone] init];
  62. if (_queries) {
  63. newBatch.queries = [[NSArray alloc] initWithArray:_queries
  64. copyItems:YES];
  65. }
  66. // Using the executionParameters ivar avoids creating the object.
  67. newBatch.executionParameters = _executionParameters;
  68. // Copied in the same order as synthesized above.
  69. newBatch.shouldSkipAuthorization = _shouldSkipAuthorization;
  70. newBatch.additionalHTTPHeaders = _additionalHTTPHeaders;
  71. newBatch.additionalURLQueryParameters = _additionalURLQueryParameters;
  72. newBatch.boundary = _boundary;
  73. newBatch.loggingName = _loggingName;
  74. // No need to copy _requestIDMap as it's created on demand.
  75. return newBatch;
  76. }
  77. - (NSString *)description {
  78. NSArray *queries = self.queries;
  79. NSArray *loggingNames = [queries valueForKey:@"loggingName"];
  80. NSMutableSet *dedupedNames = [NSMutableSet setWithArray:loggingNames]; // de-dupe
  81. [dedupedNames removeObject:[NSNull null]]; // In case any didn't have a loggingName.
  82. NSString *namesStr = [[dedupedNames allObjects] componentsJoinedByString:@","];
  83. return [NSString stringWithFormat:@"%@ %p (queries:%lu - %@)",
  84. [self class], self, (unsigned long)queries.count, namesStr];
  85. }
  86. #pragma mark -
  87. - (BOOL)isBatchQuery {
  88. return YES;
  89. }
  90. - (GTLRUploadParameters *)uploadParameters {
  91. // File upload is not supported for batches
  92. return nil;
  93. }
  94. - (void)invalidateQuery {
  95. NSArray *queries = self.queries;
  96. [queries makeObjectsPerformSelector:@selector(invalidateQuery)];
  97. _executionParameters = nil;
  98. }
  99. - (GTLRQuery *)queryForRequestID:(NSString *)requestID {
  100. GTLRQuery *result = [_requestIDMap objectForKey:requestID];
  101. if (result) return result;
  102. // We've not before tried to look up a query, or the map is stale
  103. _requestIDMap = [[NSMutableDictionary alloc] init];
  104. for (GTLRQuery *query in _queries) {
  105. [_requestIDMap setObject:query forKey:query.requestID];
  106. }
  107. result = [_requestIDMap objectForKey:requestID];
  108. return result;
  109. }
  110. #pragma mark -
  111. - (void)setQueries:(NSArray<GTLRQuery *> *)array {
  112. #if DEBUG
  113. for (GTLRQuery *query in array) {
  114. DebugAssertValidBatchQueryItem(query);
  115. }
  116. #endif
  117. _queries = [array mutableCopy];
  118. }
  119. - (NSArray<GTLRQuery *> *)queries {
  120. return _queries;
  121. }
  122. - (void)addQuery:(GTLRQuery *)query {
  123. DebugAssertValidBatchQueryItem(query);
  124. if (_queries == nil) {
  125. _queries = [[NSMutableArray alloc] init];
  126. }
  127. [_queries addObject:query];
  128. }
  129. - (GTLRServiceExecutionParameters *)executionParameters {
  130. @synchronized(self) {
  131. if (!_executionParameters) {
  132. _executionParameters = [[GTLRServiceExecutionParameters alloc] init];
  133. }
  134. }
  135. return _executionParameters;
  136. }
  137. - (void)setExecutionParameters:(GTLRServiceExecutionParameters *)executionParameters {
  138. @synchronized(self) {
  139. _executionParameters = executionParameters;
  140. }
  141. }
  142. - (BOOL)hasExecutionParameters {
  143. return _executionParameters.hasParameters;
  144. }
  145. @end