RLMJSONModels.m 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. ////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright 2017 Realm Inc.
  4. //
  5. // Licensed under the Apache License, Version 2.0 (the "License");
  6. // you may not use this file except in compliance with the License.
  7. // You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. //
  17. ////////////////////////////////////////////////////////////////////////////
  18. #import "RLMJSONModels.h"
  19. #import "RLMSyncUtil_Private.h"
  20. #import "RLMSyncUser.h"
  21. #pragma mark - Constants
  22. static const NSString *const kRLMSyncAccessTokenKey = @"access_token";
  23. static const NSString *const kRLMSyncAccountsKey = @"accounts";
  24. static const NSString *const kRLMSyncErrorCodeKey = @"code";
  25. static const NSString *const kRLMSyncExpiresKey = @"expires";
  26. static const NSString *const kRLMSyncErrorHintKey = @"hint";
  27. static const NSString *const kRLMSyncIdKey = @"id";
  28. static const NSString *const kRLMSyncKeyKey = @"key";
  29. static const NSString *const kRLMSyncMetadataKey = @"metadata";
  30. static const NSString *const kRLMSyncRefreshTokenKey = @"refresh_token";
  31. static const NSString *const kRLMSyncErrorStatusKey = @"status";
  32. static const NSString *const kRLMSyncErrorTitleKey = @"title";
  33. static const NSString *const kRLMSyncTokenDataKey = @"token_data";
  34. static const NSString *const kRLMSyncUserKey = @"user";
  35. static const NSString *const kRLMSyncValueKey = @"value";
  36. #pragma mark - RLMTokenDataModel
  37. @interface RLMTokenDataModel ()
  38. @property (nonatomic, readwrite) NSString *identity;
  39. @property (nonatomic, readwrite) NSString *appID;
  40. @property (nonatomic, readwrite) NSString *path;
  41. @property (nonatomic, readwrite) NSTimeInterval expires;
  42. @property (nonatomic, readwrite) BOOL isAdmin;
  43. //@property (nonatomic, readwrite) NSArray *access;
  44. @end
  45. @implementation RLMTokenDataModel
  46. - (instancetype)initWithDictionary:(NSDictionary *)jsonDictionary {
  47. if (self = [super init]) {
  48. self.isAdmin = NO;
  49. RLM_SYNC_PARSE_STRING_OR_ABORT(jsonDictionary, kRLMSyncIdentityKey, identity);
  50. RLM_SYNC_PARSE_OPTIONAL_STRING(jsonDictionary, kRLMSyncAppIDKey, appID);
  51. RLM_SYNC_PARSE_OPTIONAL_STRING(jsonDictionary, kRLMSyncPathKey, path);
  52. RLM_SYNC_PARSE_OPTIONAL_BOOL(jsonDictionary, kRLMSyncIsAdminKey, isAdmin);
  53. RLM_SYNC_PARSE_DOUBLE_OR_ABORT(jsonDictionary, kRLMSyncExpiresKey, expires);
  54. return self;
  55. }
  56. return nil;
  57. }
  58. @end
  59. #pragma mark - RLMTokenModel
  60. @interface RLMTokenModel ()
  61. @property (nonatomic, readwrite) NSString *token;
  62. @property (nonatomic, nullable, readwrite) NSString *path;
  63. @property (nonatomic, readwrite) RLMTokenDataModel *tokenData;
  64. @end
  65. @implementation RLMTokenModel
  66. - (instancetype)initWithDictionary:(NSDictionary *)jsonDictionary {
  67. if (self = [super init]) {
  68. RLM_SYNC_PARSE_STRING_OR_ABORT(jsonDictionary, kRLMSyncTokenKey, token);
  69. RLM_SYNC_PARSE_OPTIONAL_STRING(jsonDictionary, kRLMSyncPathKey, path);
  70. RLM_SYNC_PARSE_MODEL_OR_ABORT(jsonDictionary, kRLMSyncTokenDataKey, RLMTokenDataModel, tokenData);
  71. return self;
  72. }
  73. return nil;
  74. }
  75. @end
  76. #pragma mark - RLMAuthResponseModel
  77. @interface RLMAuthResponseModel ()
  78. @property (nonatomic, readwrite) RLMTokenModel *accessToken;
  79. @property (nonatomic, readwrite) RLMTokenModel *refreshToken;
  80. @end
  81. @implementation RLMAuthResponseModel
  82. - (instancetype)initWithDictionary:(NSDictionary *)jsonDictionary
  83. requireAccessToken:(BOOL)requireAccessToken
  84. requireRefreshToken:(BOOL)requireRefreshToken {
  85. if (self = [super init]) {
  86. // Get the access token.
  87. if (requireAccessToken) {
  88. RLM_SYNC_PARSE_MODEL_OR_ABORT(jsonDictionary, kRLMSyncAccessTokenKey, RLMTokenModel, accessToken);
  89. } else {
  90. RLM_SYNC_PARSE_OPTIONAL_MODEL(jsonDictionary, kRLMSyncAccessTokenKey, RLMTokenModel, accessToken);
  91. }
  92. // Get the refresh token.
  93. if (requireRefreshToken) {
  94. RLM_SYNC_PARSE_MODEL_OR_ABORT(jsonDictionary, kRLMSyncRefreshTokenKey, RLMTokenModel, refreshToken);
  95. } else {
  96. RLM_SYNC_PARSE_OPTIONAL_MODEL(jsonDictionary, kRLMSyncRefreshTokenKey, RLMTokenModel, refreshToken);
  97. }
  98. return self;
  99. }
  100. return nil;
  101. }
  102. @end
  103. #pragma mark - RLMUserInfoResponseModel
  104. @interface RLMSyncUserAccountInfo ()
  105. @property (nonatomic, readwrite) NSString *provider;
  106. @property (nonatomic, readwrite) NSString *providerUserIdentity;
  107. @end
  108. @implementation RLMSyncUserAccountInfo
  109. - (instancetype)initWithDictionary:(NSDictionary *)jsonDictionary {
  110. if (self = [super init]) {
  111. RLM_SYNC_PARSE_STRING_OR_ABORT(jsonDictionary, kRLMSyncProviderKey, provider);
  112. RLM_SYNC_PARSE_STRING_OR_ABORT(jsonDictionary, kRLMSyncProviderIDKey, providerUserIdentity);
  113. return self;
  114. }
  115. return nil;
  116. }
  117. @end
  118. @interface RLMUserResponseModel ()
  119. @property (nonatomic, readwrite) NSString *identity;
  120. @property (nonatomic, readwrite) NSArray *accounts;
  121. @property (nonatomic, readwrite) NSDictionary *metadata;
  122. @property (nonatomic, readwrite) BOOL isAdmin;
  123. @end
  124. @implementation RLMUserResponseModel
  125. - (void)parseMetadataFromJSON:(NSDictionary *)jsonDictionary {
  126. NSMutableDictionary *buffer = [NSMutableDictionary dictionary];
  127. NSArray *metadataArray = jsonDictionary[kRLMSyncMetadataKey];
  128. if (![metadataArray isKindOfClass:[NSArray class]]) {
  129. self.metadata = @{};
  130. return;
  131. }
  132. for (NSDictionary *object in metadataArray) {
  133. if (![object isKindOfClass:[NSDictionary class]]) {
  134. continue;
  135. }
  136. NSString *key = object[kRLMSyncKeyKey];
  137. NSString *value = object[kRLMSyncValueKey];
  138. if (!key || !value) {
  139. continue;
  140. }
  141. buffer[key] = value;
  142. }
  143. self.metadata = [buffer copy];
  144. }
  145. - (instancetype)initWithDictionary:(NSDictionary *)jsonDictionary {
  146. if (self = [super init]) {
  147. self.isAdmin = NO;
  148. RLM_SYNC_PARSE_STRING_OR_ABORT(jsonDictionary, kRLMSyncUserIDKey, identity);
  149. RLM_SYNC_PARSE_OPTIONAL_BOOL(jsonDictionary, kRLMSyncIsAdminKey, isAdmin);
  150. RLM_SYNC_PARSE_MODEL_ARRAY_OR_ABORT(jsonDictionary, kRLMSyncAccountsKey, RLMSyncUserAccountInfo, accounts);
  151. [self parseMetadataFromJSON:jsonDictionary];
  152. return self;
  153. }
  154. return nil;
  155. }
  156. @end
  157. #pragma mark - RLMSyncErrorResponseModel
  158. @interface RLMSyncErrorResponseModel ()
  159. @property (nonatomic, readwrite) NSInteger status;
  160. @property (nonatomic, readwrite) NSInteger code;
  161. @property (nonatomic, readwrite) NSString *title;
  162. @property (nonatomic, readwrite) NSString *hint;
  163. @end
  164. @implementation RLMSyncErrorResponseModel
  165. - (instancetype)initWithDictionary:(NSDictionary *)jsonDictionary {
  166. if (self = [super init]) {
  167. RLM_SYNC_PARSE_DOUBLE_OR_ABORT(jsonDictionary, kRLMSyncErrorStatusKey, status);
  168. RLM_SYNC_PARSE_DOUBLE_OR_ABORT(jsonDictionary, kRLMSyncErrorCodeKey, code);
  169. RLM_SYNC_PARSE_OPTIONAL_STRING(jsonDictionary, kRLMSyncErrorTitleKey, title);
  170. RLM_SYNC_PARSE_OPTIONAL_STRING(jsonDictionary, kRLMSyncErrorHintKey, hint);
  171. NSString *detail = jsonDictionary[@"detail"];
  172. if ([detail isKindOfClass:[NSString class]]) {
  173. _title = detail;
  174. }
  175. for (NSDictionary<NSString *, NSString *> *problem in jsonDictionary[@"invalid_params"]) {
  176. NSString *name = problem[@"name"];
  177. NSString *reason = problem[@"reason"];
  178. if (name && reason) {
  179. _title = [NSString stringWithFormat:@"%@ %@: %@;", _title, name, reason];
  180. }
  181. }
  182. return self;
  183. }
  184. return nil;
  185. }
  186. @end