PhalApiClient.m 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. //
  2. // PhalApiClient.m
  3. // PhalApiClientDemo
  4. //
  5. // Created by Aevit on 15/10/17.
  6. // Copyright © 2015年 Aevit. All rights reserved.
  7. //
  8. #import "PhalApiClient.h"
  9. @interface PhalApiClient()
  10. @end
  11. @implementation PhalApiClient
  12. - (instancetype)init {
  13. self = [super init];
  14. if (self) {
  15. [self commonInit];
  16. }
  17. return self;
  18. }
  19. - (void)commonInit {
  20. [self reset];
  21. /**
  22. * 如果整个客户端只有一个接口host,可以在reset后设置好host,这样外部就可以不用调用withHost方法了
  23. * e.g. [self withHost:@"http://api.your_host.com/project_name/"];
  24. */
  25. }
  26. #pragma mark - generate a client
  27. /**
  28. * 生成单例
  29. *
  30. * @return 单例PhalApiClient
  31. */
  32. + (PhalApiClient*)sharedClient {
  33. static PhalApiClient *_sharedClient;
  34. static dispatch_once_t onceToken;
  35. dispatch_once(&onceToken, ^{
  36. _sharedClient = [[PhalApiClient alloc] init];
  37. });
  38. return _sharedClient;
  39. }
  40. /**
  41. * 生成非单例
  42. *
  43. * @return 非单例PhalApiClient
  44. */
  45. + (PhalApiClient*)create {
  46. PhalApiClient *client = [[PhalApiClient alloc] init];
  47. return client;
  48. }
  49. #pragma mark - configure url and params
  50. /**
  51. * 重复查询时须重置请求状态,包括接口服务名称、接口参数和超时时间
  52. *
  53. * @return PhalApiClient实例
  54. */
  55. - (PhalApiClient*)reset {
  56. self.host = nil;
  57. self.service = nil;
  58. self.params = nil;
  59. self.timeout = 0;
  60. return self;
  61. }
  62. /**
  63. * 设置接口域名
  64. * 如果整个客户端只有一个接口host,可以在"commonInit"方法设置好host,这样外部就可以不用调用withHost方法了
  65. *
  66. * @param host 域名
  67. *
  68. * @return PhalApiClient实例
  69. */
  70. - (PhalApiClient*)withHost:(NSString*)host {
  71. self.host = nil;
  72. self.host = host;
  73. return self;
  74. }
  75. - (NSString *)host {
  76. if ([[_host substringFromIndex:(_host.length - 1)] isEqualToString:@"/"]) {
  77. return _host;
  78. }
  79. _host = [NSString stringWithFormat:@"%@/", _host];
  80. return _host;
  81. }
  82. /**
  83. * 设置将在调用的接口服务名称,如:Default.Index
  84. *
  85. * @param service 接口服务名称
  86. *
  87. * @return PhalApiClient实例
  88. */
  89. - (PhalApiClient*)withService:(NSString*)service {
  90. self.service = nil;
  91. self.service = service;
  92. return self;
  93. }
  94. /**
  95. * 设置接口参数,此方法是唯一一个可以多次调用并累加参数的操作
  96. *
  97. * @param params 参数
  98. *
  99. * @return PhalApiClient实例
  100. */
  101. - (PhalApiClient*)withParams:(NSDictionary*)params {
  102. self.params = nil;
  103. if (!params) {
  104. return self;
  105. }
  106. self.params = [NSMutableDictionary dictionaryWithDictionary:params];
  107. return self;
  108. }
  109. /**
  110. * 设置过滤器,与服务器的DI().filter对应
  111. *
  112. * @return PhalApiClient实例
  113. */
  114. - (PhalApiClient*)withFilter:(PhalApiClientFilter*)filter {
  115. self.filter = filter;
  116. return self;
  117. }
  118. /**
  119. * 设置超时时间,单位毫秒
  120. *
  121. * @param timeout 超时时间,单位秒
  122. *
  123. * @return PhalApiClient实例
  124. */
  125. - (PhalApiClient*)withTimeout:(float)timeout {
  126. self.timeout = timeout;
  127. return self;
  128. }
  129. - (float)timeout {
  130. // 默认60s超时
  131. return (_timeout > 0 ? _timeout : 60);
  132. }
  133. #pragma mark - request
  134. /**
  135. * 发起接口请求(POST请求)
  136. *
  137. * @param completeBlock 请求成功的回调
  138. * @param failureBlock 请求失败的回调
  139. *
  140. * @return 请求实例
  141. */
  142. - (id)request:(HttpCompleteBlock)completeBlock failureBlock:(HttpFailureBlock)failureBlock {
  143. /**
  144. * 请求方式,可以使用系统的NSURLSession,或第三方的网络请求库等
  145. * 这里我们使用第三方的AFNetworking,进行二次封装(https://github.com/AFNetworking/AFNetworking)
  146. * 新建子类(AFNPhalApiClient,继承自PhalApiClient),重写此request方法
  147. * 如需使用其他网络请求方式(如NSURLSession),请继承自PhalApiClient,重写此request即可,可参考AFNPhalApiClient
  148. */
  149. return [self requestWithFormDataBlock:nil completeBlock:completeBlock failureBlock:failureBlock];
  150. }
  151. /**
  152. * 发起接口请求(提交表单)
  153. *
  154. * @param formDataBlock 表单内容
  155. * @param completeBlock 请求成功的回调
  156. * @param failureBlock 请求失败的回调
  157. *
  158. * @return 请求实例
  159. */
  160. - (id)requestWithFormDataBlock:(FormDataBlock)formDataBlock completeBlock:(HttpCompleteBlock)completeBlock failureBlock:(HttpFailureBlock)failureBlock {
  161. /**
  162. * 请求方式,可以使用系统的NSURLSession,第三方的网络请求库等
  163. * 这里我们使用第三方的AFNetworking,进行二次封装(https://github.com/AFNetworking/AFNetworking)
  164. * 新建子类(AFNPhalApiClient,继承自PhalApiClient),重写此request方法
  165. * 如需使用其他网络请求方式(如NSURLSession),请继承自PhalApiClient,重写此request即可,可参考AFNPhalApiClient
  166. */
  167. return nil;
  168. }
  169. #pragma mark - utils
  170. /**
  171. * 以get形式打印整个url,方便服务端开发人员调试
  172. *
  173. * @return 整个url
  174. */
  175. - (NSString*)printTotalUrlStr {
  176. if (!_host || _host.length <= 0) {
  177. return @"empty host";
  178. }
  179. NSMutableString *finalStr = [NSMutableString stringWithString:_host];
  180. [finalStr appendFormat:@"?service=%@&", _service];
  181. if (_params) {
  182. for (NSString *key in [_params allKeys]) {
  183. [finalStr appendString:key];
  184. [finalStr appendString:@"="];
  185. id value = [_params objectForKey:key];
  186. NSString *valueStr = @"";
  187. if ([value isKindOfClass:[NSNumber class]]) {
  188. valueStr = [value stringValue];
  189. } else if ([value isKindOfClass:[NSString class]]) {
  190. valueStr = value;
  191. } else {
  192. PALog(@"什么鬼?!");
  193. }
  194. [finalStr appendString:valueStr];
  195. [finalStr appendString:@"&"];
  196. }
  197. }
  198. return [finalStr substringWithRange:NSMakeRange(0, finalStr.length - 1)];
  199. }
  200. @end