GTLRQuery.h 8.7 KB

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