猎豆优选

MtopExtRequest.h 16KB

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