AFNPhalApiClient.m 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. //
  2. // AFNPhalApiClient.m
  3. // PhalApiClientDemo
  4. //
  5. // Created by Aevit on 15/10/18.
  6. // Copyright © 2015年 Aevit. All rights reserved.
  7. //
  8. #import "AFNPhalApiClient.h"
  9. #import "PhalApiClientFilter.h"
  10. @implementation AFNPhalApiClient
  11. #pragma mark - generate a client
  12. /**
  13. * 生成单例
  14. *
  15. * @return 单例PhalApiClient
  16. */
  17. + (AFNPhalApiClient*)sharedClient {
  18. static AFNPhalApiClient *_sharedClient;
  19. static dispatch_once_t onceToken;
  20. dispatch_once(&onceToken, ^{
  21. _sharedClient = [[AFNPhalApiClient alloc] init];
  22. });
  23. return _sharedClient;
  24. }
  25. /**
  26. * 生成非单例
  27. *
  28. * @return 非单例PhalApiClient
  29. */
  30. + (AFNPhalApiClient*)create {
  31. AFNPhalApiClient *client = [[AFNPhalApiClient alloc] init];
  32. return client;
  33. }
  34. #pragma mark - request
  35. /**
  36. * 发起接口请求(POST请求)
  37. *
  38. * @param completeBlock 请求成功的回调
  39. * @param failureBlock 请求失败的回调
  40. *
  41. * @return 请求实例
  42. */
  43. - (id)request:(HttpCompleteBlock)completeBlock failureBlock:(HttpFailureBlock)failureBlock {
  44. return [self requestWithFormDataBlock:nil completeBlock:completeBlock failureBlock:failureBlock];
  45. }
  46. /**
  47. * 发起接口请求(提交表单)
  48. *
  49. * @param formDataBlock 表单内容
  50. * @param completeBlock 请求成功的回调
  51. * @param failureBlock 请求失败的回调
  52. *
  53. * @return 请求实例
  54. */
  55. - (id)requestWithFormDataBlock:(FormDataBlock)formDataBlock completeBlock:(HttpCompleteBlock)completeBlock failureBlock:(HttpFailureBlock)failureBlock {
  56. if (self.filter) {
  57. // 过滤器,可生成签名验证等
  58. [self.filter filter:self.service params:self.params];
  59. }
  60. NSString *url = self.host;
  61. if (self.service && self.service.length > 0) {
  62. // 一般来说,建议service以GET方式写在url里,其他所有参数,统一用POST方式
  63. url = [NSString stringWithFormat:@"%@?service=%@", self.host, self.service];
  64. }
  65. AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
  66. manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html", @"text/plain", @"text/json", @"text/xml", nil];
  67. manager.requestSerializer.timeoutInterval = self.timeout;
  68. AFHTTPRequestOperation *operation = [manager POST:url parameters:self.params constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) {
  69. if (formDataBlock) {
  70. formDataBlock(formData);
  71. }
  72. } success:^(AFHTTPRequestOperation * _Nonnull operation, id _Nonnull responseObject) {
  73. // 默认以JSON返回,如需其他格式(如XML,请上google搜索"AFNetworking XML"相关资料即可)
  74. if (completeBlock) {
  75. completeBlock(responseObject);
  76. }
  77. } failure:^(AFHTTPRequestOperation * _Nonnull operation, NSError * _Nonnull error) {
  78. if (error.code == NSURLErrorTimedOut) {
  79. NSDictionary *timeoutDict = @{@"ret": @408, @"data": @{}, @"msg": @"Request Timeout"};
  80. failureBlock(timeoutDict);
  81. return ;
  82. }
  83. if (failureBlock) {
  84. failureBlock(error);
  85. }
  86. }];
  87. return operation;
  88. }
  89. @end