RLMJSONModels.m 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. @end
  44. @implementation RLMTokenDataModel
  45. - (instancetype)initWithDictionary:(NSDictionary *)jsonDictionary {
  46. if (self = [super init]) {
  47. self.isAdmin = NO;
  48. RLM_SYNC_PARSE_STRING_OR_ABORT(jsonDictionary, kRLMSyncIdentityKey, identity);
  49. RLM_SYNC_PARSE_OPTIONAL_STRING(jsonDictionary, kRLMSyncAppIDKey, appID);
  50. RLM_SYNC_PARSE_OPTIONAL_STRING(jsonDictionary, kRLMSyncPathKey, path);
  51. RLM_SYNC_PARSE_OPTIONAL_BOOL(jsonDictionary, kRLMSyncIsAdminKey, isAdmin);
  52. RLM_SYNC_PARSE_DOUBLE_OR_ABORT(jsonDictionary, kRLMSyncExpiresKey, expires);
  53. return self;
  54. }
  55. return nil;
  56. }
  57. @end
  58. #pragma mark - RLMTokenModel
  59. @interface RLMTokenModel ()
  60. @property (nonatomic, readwrite) NSString *token;
  61. @property (nonatomic, nullable, readwrite) NSString *path;
  62. @property (nonatomic, readwrite) RLMTokenDataModel *tokenData;
  63. @end
  64. @implementation RLMTokenModel
  65. - (instancetype)initWithDictionary:(NSDictionary *)jsonDictionary {
  66. if (self = [super init]) {
  67. RLM_SYNC_PARSE_STRING_OR_ABORT(jsonDictionary, kRLMSyncTokenKey, token);
  68. RLM_SYNC_PARSE_OPTIONAL_STRING(jsonDictionary, kRLMSyncPathKey, path);
  69. RLM_SYNC_PARSE_MODEL_OR_ABORT(jsonDictionary, kRLMSyncTokenDataKey, RLMTokenDataModel, tokenData);
  70. return self;
  71. }
  72. return nil;
  73. }
  74. @end
  75. #pragma mark - RLMAuthResponseModel
  76. @interface RLMAuthResponseModel ()
  77. @property (nonatomic, readwrite) RLMTokenModel *accessToken;
  78. @property (nonatomic, readwrite) RLMTokenModel *refreshToken;
  79. @property (nonatomic, readwrite) NSString *urlPrefix;
  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. self.urlPrefix = jsonDictionary[@"sync_worker"][@"path"];
  99. return self;
  100. }
  101. return nil;
  102. }
  103. @end
  104. #pragma mark - RLMUserInfoResponseModel
  105. @interface RLMSyncUserAccountInfo ()
  106. @property (nonatomic, readwrite) NSString *provider;
  107. @property (nonatomic, readwrite) NSString *providerUserIdentity;
  108. @end
  109. @implementation RLMSyncUserAccountInfo
  110. - (instancetype)initWithDictionary:(NSDictionary *)jsonDictionary {
  111. if (self = [super init]) {
  112. RLM_SYNC_PARSE_STRING_OR_ABORT(jsonDictionary, kRLMSyncProviderKey, provider);
  113. RLM_SYNC_PARSE_STRING_OR_ABORT(jsonDictionary, kRLMSyncProviderIDKey, providerUserIdentity);
  114. return self;
  115. }
  116. return nil;
  117. }
  118. @end
  119. @interface RLMUserResponseModel ()
  120. @property (nonatomic, readwrite) NSString *identity;
  121. @property (nonatomic, readwrite) NSArray *accounts;
  122. @property (nonatomic, readwrite) NSDictionary *metadata;
  123. @property (nonatomic, readwrite) BOOL isAdmin;
  124. @end
  125. @implementation RLMUserResponseModel
  126. - (void)parseMetadataFromJSON:(NSDictionary *)jsonDictionary {
  127. NSMutableDictionary *buffer = [NSMutableDictionary dictionary];
  128. NSArray *metadataArray = jsonDictionary[kRLMSyncMetadataKey];
  129. if (![metadataArray isKindOfClass:[NSArray class]]) {
  130. self.metadata = @{};
  131. return;
  132. }
  133. for (NSDictionary *object in metadataArray) {
  134. if (![object isKindOfClass:[NSDictionary class]]) {
  135. continue;
  136. }
  137. NSString *key = object[kRLMSyncKeyKey];
  138. NSString *value = object[kRLMSyncValueKey];
  139. if (!key || !value) {
  140. continue;
  141. }
  142. buffer[key] = value;
  143. }
  144. self.metadata = [buffer copy];
  145. }
  146. - (instancetype)initWithDictionary:(NSDictionary *)jsonDictionary {
  147. if (self = [super init]) {
  148. self.isAdmin = NO;
  149. RLM_SYNC_PARSE_STRING_OR_ABORT(jsonDictionary, kRLMSyncUserIDKey, identity);
  150. RLM_SYNC_PARSE_OPTIONAL_BOOL(jsonDictionary, kRLMSyncIsAdminKey, isAdmin);
  151. RLM_SYNC_PARSE_MODEL_ARRAY_OR_ABORT(jsonDictionary, kRLMSyncAccountsKey, RLMSyncUserAccountInfo, accounts);
  152. [self parseMetadataFromJSON:jsonDictionary];
  153. return self;
  154. }
  155. return nil;
  156. }
  157. @end
  158. #pragma mark - RLMSyncErrorResponseModel
  159. @interface RLMSyncErrorResponseModel ()
  160. @property (nonatomic, readwrite) NSInteger status;
  161. @property (nonatomic, readwrite) NSInteger code;
  162. @property (nonatomic, readwrite) NSString *title;
  163. @property (nonatomic, readwrite) NSString *hint;
  164. @end
  165. @implementation RLMSyncErrorResponseModel
  166. - (instancetype)initWithDictionary:(NSDictionary *)jsonDictionary {
  167. if (self = [super init]) {
  168. RLM_SYNC_PARSE_DOUBLE_OR_ABORT(jsonDictionary, kRLMSyncErrorStatusKey, status);
  169. RLM_SYNC_PARSE_DOUBLE_OR_ABORT(jsonDictionary, kRLMSyncErrorCodeKey, code);
  170. RLM_SYNC_PARSE_OPTIONAL_STRING(jsonDictionary, kRLMSyncErrorTitleKey, title);
  171. RLM_SYNC_PARSE_OPTIONAL_STRING(jsonDictionary, kRLMSyncErrorHintKey, hint);
  172. NSString *detail = jsonDictionary[@"detail"];
  173. if ([detail isKindOfClass:[NSString class]]) {
  174. _title = detail;
  175. }
  176. for (NSDictionary<NSString *, NSString *> *problem in jsonDictionary[@"invalid_params"]) {
  177. NSString *name = problem[@"name"];
  178. NSString *reason = problem[@"reason"];
  179. if (name && reason) {
  180. _title = [NSString stringWithFormat:@"%@ %@: %@;", _title, name, reason];
  181. }
  182. }
  183. return self;
  184. }
  185. return nil;
  186. }
  187. @end