AFHTTPRequestOperationManager+Synchronous.m 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #import "AFHTTPRequestOperationManager+Synchronous.h"
  2. @implementation AFHTTPRequestOperationManager (Synchronous)
  3. - (id)synchronouslyPerformMethod:(NSString *)method
  4. URLString:(NSString *)URLString
  5. parameters:(NSDictionary *)parameters
  6. operation:(AFHTTPRequestOperation *__autoreleasing *)operationPtr
  7. error:(NSError *__autoreleasing *)outError {
  8. NSMutableURLRequest *request = [self.requestSerializer requestWithMethod:method URLString:[[NSURL URLWithString:URLString relativeToURL:self.baseURL] absoluteString] parameters:parameters error:nil];
  9. AFHTTPRequestOperation *op = [self HTTPRequestOperationWithRequest:request
  10. success:nil
  11. failure:nil];
  12. [op start];
  13. [op waitUntilFinished];
  14. if (operationPtr != nil) *operationPtr = op;
  15. // Must call responseObject before checking the error
  16. id responseObject = [op responseObject];
  17. if (outError != nil) *outError = [op error];
  18. return responseObject;
  19. }
  20. - (id)syncGET:(NSString *)URLString
  21. parameters:(NSDictionary *)parameters
  22. operation:(AFHTTPRequestOperation *__autoreleasing *)operationPtr
  23. error:(NSError *__autoreleasing *)outError
  24. {
  25. return [self synchronouslyPerformMethod:@"GET" URLString:URLString parameters:parameters operation:operationPtr error:outError];
  26. }
  27. - (id)syncPOST:(NSString *)URLString
  28. parameters:(NSDictionary *)parameters
  29. operation:(AFHTTPRequestOperation *__autoreleasing *) operationPtr
  30. error:(NSError *__autoreleasing *) outError
  31. {
  32. return [self synchronouslyPerformMethod:@"POST" URLString:URLString parameters:parameters operation:operationPtr error:outError];
  33. }
  34. - (id)syncPUT:(NSString *)URLString
  35. parameters:(NSDictionary *)parameters
  36. operation:(AFHTTPRequestOperation *__autoreleasing *) operationPtr
  37. error:(NSError *__autoreleasing *) outError
  38. {
  39. return [self synchronouslyPerformMethod:@"PUT" URLString:URLString parameters:parameters operation:operationPtr error:outError];
  40. }
  41. - (id)syncDELETE:(NSString *)URLString
  42. parameters:(NSDictionary *)parameters
  43. operation:(AFHTTPRequestOperation *__autoreleasing *) operationPtr
  44. error:(NSError *__autoreleasing *) outError
  45. {
  46. return [self synchronouslyPerformMethod:@"DELETE" URLString:URLString parameters:parameters operation:operationPtr error:outError];
  47. }
  48. - (id)syncPATCH:(NSString *)URLString
  49. parameters:(NSDictionary *)parameters
  50. operation:(AFHTTPRequestOperation *__autoreleasing *) operationPtr
  51. error:(NSError *__autoreleasing *) outError
  52. {
  53. return [self synchronouslyPerformMethod:@"PATCH" URLString:URLString parameters:parameters operation:operationPtr error:outError];
  54. }
  55. @end