GTLRObject.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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. // GTLRObject documentation:
  16. // https://github.com/google/google-api-objectivec-client-for-rest/blob/main/USING.md#objects-and-queries
  17. #import <Foundation/Foundation.h>
  18. #import "GTLRDateTime.h"
  19. #import "GTLRDuration.h"
  20. NS_ASSUME_NONNULL_BEGIN
  21. /**
  22. * Protocol that can be implemented to provide custom logic for what class
  23. * should be created out of the given JSON.
  24. */
  25. @protocol GTLRObjectClassResolver <NSObject>
  26. - (Class)classForJSON:(NSDictionary *)json
  27. defaultClass:(Class)defaultClass;
  28. @end
  29. /**
  30. * Standard GTLRObjectClassResolver used by the core library.
  31. */
  32. @interface GTLRObjectClassResolver : NSObject<GTLRObjectClassResolver>
  33. /**
  34. * Returns a resolver that will look up the 'kind' properties to find classes
  35. * based on the JSON.
  36. *
  37. * The generated service classes provide a +kindStringToClassMap method for any
  38. * mappings that were found from discovery when generating the service.
  39. */
  40. + (instancetype)resolverWithKindMap:(NSDictionary<NSString *, Class> *)kindStringToClassMap;
  41. /**
  42. * Returns a resolver that will look up the 'kind' properties to find classes
  43. * based on the JSON and then applies mapping of surrogate classes to swap out
  44. * specific classes.
  45. *
  46. * Surrogates are subclasses to be instantiated instead of standard classes
  47. * when creating objects from the JSON. For example, this code will, for one query's
  48. * execution, swap a service's default resolver for one that will then use
  49. * MyCalendarEventSubclass instead of GTLRCalendarEvent and
  50. * MyCalendarReminderSubclass instead of GTLRCalendarReminder.
  51. *
  52. * @code
  53. * NSDictionary *surrogates = @{
  54. * [GTLRCalendarEvent class] : [MyCalendarEventSubclass class]
  55. * [GTLRCalendarReminder class] : [MyCalendarReminderSubclass class],
  56. * };
  57. * NSDictionary *serviceKindMap = [[calendarService class] kindStringToClassMap];
  58. * GTLRObjectClassResolver *updatedResolver =
  59. * [GTLRObjectClassResolver resolverWithKindMap:serviceKindMap
  60. * surrogates:surrogates];
  61. * query.executionParameters.objectClassResolver = updatedResolver;
  62. * @endcode
  63. *
  64. * @note To install surrogates for all queries executed by the service, use
  65. * the service's @c -setSurrogates method.
  66. */
  67. + (instancetype)resolverWithKindMap:(NSDictionary<NSString *, Class> *)kindStringToClassMap
  68. surrogates:(NSDictionary<Class, Class> *)surrogates;
  69. @end
  70. /**
  71. * @c GTLRObject serves as the common superclass for classes wrapping JSON, errors, and other data
  72. * passed in server requests and responses.
  73. *
  74. * @note This class is @em not safe for simultaneous use from multiple threads. Applications should
  75. * serialize or protect access to a @c GTLRObject instance as they would for any standard
  76. * Cocoa mutable container.
  77. */
  78. @interface GTLRObject : NSObject <NSCopying, NSSecureCoding>
  79. /**
  80. * The JSON underlying the property values for this object.
  81. *
  82. * The JSON should be accessed or set using the generated properties of a
  83. * class derived from GTLRObject or with the methods @c setJSONValue:forKey:
  84. * and @c JSONValueForKey:
  85. *
  86. * @note: Applications should use @c additionalPropertyForKey: when accessing
  87. * API object properties that do not have generated @c \@property accessors.
  88. */
  89. @property(nonatomic, strong, nullable) NSMutableDictionary *JSON;
  90. /**
  91. * A dictionary retained by the object for the convenience of the client application.
  92. *
  93. * A client application may use this to retain any dictionary.
  94. *
  95. * The values of the user properties dictionary will not be sent to the server during
  96. * query execution, and will not be copied by NSCopying or encoded by NSSecureCoding.
  97. */
  98. @property(nonatomic, strong) NSDictionary *userProperties;
  99. /////////////////////////////////////////////////////////////////////////////////////////////
  100. //
  101. // Public methods
  102. //
  103. // These methods are intended for users of the library
  104. //
  105. /////////////////////////////////////////////////////////////////////////////////////////////
  106. /**
  107. * Constructor for an empty object.
  108. */
  109. + (instancetype)object;
  110. /**
  111. * Constructor for an object including JSON.
  112. */
  113. + (instancetype)objectWithJSON:(nullable NSDictionary *)dict;
  114. /**
  115. * Constructor for an object including JSON and providing a resolver to help
  116. * select the correct classes for sub objects within the json.
  117. *
  118. * The generated services provide a default resolver (-objectClassResolver)
  119. * that covers the kinds for that service. They also expose the kind mappings
  120. * via the +kindStringToClassMap method.
  121. */
  122. + (instancetype)objectWithJSON:(nullable NSDictionary *)dict
  123. objectClassResolver:(id<GTLRObjectClassResolver>)objectClassResolver;
  124. /**
  125. * The JSON for the object, or an empty string if there is no JSON or if the JSON
  126. * dictionary cannot be represented as JSON.
  127. */
  128. - (NSString *)JSONString;
  129. /**
  130. * Generic access for setting entries in the JSON dictionary. This creates the JSON dictionary
  131. * if necessary.
  132. *
  133. * @note: Applications should use @c setAdditionalProperty:forKey: when setting
  134. * API object properties that do not have generated @c \@property accessors.
  135. */
  136. - (void)setJSONValue:(nullable id)obj forKey:(nonnull NSString *)key;
  137. /**
  138. * Generic access to the JSON dictionary.
  139. *
  140. * @note: Applications should use @c additionalPropertyForKey: when accessing
  141. * API object properties that do not have generated @c \@property accessors.
  142. */
  143. - (nullable id)JSONValueForKey:(NSString *)key;
  144. /**
  145. * The list of keys in this object's JSON that are not listed as properties on the object.
  146. */
  147. - (nullable NSArray<NSString *> *)additionalJSONKeys;
  148. /**
  149. * Setter for any key in the JSON that is not listed as a @c \@property in the class declaration.
  150. */
  151. - (void)setAdditionalProperty:(id)obj forName:(NSString *)name;
  152. /**
  153. * Accessor for any key in the JSON that is not listed as a @c \@property in the class
  154. * declaration.
  155. */
  156. - (nullable id)additionalPropertyForName:(NSString *)name;
  157. /**
  158. * A dictionary of all keys in the JSON that is not listed as a @c \@property in the class
  159. * declaration.
  160. */
  161. - (NSDictionary<NSString *, id> *)additionalProperties;
  162. /**
  163. * A string for a partial query describing the fields present.
  164. *
  165. * @note Only the first element of any array is examined.
  166. *
  167. * @see https://developers.google.com/google-apps/tasks/performance?csw=1#partial
  168. *
  169. * @return A @c fields string describing the fields present in the object.
  170. */
  171. - (NSString *)fieldsDescription;
  172. /**
  173. * An object containing only the changes needed to do a partial update (patch),
  174. * where the patch would be to change an object from the original to the receiver,
  175. * such as
  176. * @c GTLRSomeObject *patchObject = [newVersion patchObjectFromOriginal:oldVersion];
  177. *
  178. * @note This method returns nil if there are no changes between the original and the receiver.
  179. *
  180. * @see https://developers.google.com/google-apps/tasks/performance?csw=1#patch
  181. *
  182. * @param original The original object from which to create the patch object.
  183. *
  184. * @return The object used for the patch body.
  185. */
  186. - (nullable id)patchObjectFromOriginal:(GTLRObject *)original;
  187. /**
  188. * A null value to set object properties for patch queries that delete fields.
  189. *
  190. * Do not use this except when setting an object property for a patch query.
  191. *
  192. * @return The null value object.
  193. */
  194. + (id)nullValue;
  195. #pragma mark Internal
  196. ///////////////////////////////////////////////////////////////////////////////
  197. //
  198. // Protected methods
  199. //
  200. // These methods are intended for subclasses of GTLRObject
  201. //
  202. // Creation of objects from a JSON dictionary. The class created depends on
  203. // the content of the JSON, not the class messaged.
  204. + (nullable GTLRObject *)objectForJSON:(NSMutableDictionary *)json
  205. defaultClass:(nullable Class)defaultClass
  206. objectClassResolver:(id<GTLRObjectClassResolver>)objectClassResolver;
  207. // Property-to-key mapping (for JSON keys which are not used as method names)
  208. + (nullable NSDictionary<NSString *, NSString *> *)propertyToJSONKeyMap;
  209. // property-to-Class mapping for array properties (to say what is in the array)
  210. + (nullable NSDictionary<NSString *, Class> *)arrayPropertyToClassMap;
  211. // The default class for additional JSON keys
  212. + (nullable Class)classForAdditionalProperties;
  213. // Indicates if a "kind" property on this class can be used for the class
  214. // registry or if it appears to be non standard.
  215. + (BOOL)isKindValidForClassRegistry;
  216. @end
  217. /**
  218. * Collection results have a property containing an array of @c GTLRObject
  219. *
  220. * This provides support for @c NSFastEnumeration and for indexed subscripting to
  221. * access the objects in the array.
  222. */
  223. @interface GTLRCollectionObject : GTLRObject<NSFastEnumeration>
  224. /**
  225. * The property name that holds the collection.
  226. *
  227. * @return The key for the property holding the array of @c GTLRObject items.
  228. */
  229. + (NSString *)collectionItemsKey;
  230. // objectAtIndexedSubscript: will throw if the index is out of bounds (like
  231. // NSArray does).
  232. - (nullable id)objectAtIndexedSubscript:(NSUInteger)idx;
  233. @end
  234. /**
  235. * A GTLRDataObject holds media data and the MIME type of the data returned by a media
  236. * download query.
  237. *
  238. * The JSON for the object may be nil.
  239. */
  240. @interface GTLRDataObject : GTLRObject
  241. /**
  242. * The downloaded media data.
  243. */
  244. @property(atomic, strong) NSData *data;
  245. /**
  246. * The MIME type of the downloaded media data.
  247. */
  248. @property(atomic, copy) NSString *contentType;
  249. @end
  250. /**
  251. * Base class used when a service method directly returns an array instead
  252. * of a JSON object. This exists for the methods not up to spec.
  253. */
  254. @interface GTLRResultArray : GTLRCollectionObject
  255. /**
  256. * This method should only be called by subclasses.
  257. */
  258. - (nullable NSArray *)itemsWithItemClass:(Class)itemClass;
  259. @end
  260. // ----------------------------------------------------------------------------
  261. /**
  262. * Helper to call the resolver and find the class to use for the given JSON.
  263. * Intended for internal library use only.
  264. */
  265. Class GTLRObjectResolveClass(
  266. id<GTLRObjectClassResolver> objectClassResolver,
  267. NSDictionary *json,
  268. Class defaultClass);
  269. // ----------------------------------------------------------------------------
  270. // Version marker used to validate the generated sources against the library
  271. // version. The will be changed any time the library makes a change that means
  272. // sources need to be regenerated.
  273. #define GTLR_RUNTIME_VERSION 3000
  274. // ----------------------------------------------------------------------------
  275. NS_ASSUME_NONNULL_END