added iOS source code
[wl-app.git] / iOS / Pods / Realm / Realm / RLMJSONModels.m
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
19 #import "RLMJSONModels.h"
20 #import "RLMSyncUtil_Private.h"
21 #import "RLMSyncUser.h"
22
23 #pragma mark - Constants
24
25 static const NSString *const kRLMSyncAccessTokenKey     = @"access_token";
26 static const NSString *const kRLMSyncAccountsKey        = @"accounts";
27 static const NSString *const kRLMSyncErrorCodeKey       = @"code";
28 static const NSString *const kRLMSyncExpiresKey         = @"expires";
29 static const NSString *const kRLMSyncErrorHintKey       = @"hint";
30 static const NSString *const kRLMSyncIdKey              = @"id";
31 static const NSString *const kRLMSyncKeyKey             = @"key";
32 static const NSString *const kRLMSyncMetadataKey        = @"metadata";
33 static const NSString *const kRLMSyncRefreshTokenKey    = @"refresh_token";
34 static const NSString *const kRLMSyncErrorStatusKey     = @"status";
35 static const NSString *const kRLMSyncErrorTitleKey      = @"title";
36 static const NSString *const kRLMSyncTokenDataKey       = @"token_data";
37 static const NSString *const kRLMSyncUserKey            = @"user";
38 static const NSString *const kRLMSyncValueKey           = @"value";
39
40 #pragma mark - RLMTokenDataModel
41
42 @interface RLMTokenDataModel ()
43
44 @property (nonatomic, readwrite) NSString *identity;
45 @property (nonatomic, readwrite) NSString *appID;
46 @property (nonatomic, readwrite) NSString *path;
47 @property (nonatomic, readwrite) NSTimeInterval expires;
48 @property (nonatomic, readwrite) BOOL isAdmin;
49 //@property (nonatomic, readwrite) NSArray *access;
50
51 @end
52
53 @implementation RLMTokenDataModel
54
55 - (instancetype)initWithDictionary:(NSDictionary *)jsonDictionary {
56     if (self = [super init]) {
57         self.isAdmin = NO;
58         RLM_SYNC_PARSE_STRING_OR_ABORT(jsonDictionary, kRLMSyncIdentityKey, identity);
59         RLM_SYNC_PARSE_OPTIONAL_STRING(jsonDictionary, kRLMSyncAppIDKey, appID);
60         RLM_SYNC_PARSE_OPTIONAL_STRING(jsonDictionary, kRLMSyncPathKey, path);
61         RLM_SYNC_PARSE_OPTIONAL_BOOL(jsonDictionary, kRLMSyncIsAdminKey, isAdmin);
62         RLM_SYNC_PARSE_DOUBLE_OR_ABORT(jsonDictionary, kRLMSyncExpiresKey, expires);
63         return self;
64     }
65     return nil;
66 }
67
68 @end
69
70 #pragma mark - RLMTokenModel
71
72 @interface RLMTokenModel ()
73
74 @property (nonatomic, readwrite) NSString *token;
75 @property (nonatomic, nullable, readwrite) NSString *path;
76 @property (nonatomic, readwrite) RLMTokenDataModel *tokenData;
77
78 @end
79
80 @implementation RLMTokenModel
81
82 - (instancetype)initWithDictionary:(NSDictionary *)jsonDictionary {
83     if (self = [super init]) {
84         RLM_SYNC_PARSE_STRING_OR_ABORT(jsonDictionary, kRLMSyncTokenKey, token);
85         RLM_SYNC_PARSE_OPTIONAL_STRING(jsonDictionary, kRLMSyncPathKey, path);
86         RLM_SYNC_PARSE_MODEL_OR_ABORT(jsonDictionary, kRLMSyncTokenDataKey, RLMTokenDataModel, tokenData);
87         return self;
88     }
89     return nil;
90 }
91
92 @end
93
94 #pragma mark - RLMAuthResponseModel
95
96 @interface RLMAuthResponseModel ()
97
98 @property (nonatomic, readwrite) RLMTokenModel *accessToken;
99 @property (nonatomic, readwrite) RLMTokenModel *refreshToken;
100
101 @end
102
103 @implementation RLMAuthResponseModel
104
105 - (instancetype)initWithDictionary:(NSDictionary *)jsonDictionary
106                 requireAccessToken:(BOOL)requireAccessToken
107                requireRefreshToken:(BOOL)requireRefreshToken {
108     if (self = [super init]) {
109         // Get the access token.
110         if (requireAccessToken) {
111             RLM_SYNC_PARSE_MODEL_OR_ABORT(jsonDictionary, kRLMSyncAccessTokenKey, RLMTokenModel, accessToken);
112         } else {
113             RLM_SYNC_PARSE_OPTIONAL_MODEL(jsonDictionary, kRLMSyncAccessTokenKey, RLMTokenModel, accessToken);
114         }
115         // Get the refresh token.
116         if (requireRefreshToken) {
117             RLM_SYNC_PARSE_MODEL_OR_ABORT(jsonDictionary, kRLMSyncRefreshTokenKey, RLMTokenModel, refreshToken);
118         } else {
119             RLM_SYNC_PARSE_OPTIONAL_MODEL(jsonDictionary, kRLMSyncRefreshTokenKey, RLMTokenModel, refreshToken);
120         }
121         return self;
122     }
123     return nil;
124 }
125
126 @end
127
128 #pragma mark - RLMUserInfoResponseModel
129
130 @interface RLMSyncUserAccountInfo ()
131 @property (nonatomic, readwrite) NSString *provider;
132 @property (nonatomic, readwrite) NSString *providerUserIdentity;
133 @end
134
135 @implementation RLMSyncUserAccountInfo
136
137 - (instancetype)initWithDictionary:(NSDictionary *)jsonDictionary {
138     if (self = [super init]) {
139         RLM_SYNC_PARSE_STRING_OR_ABORT(jsonDictionary, kRLMSyncProviderKey, provider);
140         RLM_SYNC_PARSE_STRING_OR_ABORT(jsonDictionary, kRLMSyncProviderIDKey, providerUserIdentity);
141         return self;
142     }
143     return nil;
144 }
145
146 @end
147
148 @interface RLMUserResponseModel ()
149
150 @property (nonatomic, readwrite) NSString *identity;
151 @property (nonatomic, readwrite) NSArray *accounts;
152 @property (nonatomic, readwrite) NSDictionary *metadata;
153 @property (nonatomic, readwrite) BOOL isAdmin;
154
155 @end
156
157 @implementation RLMUserResponseModel
158
159 - (void)parseMetadataFromJSON:(NSDictionary *)jsonDictionary {
160     NSMutableDictionary *buffer = [NSMutableDictionary dictionary];
161     NSArray *metadataArray = jsonDictionary[kRLMSyncMetadataKey];
162     if (![metadataArray isKindOfClass:[NSArray class]]) {
163         self.metadata = @{};
164         return;
165     }
166     for (NSDictionary *object in metadataArray) {
167         if (![object isKindOfClass:[NSDictionary class]]) {
168             continue;
169         }
170         NSString *key = object[kRLMSyncKeyKey];
171         NSString *value = object[kRLMSyncValueKey];
172         if (!key || !value) {
173             continue;
174         }
175         buffer[key] = value;
176     }
177     self.metadata = [buffer copy];
178 }
179
180 - (instancetype)initWithDictionary:(NSDictionary *)jsonDictionary {
181     if (self = [super init]) {
182         self.isAdmin = NO;
183         RLM_SYNC_PARSE_STRING_OR_ABORT(jsonDictionary, kRLMSyncUserIDKey, identity);
184         RLM_SYNC_PARSE_OPTIONAL_BOOL(jsonDictionary, kRLMSyncIsAdminKey, isAdmin);
185         RLM_SYNC_PARSE_MODEL_ARRAY_OR_ABORT(jsonDictionary, kRLMSyncAccountsKey, RLMSyncUserAccountInfo, accounts);
186         [self parseMetadataFromJSON:jsonDictionary];
187         return self;
188     }
189     return nil;
190 }
191
192 @end
193
194 #pragma mark - RLMSyncErrorResponseModel
195
196 @interface RLMSyncErrorResponseModel ()
197
198 @property (nonatomic, readwrite) NSInteger status;
199 @property (nonatomic, readwrite) NSInteger code;
200 @property (nonatomic, readwrite) NSString *title;
201 @property (nonatomic, readwrite) NSString *hint;
202
203 @end
204
205 @implementation RLMSyncErrorResponseModel
206
207 - (instancetype)initWithDictionary:(NSDictionary *)jsonDictionary {
208     if (self = [super init]) {
209         RLM_SYNC_PARSE_DOUBLE_OR_ABORT(jsonDictionary, kRLMSyncErrorStatusKey, status);
210         RLM_SYNC_PARSE_DOUBLE_OR_ABORT(jsonDictionary, kRLMSyncErrorCodeKey, code);
211         RLM_SYNC_PARSE_OPTIONAL_STRING(jsonDictionary, kRLMSyncErrorTitleKey, title);
212         RLM_SYNC_PARSE_OPTIONAL_STRING(jsonDictionary, kRLMSyncErrorHintKey, hint);
213         return self;
214     }
215     return nil;
216 }
217
218 @end