酷店

TBSDKRequest.h 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. /*!
  15. * System parameters
  16. */
  17. #define SYS_PARAMETER_API_NAME @"api"
  18. #define SYS_PARAMETER_API_VERSOIN @"v"
  19. #define SYS_PARAMETER_SECURITY_APPKEY @"security_appkey"
  20. #define SYS_PARAMETER_APPKEY @"appkey"
  21. #define SYS_PARAMETER_SID @"sid"
  22. #define SYS_PARAMETER_TTID @"ttid"
  23. #define SYS_PARAMETER_TIMESTAMP @"timestamp"
  24. #define SYS_PARAMETER_DEVID @"devid"
  25. #define SYS_PARAMETER_LOCATION @"location"
  26. /*!
  27. * 系统http headers
  28. */
  29. #define SYS_HTTP_HEADER_PV @"x-pv"
  30. #define SYS_HTTP_HEADER_APPKEY @"x-appkey"
  31. #define SYS_HTTP_HEADER_SID @"x-sid"
  32. #define SYS_HTTP_HEADER_UID @"x-uid"
  33. #define SYS_HTTP_HEADER_TTID @"x-ttid"
  34. #define SYS_HTTP_HEADER_TIMESTAMP @"x-t"
  35. #define SYS_HTTP_HEADER_DEVID @"x-devid"
  36. #define SYS_HTTP_HEADER_LOCATION @"x-location"
  37. #define SYS_HTTP_HEADER_SIGN @"x-sign"
  38. #define SYS_HTTP_HEADER_UTDID @"x-utdid"
  39. #define SYS_HTTP_HEADER_REQBIZ @"x-reqbiz-ext"
  40. #define SYS_HTTP_HEADER_APP_VER @"x-app-ver"
  41. #define SYS_HTTP_HEADER_WUAT @"x-wuat"
  42. /*!
  43. * open api ext params,sdk use‘s property
  44. */
  45. #define SYS_HTTP_HEADER_EXTDATA @"x-extdata"
  46. #define SYS_HTTP_HEADER_EXTTYPE @"x-exttype"
  47. #define SYS_HTT_HEADER_ACT @"x-act"
  48. #define SYS_HTTP_HEADER_FEATURES @"x-features"
  49. /*!
  50. * security guard headers
  51. */
  52. #define SYS_HTTP_HEADER_SIGNTYPE @"x-sign-type"
  53. #define SYS_HTTP_HEADER_UMT @"x-umt"
  54. /*!
  55. * 系统http header 常量
  56. */
  57. #define M_PV @"5.0"
  58. #define SYS_PARAMETER_DATA @"data"
  59. @protocol TBSDKConnectionProtocol;
  60. typedef enum _TBSDKRequestErrorType
  61. {
  62. TBSDKRequestErrorTypeConnectionFailure = 1, //网络连接失败或者无网络
  63. TBSDKRequestErrorTypeTimedOut = 2, //网络连接超时
  64. TBSDKRequestErrorTypeUserInvalid = 3, // user sid invalid time
  65. TBSDKREquestTooMuchRedirectionErrorType,
  66. } TBSDKRequestErrorType;
  67. @protocol TBSDKRequestDataSource;
  68. @protocol TBSDKRequestDelegate;
  69. @protocol TBSDKRequestProgressProtocol;
  70. //! 网络请求默认超时时间
  71. #define TIME_OUT_SCONDS 10.
  72. @class TBSDKConnection;
  73. /** TBSDK的真正的网络请求类
  74. *
  75. * 此类主要做联网控制,如超时时间、最大联网个数、取消所有网络连接等。
  76. * TBSDK的真正的网络请求类,此类通过delegate来获取网络请求
  77. * 的配置参数(get或post,post的value,需要上传的数据)。
  78. *
  79. */
  80. @interface TBSDKRequest : NSObject
  81. @property (nonatomic, strong) NSError *error;
  82. //! 代理对象
  83. @property (nonatomic, weak) id<TBSDKRequestDelegate> delegate;
  84. //! 代理对象
  85. @property (nonatomic, weak) id<TBSDKRequestDataSource> dataSource;
  86. //! 上传下载进度代理对象
  87. @property (nonatomic, weak) id<TBSDKRequestProgressProtocol> requestProgressDelegate;
  88. @property (nonatomic, strong, readonly) TBSDKConnection *request;
  89. // api request url
  90. @property (nonatomic, strong) NSURL *url;
  91. // api single request custom host
  92. @property (nonatomic, strong) NSString* customHost;
  93. //! 请求过期时间,默认10s
  94. @property (nonatomic, unsafe_unretained) NSTimeInterval timeOutSeconds;
  95. //! NSData 对象的请求响应数据
  96. @property (nonatomic, retain) NSData *responseData;
  97. //! NSString 对象的请求响应数据
  98. @property (nonatomic, retain) NSString *responseString;
  99. //! http responseStatusCode
  100. @property (nonatomic, unsafe_unretained) int responseStatusCode;
  101. @property (nonatomic, strong, readonly) NSDictionary *responseHeaders;
  102. @property (nonatomic, strong) NSDictionary *requestHeaders;
  103. //! 默认关闭
  104. @property (nonatomic, unsafe_unretained) BOOL useHTTPCache;
  105. @property (nonatomic, unsafe_unretained) BOOL forceRefresh;
  106. @property (nonatomic, strong) NSString *apiMethod;
  107. @property (nonatomic) BOOL isAvalancheCache;
  108. // post method of body gzip size
  109. @property (nonatomic, unsafe_unretained) int gzipLimitedSize;
  110. @property (nonatomic, strong) NSArray *blackCacheKeyParam;
  111. @property (nonatomic, strong) NSMutableDictionary *networkDataSet;
  112. @property (nonatomic) CFAbsoluteTime operationQueueTime;
  113. @property (nonatomic) CFAbsoluteTime networkFinishedTime;
  114. // base property
  115. @property(strong, nonatomic) NSString* userId; // app user's id
  116. @property(strong, nonatomic) NSString* apiName; // 网关URL
  117. @property(strong, nonatomic) NSString* apiVersion; // 网关URL
  118. @property(strong, nonatomic) NSMutableDictionary* protocolParameters; // 协议参数
  119. @property(strong, nonatomic) NSMutableDictionary* httpHeaders; // http请求头
  120. @property(strong, nonatomic) NSMutableDictionary* extParameters; // 扩展参数 (和data参数平级)
  121. @property(strong, nonatomic) NSMutableDictionary* bizParameters; // 业务参数
  122. @property(strong,nonatomic) NSMutableArray* uploadFiles; //
  123. @property(assign, nonatomic) BOOL isUseHttps; // 是否启用https
  124. @property(assign, nonatomic) BOOL isUseHttpPost; // 是否使用http post
  125. @property(strong, nonatomic) NSString* unitPrefix; // 单元化前缀
  126. @property(strong, nonatomic) id context; // 调用方可以使用的上下文
  127. /*!
  128. * 创建一个MtopRequest
  129. * @param apiName API名称
  130. * @param apiVersion API版本
  131. * @return
  132. * MtopRequest
  133. */
  134. - (TBSDKRequest*) initWithApiName: (NSString*) apiName apiVersion: (NSString*) apiVersion;
  135. /*!
  136. * 添加一个http请求头
  137. * @param value HTTP头value
  138. * @param key HTTP头key
  139. */
  140. - (void) addHttpHeader: (NSString*) value forKey: (NSString*) key;
  141. /*!
  142. * 添加一个协议参数
  143. * @param value 参数值
  144. * @param key 参数名
  145. */
  146. - (void) addProtocolParameter: (NSString*) value forKey: (NSString*) key;
  147. /*!
  148. * 添加一个扩展参数和data参数平级
  149. * @param value 参数值
  150. * @param key 参数名
  151. */
  152. - (void) addExtParameter: (id) value forKey: (NSString*) key;
  153. /*!
  154. * 移除一个扩展参数
  155. * @param key
  156. */
  157. - (void) removeExtParameter:(NSString*) key;
  158. /*!
  159. * 添加一个业务参数(业务级别参数)
  160. * @param value 参数值
  161. * @param key 参数名
  162. */
  163. - (void) addBizParameter: (id) value forKey: (NSString*) key;
  164. /*!
  165. * 移除一个业务参数
  166. * @param key
  167. */
  168. - (void) removeBizParameter:(NSString*) key;
  169. /*!
  170. * 添加要上传的文件
  171. * @param data 文件数据
  172. * @param fileName 本地文件名(全路径)
  173. * @param forKey form field 的 key
  174. *
  175. */
  176. - (void) addUploadFileWithData: (NSData*) data fileName: (NSString*) fileName forKey: (NSString*) key;
  177. //! 取消TBSDK中的所有的网络连接
  178. + (void)cancelTBSDKAllRequest;
  179. //! 开始同步网络请求, 当网络请求完成或失败后返回
  180. - (BOOL)startSynchronous;
  181. //! 开始异步请求
  182. - (void)startAsynchronous;
  183. //! 退出网路请求,并把delegate设置为空
  184. - (void)cancelRequest;
  185. /*!
  186. * 重置
  187. */
  188. - (void) reset;
  189. @end