FLEXNetworkTransaction.m 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. //
  2. // FLEXNetworkTransaction.m
  3. // Flipboard
  4. //
  5. // Created by Ryan Olson on 2/8/15.
  6. // Copyright (c) 2020 FLEX Team. All rights reserved.
  7. //
  8. #import "FLEXNetworkTransaction.h"
  9. @interface FLEXNetworkTransaction ()
  10. @property (nonatomic, readwrite) NSData *cachedRequestBody;
  11. @end
  12. @implementation FLEXNetworkTransaction
  13. - (NSString *)description {
  14. NSString *description = [super description];
  15. description = [description stringByAppendingFormat:@" id = %@;", self.requestID];
  16. description = [description stringByAppendingFormat:@" url = %@;", self.request.URL];
  17. description = [description stringByAppendingFormat:@" duration = %f;", self.duration];
  18. description = [description stringByAppendingFormat:@" receivedDataLength = %lld", self.receivedDataLength];
  19. return description;
  20. }
  21. - (NSData *)cachedRequestBody {
  22. if (!_cachedRequestBody) {
  23. if (self.request.HTTPBody != nil) {
  24. _cachedRequestBody = self.request.HTTPBody;
  25. } else if ([self.request.HTTPBodyStream conformsToProtocol:@protocol(NSCopying)]) {
  26. NSInputStream *bodyStream = [self.request.HTTPBodyStream copy];
  27. const NSUInteger bufferSize = 1024;
  28. uint8_t buffer[bufferSize];
  29. NSMutableData *data = [NSMutableData new];
  30. [bodyStream open];
  31. NSInteger readBytes = 0;
  32. do {
  33. readBytes = [bodyStream read:buffer maxLength:bufferSize];
  34. [data appendBytes:buffer length:readBytes];
  35. } while (readBytes > 0);
  36. [bodyStream close];
  37. _cachedRequestBody = data;
  38. }
  39. }
  40. return _cachedRequestBody;
  41. }
  42. + (NSString *)readableStringFromTransactionState:(FLEXNetworkTransactionState)state {
  43. NSString *readableString = nil;
  44. switch (state) {
  45. case FLEXNetworkTransactionStateUnstarted:
  46. readableString = @"Unstarted";
  47. break;
  48. case FLEXNetworkTransactionStateAwaitingResponse:
  49. readableString = @"Awaiting Response";
  50. break;
  51. case FLEXNetworkTransactionStateReceivingData:
  52. readableString = @"Receiving Data";
  53. break;
  54. case FLEXNetworkTransactionStateFinished:
  55. readableString = @"Finished";
  56. break;
  57. case FLEXNetworkTransactionStateFailed:
  58. readableString = @"Failed";
  59. break;
  60. }
  61. return readableString;
  62. }
  63. @end