GTLRQuery.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. // Query documentation:
  16. // https://github.com/google/google-api-objectivec-client-for-rest/blob/main/USING.md#query-operations
  17. #import "GTLRObject.h"
  18. #import "GTLRUploadParameters.h"
  19. NS_ASSUME_NONNULL_BEGIN
  20. @class GTLRServiceTicket;
  21. @class GTLRServiceExecutionParameters;
  22. /**
  23. * This protocol is just to support passing of either a batch or a single query
  24. * to a GTLRService instance. The library does not expect or support client app
  25. * implementations of this protocol.
  26. */
  27. @protocol GTLRQueryProtocol <NSObject, NSCopying>
  28. /**
  29. * Service ticket values may be set in the execution parameters for an individual query
  30. * prior to executing the query.
  31. */
  32. @property(atomic, strong, null_resettable) GTLRServiceExecutionParameters *executionParameters;
  33. - (BOOL)isBatchQuery;
  34. - (BOOL)hasExecutionParameters;
  35. - (BOOL)shouldSkipAuthorization;
  36. - (void)invalidateQuery;
  37. - (nullable NSDictionary<NSString *, NSString *> *)additionalHTTPHeaders;
  38. - (nullable NSDictionary<NSString *, NSString *> *)additionalURLQueryParameters;
  39. - (nullable NSString *)loggingName;
  40. - (nullable GTLRUploadParameters *)uploadParameters;
  41. @end
  42. @protocol GTLRQueryCollectionProtocol
  43. @optional
  44. @property(nonatomic, strong) NSString *pageToken;
  45. @end
  46. /**
  47. * A block called when a query completes executing.
  48. *
  49. * Errors passed to the completionBlock will have an "underlying" GTLRErrorObject
  50. * when the server returned an error for this specific query:
  51. *
  52. * GTLRErrorObject *errorObj = [GTLRErrorObject underlyingObjectForError:callbackError];
  53. * if (errorObj) {
  54. * // The server returned this error for this specific query.
  55. * } else {
  56. * // The query execution fetch failed.
  57. * }
  58. *
  59. * @param callbackTicket The ticket that tracked query execution.
  60. * @param object The result of query execution. This will be derived from
  61. * GTLRObject.
  62. * @param callbackError If non-nil, the query execution failed.
  63. */
  64. typedef void (^GTLRQueryCompletionBlock)(GTLRServiceTicket *callbackTicket,
  65. id _Nullable object,
  66. NSError * _Nullable callbackError);
  67. /**
  68. * Class for a single query.
  69. */
  70. @interface GTLRQuery : NSObject <GTLRQueryProtocol, NSCopying>
  71. /**
  72. * The object to be uploaded with the query. The JSON of this object becomes
  73. * the body for PUT and POST requests.
  74. */
  75. @property(atomic, strong, nullable) GTLRObject *bodyObject;
  76. /**
  77. * Each query must have a request ID string. The client app may replace the
  78. * default assigned request ID with a custom string, provided that if
  79. * used in a batch query, all request IDs in the batch must be unique.
  80. */
  81. @property(atomic, copy) NSString *requestID;
  82. /**
  83. * For queries which support file upload, the MIME type and file URL
  84. * or data must be provided.
  85. */
  86. @property(atomic, copy, nullable) GTLRUploadParameters *uploadParameters;
  87. /**
  88. * Any additional URL query parameters for this query.
  89. *
  90. * These query parameters override the same keys from the service object's
  91. * additionalURLQueryParameters
  92. */
  93. @property(atomic, copy, nullable) NSDictionary<NSString *, NSString *> *additionalURLQueryParameters;
  94. /**
  95. * Any additional HTTP headers for this query.
  96. *
  97. * These headers override the same keys from the service object's additionalHTTPHeaders
  98. */
  99. @property(atomic, copy, nullable) NSDictionary<NSString *, NSString *> *additionalHTTPHeaders;
  100. /**
  101. * If set, when the query is executed, an @c "alt" query parameter is added
  102. * with this value and the raw result of the query is returned in a
  103. * GTLRDataObject. This is useful when the server documents result datatypes
  104. * other than JSON ("csv", for example).
  105. */
  106. @property(atomic, copy) NSString *downloadAsDataObjectType;
  107. /**
  108. * If set, and the query also has a non-empty @c downloadAsDataObjectType, the
  109. * URL to download from will be modified to include "download/". This extra path
  110. * component avoids the need for a server redirect to the download URL.
  111. */
  112. @property(atomic, assign) BOOL useMediaDownloadService;
  113. /**
  114. * Clients may set this to YES to disallow authorization. Defaults to NO.
  115. */
  116. @property(atomic, assign) BOOL shouldSkipAuthorization;
  117. /**
  118. * An optional callback block to be called immediately before the executeQuery: completion handler.
  119. *
  120. * The completionBlock property is particularly useful for queries executed in a batch.
  121. */
  122. @property(atomic, copy, nullable) GTLRQueryCompletionBlock completionBlock;
  123. /**
  124. * The brief string to identify this query in GTMSessionFetcher http logs.
  125. *
  126. * A default logging name is set by the code generator, but may be overridden by the client app.
  127. */
  128. @property(atomic, copy, nullable) NSString *loggingName;
  129. #pragma mark Internal
  130. /////////////////////////////////////////////////////////////////////////////////////////////
  131. //
  132. // Properties below are used by the library and aren't typically needed by client apps.
  133. //
  134. /////////////////////////////////////////////////////////////////////////////////////////////
  135. /**
  136. * The URITemplate path segment. This is initialized in by the service generator.
  137. */
  138. @property(atomic, readonly) NSString *pathURITemplate;
  139. /**
  140. * The HTTP method to use for this query. This is initialized in by the service generator.
  141. */
  142. @property(atomic, readonly, nullable) NSString *httpMethod;
  143. /**
  144. * The parameters names that are in the URI Template.
  145. * This is initialized in by the service generator.
  146. *
  147. * The service generator collects these via the discovery info instead of having to parse the
  148. * template to figure out what is part of the path.
  149. */
  150. @property(atomic, readonly, nullable) NSArray<NSString *> *pathParameterNames;
  151. /**
  152. * The JSON dictionary of all the parameters set on this query.
  153. *
  154. * The JSON values are set by setting the query's properties.
  155. */
  156. @property(nonatomic, strong, nullable) NSMutableDictionary<NSString *, id> *JSON;
  157. /**
  158. * A custom URI template for resumable uploads. This is initialized by the service generator
  159. * if needed.
  160. */
  161. @property(atomic, copy, nullable) NSString *resumableUploadPathURITemplateOverride;
  162. /**
  163. * A custom URI template for simple and multipart media uploads. This is initialized
  164. * by the service generator.
  165. */
  166. @property(atomic, copy, nullable) NSString *simpleUploadPathURITemplateOverride;
  167. /**
  168. * The GTLRObject subclass expected for results. This is initialized by the service generator.
  169. *
  170. * This is needed if the object returned by the server lacks a known "kind" string.
  171. */
  172. @property(atomic, assign, nullable) Class expectedObjectClass;
  173. /**
  174. * Set when the query has been invalidated, meaning it was slated for execution so it's been copied
  175. * and its callbacks were released, or it's a copy that has finished executing.
  176. *
  177. * Once a query has been invalidated, it cannot be executed, added to a batch, or copied.
  178. */
  179. @property(atomic, assign, getter=isQueryInvalid) BOOL queryInvalid;
  180. /**
  181. * Internal query init method.
  182. *
  183. * @param pathURITemplate URI template to be filled in with parameters.
  184. * @param httpMethod The requests's http method. A nil method will execute as GET.
  185. * @param pathParameterNames Names of parameters to be replaced in the template.
  186. */
  187. - (instancetype)initWithPathURITemplate:(NSString *)pathURITemplate
  188. HTTPMethod:(nullable NSString *)httpMethod
  189. pathParameterNames:(nullable NSArray<NSString *> *)pathParameterNames NS_DESIGNATED_INITIALIZER;
  190. /**
  191. * @return Auto-generated request ID string.
  192. */
  193. + (NSString *)nextRequestID;
  194. /**
  195. * Overridden by subclasses.
  196. *
  197. * @return Substitute parameter names where needed for Objective-C or library compatibility.
  198. */
  199. + (nullable NSDictionary<NSString *, NSString *> *)parameterNameMap;
  200. /**
  201. * Overridden by subclasses.
  202. *
  203. * @return Map of property keys to specifying the class of objects to be instantiated in arrays.
  204. */
  205. + (nullable NSDictionary<NSString *, Class> *)arrayPropertyToClassMap;
  206. - (instancetype)init NS_UNAVAILABLE;
  207. @end
  208. /**
  209. * The library doesn't use GTLRQueryCollectionImpl, but it provides a concrete implementation
  210. * of the protocol so the methods do not cause private method errors in Xcode/AppStore review.
  211. */
  212. @interface GTLRQueryCollectionImpl : GTLRQuery <GTLRQueryCollectionProtocol>
  213. @end
  214. NS_ASSUME_NONNULL_END