PhalApiClient.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. //
  2. // PhalApiClient.h
  3. // PhalApiClientDemo
  4. //
  5. // Created by Aevit on 15/10/17.
  6. // Copyright © 2015年 Aevit. All rights reserved.
  7. //
  8. #import <Foundation/Foundation.h>
  9. #import "PhalApiClientFilter.h"
  10. #if 1
  11. # define PALog(x, ...) NSLog(x, ## __VA_ARGS__);
  12. #else
  13. # define PALog(x, ...)
  14. #endif
  15. typedef void(^HttpCompleteBlock)(id resultObject);
  16. typedef void(^HttpFailureBlock)(id error);
  17. typedef void(^FormDataBlock)(id formData);
  18. @interface PhalApiClient : NSObject
  19. @property (nonatomic, copy) NSString *host;
  20. @property (nonatomic, copy) NSString *service;
  21. @property (nonatomic, strong) NSMutableDictionary *params;
  22. @property (nonatomic, assign) float timeout;
  23. @property (nonatomic, strong) PhalApiClientFilter *filter;
  24. #pragma mark - generate a client
  25. /**
  26. * 生成单例
  27. *
  28. * @return 单例PhalApiClient
  29. */
  30. + (PhalApiClient*)sharedClient;
  31. /**
  32. * 生成非单例
  33. *
  34. * @return 非单例PhalApiClient
  35. */
  36. + (PhalApiClient*)create;
  37. #pragma mark - configure url and params
  38. /**
  39. * 重复查询时须重置请求状态,包括接口服务名称、接口参数和超时时间
  40. *
  41. * @return PhalApiClient实例
  42. */
  43. - (PhalApiClient*)reset;
  44. /**
  45. * 设置接口域名
  46. * 如果整个客户端只有一个接口host,可以在"commonInit"方法设置好host,这样外部就可以不用调用withHost方法了
  47. *
  48. * @param host 域名
  49. *
  50. * @return PhalApiClient实例
  51. */
  52. - (PhalApiClient*)withHost:(NSString*)host;
  53. /**
  54. * 设置将在调用的接口服务名称,如:Default.Index
  55. *
  56. * @param service 接口服务名称
  57. *
  58. * @return PhalApiClient实例
  59. */
  60. - (PhalApiClient*)withService:(NSString*)service;
  61. /**
  62. * 设置接口参数,此方法是唯一一个可以多次调用并累加参数的操作
  63. *
  64. * @param params 参数
  65. *
  66. * @return PhalApiClient实例
  67. */
  68. - (PhalApiClient*)withParams:(NSDictionary*)params;
  69. /**
  70. * 设置过滤器,与服务器的DI().filter对应
  71. *
  72. * @return PhalApiClient实例
  73. */
  74. - (PhalApiClient*)withFilter:(PhalApiClientFilter*)filter;
  75. /**
  76. * 设置超时时间,单位毫秒
  77. *
  78. * @param timeout 超时时间,单位秒
  79. *
  80. * @return PhalApiClient实例
  81. */
  82. - (PhalApiClient*)withTimeout:(float)timeout;
  83. #pragma mark - request
  84. /**
  85. * 发起接口请求(POST请求)
  86. *
  87. * @param completeBlock 请求成功的回调
  88. * @param failureBlock 请求失败的回调
  89. *
  90. * @return 请求实例
  91. */
  92. - (id)request:(HttpCompleteBlock)completeBlock failureBlock:(HttpFailureBlock)failureBlock;
  93. /**
  94. * 发起接口请求(提交表单)
  95. *
  96. * @param formDataBlock 表单内容
  97. * @param completeBlock 请求成功的回调
  98. * @param failureBlock 请求失败的回调
  99. *
  100. * @return 请求实例
  101. */
  102. - (id)requestWithFormDataBlock:(FormDataBlock)formDataBlock completeBlock:(HttpCompleteBlock)completeBlock failureBlock:(HttpFailureBlock)failureBlock;
  103. #pragma mark - utils
  104. /**
  105. * 以get形式打印整个url,方便服务端开发人员调试
  106. *
  107. * @return 整个url
  108. */
  109. - (NSString*)printTotalUrlStr;
  110. @end