Skip to content

Commit b9da1d6

Browse files
committed
refactor: default rest client support changing id and key
1 parent ef8cfee commit b9da1d6

File tree

4 files changed

+27
-40
lines changed

4 files changed

+27
-40
lines changed

AVOS/Sources/Foundation/LCApplication.m

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,6 @@
1515

1616
@implementation LCApplication
1717

18-
+ (void)initializePaasClient
19-
{
20-
LCPaasClient *paasClient = [LCPaasClient sharedInstance];
21-
22-
paasClient.applicationId = [self getApplicationId];
23-
paasClient.clientKey = [self getClientKey];
24-
25-
[paasClient handleAllArchivedRequests];
26-
}
27-
2818
+ (void)setApplicationId:(NSString *)applicationId
2919
clientKey:(NSString *)clientKey
3020
serverURLString:(NSString *)serverURLString
@@ -44,11 +34,11 @@ + (void)setApplicationId:(NSString *)applicationId
4434
format:@"Server URL not found."] ;
4535
}
4636
}
47-
4837
[[LCApplication defaultApplication] setWithIdentifier:applicationId
4938
key:clientKey];
50-
51-
[self initializePaasClient];
39+
LCPaasClient *paasClient = [LCPaasClient sharedInstance];
40+
paasClient.application = [LCApplication defaultApplication];
41+
[paasClient handleAllArchivedRequests];
5242
}
5343

5444
+ (NSString *)getApplicationId {
@@ -184,8 +174,7 @@ + (void)getServerDateWithBlock:(void (^)(NSDate * _Nullable, NSError * _Nullable
184174
});
185175
}
186176

187-
+ (instancetype)defaultApplication
188-
{
177+
+ (instancetype)defaultApplication {
189178
static LCApplication *instance;
190179
static dispatch_once_t onceToken;
191180
dispatch_once(&onceToken, ^{
@@ -194,23 +183,20 @@ + (instancetype)defaultApplication
194183
return instance;
195184
}
196185

197-
- (void)setWithIdentifier:(NSString *)identifier key:(NSString *)key
198-
{
186+
- (void)setWithIdentifier:(NSString *)identifier key:(NSString *)key {
199187
_identifier = [identifier copy];
200188
_key = [key copy];
201189
}
202190

203-
- (NSString *)identifierThrowException
204-
{
191+
- (NSString *)identifierThrowException {
205192
if (!self.identifier) {
206193
[NSException raise:NSInternalInconsistencyException
207194
format:@"Application identifier not found."];
208195
}
209196
return self.identifier;
210197
}
211198

212-
- (NSString *)keyThrowException
213-
{
199+
- (NSString *)keyThrowException {
214200
if (!self.key) {
215201
[NSException raise:NSInternalInconsistencyException
216202
format:@"Application key not found."];

AVOS/Sources/Foundation/LCConstants.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -160,19 +160,18 @@ typedef void (^LCBooleanResultBlock)(BOOL succeeded, NSError * _Nullable error)
160160
typedef void (^LCIntegerResultBlock)(NSInteger number, NSError * _Nullable error);
161161
typedef void (^LCArrayResultBlock)(NSArray * _Nullable objects, NSError * _Nullable error);
162162
typedef void (^LCObjectResultBlock)(LCObject * _Nullable object, NSError * _Nullable error);
163-
typedef void (^LCSetResultBlock)(NSSet * _Nullable channels, NSError * _Nullable error);
163+
typedef void (^LCSetResultBlock)(NSSet * _Nullable set, NSError * _Nullable error);
164164
typedef void (^LCUserResultBlock)(LCUser * _Nullable user, NSError * _Nullable error);
165165
typedef void (^LCDataResultBlock)(NSData * _Nullable data, NSError * _Nullable error);
166166
#if LC_TARGET_OS_OSX
167167
typedef void (^LCImageResultBlock)(NSImage * _Nullable image, NSError * _Nullable error);
168168
#else
169169
typedef void (^LCImageResultBlock)(UIImage * _Nullable image, NSError * _Nullable error);
170170
#endif
171-
typedef void (^LCDataStreamResultBlock)(NSInputStream * _Nullable stream, NSError * _Nullable error);
172171
typedef void (^LCStringResultBlock)(NSString * _Nullable string, NSError * _Nullable error);
173172
typedef void (^LCIdResultBlock)(id _Nullable object, NSError * _Nullable error);
174-
typedef void (^LCProgressBlock)(NSInteger percentDone);
173+
typedef void (^LCProgressBlock)(NSInteger percent);
175174
typedef void (^LCFileResultBlock)(LCFile * _Nullable file, NSError * _Nullable error);
176-
typedef void (^LCDictionaryResultBlock)(NSDictionary * _Nullable dict, NSError * _Nullable error);
175+
typedef void (^LCDictionaryResultBlock)(NSDictionary * _Nullable dictionary, NSError * _Nullable error);
177176

178177
#define LC_WARN_UNUSED_RESULT __attribute__((warn_unused_result))

AVOS/Sources/Foundation/Request/LCPaasClient.h

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,21 @@
1111
#import "LCACL.h"
1212
#import "UserAgent.h"
1313

14-
static NSString *const USER_AGENT = @"LeanCloud-Objc-SDK/" SDK_VERSION;
14+
@class LCApplication;
1515

16-
FOUNDATION_EXPORT NSString *const LCHeaderFieldNameId;
17-
FOUNDATION_EXPORT NSString *const LCHeaderFieldNameKey;
18-
FOUNDATION_EXPORT NSString *const LCHeaderFieldNameSign;
19-
FOUNDATION_EXPORT NSString *const LCHeaderFieldNameSession;
20-
FOUNDATION_EXPORT NSString *const LCHeaderFieldNameProduction;
16+
static NSString * const USER_AGENT = @"LeanCloud-Objc-SDK/" SDK_VERSION;
17+
18+
FOUNDATION_EXPORT NSString * const LCHeaderFieldNameId;
19+
FOUNDATION_EXPORT NSString * const LCHeaderFieldNameKey;
20+
FOUNDATION_EXPORT NSString * const LCHeaderFieldNameSign;
21+
FOUNDATION_EXPORT NSString * const LCHeaderFieldNameSession;
22+
FOUNDATION_EXPORT NSString * const LCHeaderFieldNameProduction;
2123

2224
@interface LCPaasClient : NSObject
2325

24-
+(LCPaasClient *)sharedInstance;
26+
+ (LCPaasClient *)sharedInstance;
2527

26-
@property (nonatomic, readwrite, copy) NSString * applicationId;
27-
@property (nonatomic, readwrite, copy) NSString * clientKey;
28+
@property (nonatomic) LCApplication *application;
2829
@property (nonatomic, readonly, copy) NSString * apiVersion;
2930
@property (nonatomic, readwrite, copy) NSString * applicationIdField;
3031
@property (nonatomic, readwrite, copy) NSString * applicationKeyField;

AVOS/Sources/Foundation/Request/LCPaasClient.m

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#import "LCObjectUtils.h"
2121
#import "LCRouter_Internal.h"
2222
#import "LCConstants.h"
23+
#import "LCApplication_Internal.h"
2324

2425
static NSString * const kLC_code = @"code";
2526
static NSString * const kLC_error = @"error";
@@ -195,10 +196,10 @@ -(void)clearLastModifyCache {
195196
}
196197

197198
- (NSString *)signatureHeaderFieldValue {
198-
NSString *timestamp=[NSString stringWithFormat:@"%.0f",1000*[[NSDate date] timeIntervalSince1970]];
199-
NSString *sign=[[[NSString stringWithFormat:@"%@%@",timestamp,self.clientKey] LCMD5String] lowercaseString];
200-
NSString *headerValue=[NSString stringWithFormat:@"%@,%@",sign,timestamp];
201-
199+
NSString *key = [self.application keyThrowException];
200+
NSString *timestamp = [NSString stringWithFormat:@"%.0f", 1000 * [[NSDate date] timeIntervalSince1970]];
201+
NSString *sign = [[[NSString stringWithFormat:@"%@%@", timestamp, key] LCMD5String] lowercaseString];
202+
NSString *headerValue = [NSString stringWithFormat:@"%@,%@", sign, timestamp];
202203
return headerValue;
203204
}
204205

@@ -245,10 +246,11 @@ - (NSMutableURLRequest *)requestWithPath:(NSString *)path
245246
}
246247

247248
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL];
249+
NSString *appID = [self.application identifierThrowException];
248250

249251
[request setHTTPMethod:method];
250252
[request setTimeoutInterval:self.timeoutInterval];
251-
[request setValue:self.applicationId forHTTPHeaderField:LCHeaderFieldNameId];
253+
[request setValue:appID forHTTPHeaderField:LCHeaderFieldNameId];
252254
[request setValue:[self signatureHeaderFieldValue] forHTTPHeaderField:LCHeaderFieldNameSign];
253255
[request setValue:self.productionMode ? @"1": @"0" forHTTPHeaderField:LCHeaderFieldNameProduction];
254256
[request setValue:USER_AGENT forHTTPHeaderField:@"User-Agent"];
@@ -257,7 +259,6 @@ - (NSMutableURLRequest *)requestWithPath:(NSString *)path
257259
[request setValue:@"gzip" forHTTPHeaderField:@"Accept-Encoding"];
258260

259261
NSString *sessionToken = self.currentUser.sessionToken;
260-
261262
if (sessionToken) {
262263
[request setValue:sessionToken forHTTPHeaderField:LCHeaderFieldNameSession];
263264
}

0 commit comments

Comments
 (0)