GTLRObject.h 10 KB

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