developer
2023-05-20 e12c7b4c22df631ebdcd16b2f98fbef8f738f92f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
 
//
//  MtopExtRequest.h
//  mtopext
//
//  封装的mtop api 请求
//
//  Created by sihai on 4/11/14.
//  Copyright (c) 2014 Taobao. All rights reserved.
//
 
#import <Foundation/Foundation.h>
#import <mtopext/MtopExtRequestDelegate.h>
#import <mtopext/MtopAuthProtocol.h>
#import <mtopext/TBSDKUTUtility.h>
 
@class TBSDKRequest;
 
 
#pragma mark - 枚举定义
 
 
/**
 * cache 策略
 */
typedef NS_ENUM(NSUInteger,CachePolicy) {
    /**
     * 默认cache策略。使用这个策略时,request会先查看cache中是否有可用的缓存数据。如果没有,request会像普通request那样工作。
     * 如果有缓存数据并且缓存数据没有过期,那么request会使用缓存的数据,而且不会向服务器通信。如果缓存数据过期了,request会先
     * 进行GET请求来向服务器询问数据是否有新的版本。如果服务器说缓存的数据就是当前版本,那么缓存数据将被使用,不会下载新数据。
     * 在这种情况下,cache的有效期将被设定为服务器端提供的新的有效期。如果服务器提供更新的内容,那么新内容会被下载,并且新的数据
     * 以及它的有效期将被写入cache。
     */
    AskServerIfModifiedWhenStaleCachePolicy = 0,
    
    
    /**
     * 不使用任何cache策略,普通的请求
     */
    DoNotReadAndWriteCacheCachePolicy,
    
    
    /**
     * 用于强制刷新,没有cache回调,一定请求网络数据
     */
    AskServerIfModifiedCachePolicy,
    
    
    /**
     * 用于强制刷新,有cache回调,一定请求网络数据
     */
    AskServerIfModifiedCacheAndReturnCachePolicy,
    
    
    /**
     * 用于MessageBox使用,有cache回调,一定请求网络数据
     */
    AskServerIfModifiedCacheAndReturnCachePolicyMessageBox
    
};
 
 
/**
 * 登录控制参数
 */
typedef NS_ENUM(NSInteger,MtopSessionExpiredOption) {
    /**
     * SDK内部标记用,外部业务禁止使用
     */
    MtopSessionExpiredOptionDummy   = -1,
    
    
    /**
     * Session失效时,什么都不做
     */
    MtopSessionExpiredOptionNone    = 0,
    
    
    /**
     * Session失效时,只做Auto Login
     */
    MtopSessionExpiredOptionAutoLogin   = 1,
    
    
    /**
     * Session失效时,先Auto Login,如果Auto Login失败,唤起登录界面
     */
    MtopSessionExpiredOptionAutoLoginAndManualLogin = 2
 
};
 
 
 
/**
 * 授权控制参数
 */
typedef NS_ENUM(NSInteger,MtopAuthExpiredOption){
    /**
     * SDK内部标记用,外部业务禁止使用
     */
    MtopAuthExpiredOptionDummy  = -1,
    
    
    /**
     * Auth失效时,什么都不做
     */
    MtopAuthExpiredOptionNone   = 0,
    
    
    /**
     * Auth失效时,只做Auto Auth
     */
    MtopAuthExpiredOptionAutoAuth = 1,
    
    
    /**
     * Auth失效时,先Auto Auth,如果Auto Auth失败,唤起登录界面
     */
    MtopAuthExpiredOptionAutAuthAndManualAuth = 2
};
 
 
 
/**
 * WUA 控制参数
 */
typedef NS_ENUM(NSUInteger,WuaType) {
    kWuaDefault = 3,
    kWuaGeneral,
    kWuaMini,
    kWuaAtlas
};
 
 
/**
 *  控制请求是属于导购单元还是交易单元。该参数最终体现在请求域名上。
 */
typedef NS_ENUM(NSUInteger,MtopUnitStrategy) {
    /**
     * 导购单元(默认所有请求走导购单元)
     */
    MtopUnitStrategyGuideUnit = 1,
    
    
    /**
     * 交易单元
     */
    MtopUnitStrategyTradeUnit
};
 
#pragma mark - MTOP请求对象
 
/**
 * MTOP请求对象接口。
 */
@interface MtopExtRequest : NSObject
 
 
/**
 * 本地请求的自定义域名。
 */
@property(nonatomic, strong) NSString* customHost;
 
 
/**
 * 同步请求, 还是异步请求。默认值为 NO。
 * 其值取决于对象 [MtopService sync_call:] 与 [MtopService async_call:]
 */
@property(nonatomic, assign) BOOL isSync;
 
 
/**
 * 指示是否需要登录态,由业务设置。
 * P.S. 登录相关为什么如此命名?具体原因己不可考。基于兼容性考虑,已不便变动。
 */
@property(nonatomic, assign) BOOL isNeedEcode;
 
 
/**
 * Session 失效选项。isNeedEcode = YES 的情况下有效。
 */
@property(nonatomic, assign) MtopSessionExpiredOption sessionExpiredOption;
 
 
/**
 * 指示是否需要Auth授权
 */
@property(nonatomic, assign) BOOL isNeedAuth;
 
 
/**
 * 授权过期选项。isNeedAuth = YES 的情况下有效。
 */
@property(nonatomic, assign) MtopAuthExpiredOption authExpiredOption;
 
 
/**
 * 是否启用虚机签名
 */
@property(nonatomic, assign) BOOL isEnableWUA;
 
 
/**
 * 虚机签名类型。isEnableWUA = YES 的情况下有效。
 */
@property(nonatomic, assign) WuaType wuaType;
 
 
/**
 * 本地请求的 ttid (渠道号)。如业务未设置,以 TBSDKConfiguration.wapTTID 值为准。
 */
@property(nonatomic, strong) NSString* ttid;
 
 
/**
 * cache策略
 */
@property(nonatomic, assign) CachePolicy cachePolicy;
 
 
/**
 * 计算cache key 时需要排查的参数名列表.
 * 备注:目前用数组而没有用dict是合理的, 参数数量不会多。
 */
@property(nonatomic, strong) NSMutableArray* excludedCacheKeyParameters;
 
 
/**
 * 是否已经被取消
 */
@property(atomic, assign) BOOL isCanceled;
 
 
/**
 * 是否需要校验服务端的响应
 */
@property(assign, nonatomic) BOOL isNeedValidateResponse;
 
 
/**
 * 重试的次数
 */
@property(nonatomic, assign, readonly) int8_t retryCount;
 
 
/**
 * 自定义请求user id, 影响请求 x-uid 头域
 */
@property(nonatomic, strong) NSString *userID;
 
 
/**
 * SDK 内部生成的端側trace id, 请求 x-c-traceid 头域值。
 */
@property(nonatomic, strong, readonly) NSString *clientTraceId;
 
 
/**
 * 全链路埋点ID。
 */
@property (nonatomic, copy, readonly) NSString *fullTraceID;
 
 
/**
 * 业务ID,值由构造参数传入
 */
@property(nonatomic, strong, readonly) NSString  *bizID;
 
 
/**
 * 当前请求的page name, 埋点用。
 */
@property(nonatomic, copy) NSString *pageName;
 
 
/**
 * 当前请求的page url, 埋点用。
 */
@property(nonatomic, copy) NSString *pageUrl;
 
 
/**
 * 当前请求的单元域名策略。
 */
@property(nonatomic, assign) MtopUnitStrategy  UnitStrategy;
 
 
/**
 * 是否使用主线程回调。YES: 不从主线程回调, NO: 从主线程回调。
 */
@property(nonatomic, assign) BOOL isNotUseMainThreadCallback;
 
 
/**
 * 淘内开放API授权参数对象
 */
@property (nonatomic,strong, readonly) AuthParamObj *authObj;
 
 
#pragma mark - 小程序开放体系参数
 
/**
 * 开放小程序 Appkey. 影响协议头: x-mini-appkey
 */
@property (nonatomic, strong) NSString  *miniAppkey;
 
 
/**
 * 小程序appkey/插件appkey. 影响协议头: x-req-appkey
 */
@property (nonatomic, strong) NSString  *requestAppkey;
 
 
/**
 * 开放业务平台标识码, 目前只有 mini-app / baichuan 两种类型。影响协议头: x-open-biz
 */
@property (nonatomic, strong) NSString  *openBizCode;
 
 
/**
 * 开放业务拓展字段。影响协议头: x-open-biz-data
 */
@property (nonatomic, strong) NSString  *openBizData;
 
 
/**
 * 原底层的 mtop 请求对象. 外部业务禁止修改。
 */
@property(nonatomic, strong) TBSDKRequest* mrequest;
 
#pragma mark - 回调
 
/**
 * Block回调,请求开始
 */
@property(atomic, copy) MtopExtRequestStarted  startedBlock;
 
 
/**
 * Block回调,请求失败
 */
@property(atomic, copy) MtopExtRequestFailed   failedBlock;
 
 
/**
 * Block回调,请求成功
 */
@property(atomic, copy) MtopExtRequestSucceed  succeedBlock;
 
 
/**
 * Delegate回调, 该回调与block回调语义等价,业务方可选择合适自己的方式来设置回调。
 */
@property(atomic, weak, atomic) id<MtopExtRequestDelegate> delegate;
 
 
#pragma mark - 埋点信息
 
/**
 * 埋点信息
 */
@property(strong, nonatomic) UTDataSet* mtopUT;
 
 
#pragma mark - 废弃参数
 
@property(nonatomic, strong) NSString  *h5URL DEPRECATED_MSG_ATTRIBUTE("使用 pageUrl 代替");
@property(nonatomic, strong) NSString* wuaPageName DEPRECATED_MSG_ATTRIBUTE("无用参数");
@property(nonatomic, strong) NSString* wuaCtrlName DEPRECATED_MSG_ATTRIBUTE("无用参数");
@property(nonatomic, assign) float xCoordinate DEPRECATED_MSG_ATTRIBUTE("无用参数");
@property(nonatomic, assign) float yCoordinate DEPRECATED_MSG_ATTRIBUTE("无用参数");
@property(nonatomic, strong) NSArray *customHostList DEPRECATED_MSG_ATTRIBUTE("无用参数");
@property(nonatomic, weak) id context DEPRECATED_MSG_ATTRIBUTE("无用参数");
@property(nonatomic, assign) BOOL isFromOfflineOperation DEPRECATED_MSG_ATTRIBUTE("无用参数");
@property(nonatomic, assign) ServerType serverType DEPRECATED_MSG_ATTRIBUTE("无用参数");
@property(nonatomic, assign) BOOL isForceHttps DEPRECATED_MSG_ATTRIBUTE("无用参数");
@property(nonatomic, assign) BOOL isForceSpdy DEPRECATED_MSG_ATTRIBUTE("无用参数");
 
/**
 * 初始化一个API请求对象
 * @param apiName       API名称
 * @param apiVersion    API版本
 * @return
 *              MtopExtRequest
 */
- (MtopExtRequest*) initWithApiName: (NSString*) apiName apiVersion: (NSString*) apiVersion;
 
/*!
 * 初始化一个API请求对象
 * @param apiName       API名称
 * @param apiVersion    API版本
 * @param bizID         业务组件ID
 * @return
 *              MtopExtRequest
 */
- (MtopExtRequest *)initWithApiName:(NSString *)apiName apiVersion:(NSString *)apiVersion bizID:(NSString *)bizID;
 
 
/*
 *  设置请求不被网络库接管
 */
- (MtopExtRequest *)unHostedByNetWork;
 
/*!
 * 设置网络超时
 * @param timeout 超时时间,单位秒
 * @return
 *              MtopExtRequest 以便可以链式操作
 */
- (MtopExtRequest*) setNetworkTimeout: (int) timeout;
 
/*!
 * 走https
 * @return
 *              MtopExtRequest 以便可以链式操作
 */
- (MtopExtRequest*) useHttps;
 
/*!
 * 禁止走https
 * @return
 *              MtopExtRequest 以便可以链式操作
 */
- (MtopExtRequest*) disableHttps;
 
/*!
 * 使用post
 * @return
 *              MtopExtRequest 以便可以链式操作
 */
- (MtopExtRequest*) useHttpPost;
 
/*!
 * 添加一个http请求头
 * @param value     HTTP头value, 请不要做urlencode, 底层会统一做的
 * @param key       HTTP头key
 * @return
 *              MtopExtRequest 以便可以链式操作
 */
- (MtopExtRequest*) addHttpHeader: (NSString*) value forKey: (NSString*) key;
 
/*!
 * 一次添加多个头
 * @param kvs
 * @return
 *              MtopExtRequest 以便可以链式操作
 */
- (MtopExtRequest*) addHttpHeaders: (NSDictionary*) kvs;
 
/*!
 * 添加一个http请求头 (该接口请求头不做UrlEncode)
 * @param value     HTTP头value, 请不要做urlencode, 底层会统一做的
 * @param key       HTTP头key
 * @return
 *              MtopExtRequest 以便可以链式操作
 */
- (MtopExtRequest*) addCustomHttpHeader: (NSString*) value forKey: (NSString*) key;
 
/*!
 * 一次添加多个头 (该接口请求头不做UrlEncode)
 * @param kvs
 * @return
 *              MtopExtRequest 以便可以链式操作
 */
- (MtopExtRequest*) addCustomHttpHeaders: (NSDictionary*) kvs;
 
 
 
/*!
 * 添加一个协议级参数
 * @param value     参数值
 * @param key       参数名
 * @return
 *              MtopExtRequest 以便可以链式操作
 */
- (MtopExtRequest*) addProtocolParameter: (NSString*) value forKey: (NSString*) key;
 
/*!
 * 添加一个扩展参数和data参数平级
 * @param value     参数值
 * @param key       参数名
 * @return
 *              MtopExtRequest 以便可以链式操作
 */
- (MtopExtRequest*) addExtParameter: (id) value forKey: (NSString*) key;
 
/*!
 * 一次添加多个扩展参数和data参数平级
 * @param kvs
 * @return
 *              MtopExtRequest 以便可以链式操作
 */
- (MtopExtRequest*) addExtParameters: (NSDictionary*) kvs;
 
/*!
 * 移除一个扩展参数
 * @param key
 * @return
 *              MtopExtRequest 以便可以链式操作
 *
 */
- (MtopExtRequest*) removeExtParameter: (NSString*) key;
 
/*!
 * 添加一个业务参数(业务级别参数)
 * @param value     参数值
 * @param key       参数名
 * @return
 *              MtopExtRequest 以便可以链式操作
 */
- (MtopExtRequest*) addBizParameter: (id) value forKey: (NSString*) key;
 
/*!
 * 一次添加多个业务参数(业务级别参数)
 * @param kvs
 * @return
 *              MtopExtRequest 以便可以链式操作
 */
- (MtopExtRequest*) addBizParameters: (NSDictionary*) kvs;
 
/*!
 * 移除一个业务参数
 * @param key
 * @return
 *              MtopExtRequest 以便可以链式操作
 */
- (MtopExtRequest*) removeBizParameter:(NSString*) key;
 
/*!
 * 为了TBSDKMtopServer兼容提供的, 不建议使用
 * @return
 *              MtopExtRequest 以便可以链式操作
 */
- (MtopExtRequest*) clearBizParameters;
 
#pragma mark 排队SDK用来设置请求优先级相关参数
/*!
 * 设置API请求优先级参数
 * @param data 优先级参数
 */
- (MtopExtRequest*)addPriorityData:(NSDictionary*)data;
 
/*!
 * 设置API请求优先级参数
 * @param value
 * @param key
 */
- (MtopExtRequest*) addPriorityData:(id)value forKey:(NSString *)key;
 
#pragma mark 排队SDK用来设置API请求优先级
/*!
 * 设置API请求优先级Flag
 * @param priorityFlag 优先级标识
 */
- (void) setPriorityFlag:(BOOL)priorityFlag;
 
/*!
 * 添加要上传的文件
 * @param data          文件数据
 * @param fileName      本地文件名(全路径)
 * @param forKey        form field 的 key
 * @return
 *              MtopExtRequest 以便可以链式操作
 *
 */
- (MtopExtRequest*) addUploadFileWithData: (NSData*) data fileName: (NSString*) fileName forKey: (NSString*) key;
 
/*!
 * 添加一个计算cache key 时排查的参数名
 * @param name 参数名
 *  @return
 *              MtopExtRequest 以便可以链式操作
 */
- (MtopExtRequest*) addExcludedCacheKeyParameter: (NSString*) name;
 
#pragma mark - 淘内开发api授权设置参数
/*!
 *
 * @param appkey        需授权的三方应用appkey
 * @param authParam     授权参数
 *  @return
 *              MtopExtRequest 以便可以链式操作
 */
- (MtopExtRequest*)setNeedAuth:(NSString *)appkey andAuthParam:(NSDictionary *)authParam;
 
 
/**
 指示生成WUA, 并设定WUA可选参数
 
 @param pageName
 @param ctrlName
 @param xCoordinate
 @param yCoordinate
 @return 带wua的MtopExtRequest
 */
- (MtopExtRequest*) enableWUAWithPageName: (NSString*) pageName
                                 ctrlName: (NSString*) ctrlName
                              xCoordinate: (float) xCoordinate
                              yCoordinate: (float) yCoordinate
                                DEPRECATED_MSG_ATTRIBUTE("enableWUAWithPageName is deprecated");
 
 
/*!
 * 获取请求id (请求id是系统生成的唯一的标识)
 * @return
 *              NSString
 */
- (NSString*) getRequestId;
 
/*!
 * 获取API名称
 * @return
 *              NSString
 */
- (NSString*) getApiName;
 
/*!
 * 获取API版本
 * @return
 *              NSString
 */
- (NSString*) getApiVersion;
 
/*!
 * 为了TBSDKMtopServer兼容提供的, 不建议使用
 * @param apiVersion
 *
 */
- (void) setApiName: (NSString*) apiName;
 
/*!
 * 为了TBSDKMtopServer兼容提供的, 不建议使用
 * @param apiVersion
 *
 */
- (void) setApiVersion: (NSString*) apiVersion;
 
/*!
 * 获取扩展参数
 * @return
 *              NSDictionary
 */
- (NSDictionary*) getExtParameters;
 
/*!
 * 获取业务参数
 * @return
 *              NSDictionary
 */
- (NSDictionary*) getBizParameters;
 
/*!
 * 获取http请求头(不包含协议参数头)
 */
- (NSDictionary *)getHttpHeaders;
 
/*!
 * 判断请求是否要求使用了post
 * @return
 *              YES
 *              NO
 */
- (BOOL) isUseHttpPost;
 
/*!
 * 判断请求是否要求使用了https
 * @return
 *              YES
 *              NO
 */
- (BOOL) isUseHttps;
 
/*!
 * 重试计数 retryCount += 1
 */
- (void) retryed;
 
/*!
 * 是否需要重试
 */
- (BOOL) isNeedRetry;
 
/*!
 * mtop ut 开始 (开始请求)
 * @param isSync
 */
- (void) utStart: (BOOL) isSync;
 
/*!
 * mtop ut 结束
 */
- (void) utEnd;
 
/*!
 * mtop ut json解析开始
 */
- (void) utJsonParseStartTime;
 
/*!
 * mtop ut json解析结束
 */
- (void) utJsonParseEndTime;
 
/*!
 * 取消
 */
- (void) cancel;
 
/*!
 *
 */
- (BOOL) isNeedCallback;
 
@end