猎豆优选

TBSDKRequest.h 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. // '########'########::'######:'########:'##:::'##:
  2. // ... ##..::##.... ##'##... ##:##.... ##:##::'##::
  3. // ::: ##::::##:::: ##:##:::..::##:::: ##:##:'##:::
  4. // ::: ##::::########:. ######::##:::: ##:#####::::
  5. // ::: ##::::##.... ##:..... ##:##:::: ##:##. ##:::
  6. // ::: ##::::##:::: ##'##::: ##:##:::: ##:##:. ##::
  7. // ::: ##::::########:. ######::########::##::. ##:
  8. // :::..::::........:::......::........::..::::..::
  9. //
  10. // Created by 亿刀 on 13-1-28.
  11. // Copyright (c) 2013年 Taobao. All rights reserved.
  12. //
  13. #import <Foundation/Foundation.h>
  14. #import <MtopSDK/TBSDKMTOPEnvConfig.h>
  15. /*!
  16. * System parameters
  17. */
  18. #define SYS_PARAMETER_API_NAME @"api"
  19. #define SYS_PARAMETER_API_VERSOIN @"v"
  20. #define SYS_PARAMETER_SECURITY_APPKEY @"security_appkey"
  21. #define SYS_PARAMETER_APPKEY @"appkey"
  22. #define SYS_PARAMETER_SID @"sid"
  23. #define SYS_PARAMETER_TTID @"ttid"
  24. #define SYS_PARAMETER_TIMESTAMP @"timestamp"
  25. #define SYS_PARAMETER_DEVID @"devid"
  26. #define SYS_PARAMETER_LOCATION @"location"
  27. /**
  28. * 系统http headers
  29. */
  30. #define SYS_HTTP_HEADER_PV @"x-pv"
  31. #define SYS_HTTP_HEADER_APPKEY @"x-appkey"
  32. #define SYS_HTTP_HEADER_SID @"x-sid"
  33. #define SYS_HTTP_HEADER_UID @"x-uid"
  34. #define SYS_HTTP_HEADER_TTID @"x-ttid"
  35. #define SYS_HTTP_HEADER_TIMESTAMP @"x-t"
  36. #define SYS_HTTP_HEADER_DEVID @"x-devid"
  37. #define SYS_HTTP_HEADER_LOCATION @"x-location"
  38. #define SYS_HTTP_HEADER_SIGN @"x-sign"
  39. #define SYS_HTTP_HEADER_UTDID @"x-utdid"
  40. #define SYS_HTTP_HEADER_REQBIZ @"x-reqbiz-ext"
  41. #define SYS_HTTP_HEADER_APP_VER @"x-app-ver"
  42. #define SYS_HTTP_HEADER_MINI_WUA @"x-mini-wua"
  43. #define SYS_HTTP_HEADER_WUAT @"x-wuat"
  44. #define SYS_HTTP_HEADER_FEATURES @"x-features"
  45. #define SYS_HTTP_HEADER_PAGEURL @"x-page-url"
  46. #define SYS_HTTP_HEADER_PAGENAME @"x-page-name"
  47. /**
  48. * 开放体系头
  49. */
  50. #define SYS_HTTP_HEADER_MINI_APPKEY @"x-mini-appkey"
  51. #define SYS_HTTP_HEADER_REQ_APPKEY @"x-req-appkey"
  52. #define SYS_HTTP_HEADER_OPEN_BIZ_CODE @"x-open-biz"
  53. #define SYS_HTTP_HEADER_OPEN_BIZ_DATA @"x-open-biz-data"
  54. /**
  55. * open api ext params,sdk use‘s property
  56. */
  57. #define SYS_HTTP_HEADER_EXTDATA @"x-extdata"
  58. #define SYS_HTTP_HEADER_EXTTYPE @"x-exttype"
  59. #define SYS_HTTP_HEADER_SECURITY_APPKEY @"x-s-appkey"
  60. #define SYS_HTTP_HEADER_ACT @"x-act"
  61. /**
  62. * security guard headers
  63. */
  64. #define SYS_HTTP_HEADER_SIGNTYPE @"x-sign-type"
  65. #define SYS_HTTP_HEADER_UMT @"x-umt"
  66. #define SYS_HTTP_HEADER_SGEXT @"x-sgext"
  67. #define SYS_HTTP_HEADER_BAXIA_VERSION @"x-bx-version"
  68. /**
  69. * 协议版本
  70. */
  71. #define M_PV_INNER @"6.2"
  72. #define M_PV_OPEN @"1.3"
  73. #define SYS_PARAMETER_DATA @"data"
  74. @protocol TBSDKConnectionProtocol;
  75. typedef enum _TBSDKRequestErrorType
  76. {
  77. TBSDKRequestErrorTypeConnectionFailure = 1, //网络连接失败或者无网络
  78. TBSDKRequestErrorTypeTimedOut = 2, //网络连接超时
  79. TBSDKRequestErrorTypeUserInvalid = 3, // user sid invalid time
  80. TBSDKREquestTooMuchRedirectionErrorType,
  81. } TBSDKRequestErrorType;
  82. typedef NS_ENUM(NSInteger,TBMtopUnitStrategy){
  83. TBMtopUnitStrategyGuideUnit = 1,
  84. TBMtopUnitStrategyTradeUnit
  85. };
  86. @protocol TBSDKRequestDataSource;
  87. @protocol TBSDKRequestDelegate;
  88. @protocol TBSDKRequestProgressProtocol;
  89. //! 网络请求默认超时时间
  90. #define TIME_OUT_SCONDS 10.
  91. @class TBSDKConnection;
  92. /** TBSDK的真正的网络请求类
  93. *
  94. * 此类主要做联网控制,如超时时间、最大联网个数、取消所有网络连接等。
  95. * TBSDK的真正的网络请求类,此类通过delegate来获取网络请求
  96. * 的配置参数(get或post,post的value,需要上传的数据)。
  97. *
  98. */
  99. @interface TBSDKRequest : NSObject
  100. @property (nonatomic, strong) NSError *error;
  101. //! 代理对象
  102. @property (nonatomic, weak) id<TBSDKRequestDelegate> delegate;
  103. //! 代理对象
  104. @property (nonatomic, weak) id<TBSDKRequestDataSource> dataSource;
  105. //! 上传下载进度代理对象
  106. @property (nonatomic, weak) id<TBSDKRequestProgressProtocol> requestProgressDelegate;
  107. @property (nonatomic, strong, readonly) TBSDKConnection *request;
  108. // api request url
  109. @property (nonatomic, strong) NSURL *url;
  110. // api single request custom host
  111. @property (nonatomic, strong) NSString* customHost;
  112. // 请求级别设置线上、预发、日常 custom host (按顺序设置线上、预发、日常域名)
  113. @property (nonatomic, strong) NSArray *customHostList;
  114. //! 请求过期时间,默认10s
  115. @property (nonatomic, assign) NSTimeInterval timeOutSeconds;
  116. //! NSData 对象的请求响应数据
  117. @property (nonatomic, strong) NSData *responseData;
  118. //! NSString 对象的请求响应数据
  119. @property (nonatomic, strong) NSString *responseString;
  120. //! http responseStatusCode
  121. @property (nonatomic, assign) int responseStatusCode;
  122. @property (nonatomic, strong, readonly) NSDictionary *responseHeaders;
  123. @property (nonatomic, strong) NSDictionary *requestHeaders;
  124. //! 默认关闭
  125. @property (nonatomic, assign) BOOL useHTTPCache;
  126. @property (nonatomic, assign) BOOL forceRefresh;
  127. @property (nonatomic, strong) NSString *apiMethod;
  128. @property (nonatomic) BOOL isAvalancheCache;
  129. // post method of body gzip size
  130. @property (nonatomic, assign) int gzipLimitedSize;
  131. @property (nonatomic, strong) NSArray *blackCacheKeyParam;
  132. @property (nonatomic, strong) NSMutableDictionary *networkDataSet;
  133. @property (nonatomic) CFAbsoluteTime operationQueueTime;
  134. @property (nonatomic) CFAbsoluteTime networkFinishedTime;
  135. // base property
  136. @property(strong, nonatomic) NSString* userId; // app user's id
  137. @property(strong, nonatomic) NSString* apiName; // 网关URL
  138. @property(strong, nonatomic) NSString* apiVersion; // 网关URL
  139. @property(strong, nonatomic) NSMutableDictionary* protocolParameters; // 协议参数
  140. @property(strong, nonatomic) NSMutableDictionary* httpHeaders; // http请求头
  141. @property(strong, nonatomic) NSMutableDictionary* extParameters; // 扩展参数 (和data参数平级)
  142. @property(strong, nonatomic) NSMutableDictionary* bizParameters; // 业务参数
  143. @property(strong, nonatomic) NSMutableDictionary* priorityData; // 排队SDK优先级参数
  144. @property(strong, nonatomic) NSMutableDictionary* custhomHeaders; // 自定义不进行urlencode的http请求头
  145. @property(strong,nonatomic) NSMutableArray* uploadFiles;
  146. @property(assign, nonatomic) BOOL isUseHttps; // 是否启用https
  147. @property(assign, nonatomic) BOOL isUseHttpPost; // 是否使用http post
  148. @property(strong, nonatomic) NSString* unitPrefix; // 单元化前缀
  149. @property(assign, nonatomic) MtopType type; // 实例类型
  150. @property(strong, nonatomic) NSString *instanceID; // 实例ID
  151. @property(strong, nonatomic) id context; // 调用方可以使用的上下文
  152. @property(assign, nonatomic) BOOL isEnableWua; // 是否启用wua
  153. @property(assign, nonatomic) int wuaFlag; // 启用wua类型标识
  154. @property(assign, nonatomic) BOOL priorityFlag; // API请求优先级flag(针对排队SDK)
  155. @property(assign, nonatomic) TBMtopUnitStrategy UnitStrategy; // 设置请求级别单元化域名策略
  156. @property(assign, nonatomic) BOOL unHostedByNetWork; // 是否不要被网络库接管 默认 NO
  157. @property(strong, nonatomic) NSError *rawError; // 底层回调原始error(errorCode为网络库返回原始值)
  158. /*!
  159. * 创建一个MtopRequest
  160. * @param apiName API名称
  161. * @param apiVersion API版本
  162. * @return
  163. * MtopRequest
  164. */
  165. - (TBSDKRequest*) initWithApiName: (NSString*) apiName apiVersion: (NSString*) apiVersion;
  166. /*!
  167. * 添加一个http请求头
  168. * @param value HTTP头value
  169. * @param key HTTP头key
  170. */
  171. - (void) addHttpHeader: (NSString*) value forKey: (NSString*) key;
  172. /*!
  173. * 添加一个http请求头 (不做UrlEncode)
  174. * @param value HTTP头value
  175. * @param key HTTP头key
  176. */
  177. - (void) addCustomHttpHeader:(NSString *)value forKey:(NSString *)key;
  178. /*!
  179. * 添加一个协议参数
  180. * @param value 参数值
  181. * @param key 参数名
  182. */
  183. - (void) addProtocolParameter: (NSString*) value forKey: (NSString*) key;
  184. /*!
  185. * 添加一个扩展参数和data参数平级
  186. * @param value 参数值
  187. * @param key 参数名
  188. */
  189. - (void) addExtParameter: (id) value forKey: (NSString*) key;
  190. /*!
  191. * 移除一个扩展参数
  192. * @param key
  193. */
  194. - (void) removeExtParameter:(NSString*) key;
  195. /*!
  196. * 添加一个业务参数(业务级别参数)
  197. * @param value 参数值
  198. * @param key 参数名
  199. */
  200. - (void) addBizParameter: (id) value forKey: (NSString*) key;
  201. /*!
  202. * 移除一个业务参数
  203. * @param key
  204. */
  205. - (void) removeBizParameter:(NSString*) key;
  206. /**
  207. * 设置API请求优先级参数
  208. *
  209. * @param value 参数值
  210. * @param key 参数名
  211. */
  212. - (void) addPriorityData:(id) value forKey:(NSString*) key;
  213. /*!
  214. * 添加要上传的文件
  215. * @param data 文件数据
  216. * @param fileName 本地文件名(全路径)
  217. * @param forKey form field 的 key
  218. *
  219. */
  220. - (void) addUploadFileWithData: (NSData*) data fileName: (NSString*) fileName forKey: (NSString*) key;
  221. //! 取消TBSDK中的所有的网络连接
  222. + (void)cancelTBSDKAllRequest;
  223. //! 开始同步网络请求, 当网络请求完成或失败后返回
  224. - (BOOL)startSynchronous;
  225. //! 开始异步请求
  226. - (void)startAsynchronous;
  227. //! 退出网路请求,并把delegate设置为空
  228. - (void)cancelRequest;
  229. /*!
  230. * 重置
  231. */
  232. - (void) reset;
  233. @end