FSXMLHttpRequest.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * This file is part of the FreeStreamer project,
  3. * (C)Copyright 2011-2018 Matias Muhonen <mmu@iki.fi> 穆马帝
  4. * See the file ''LICENSE'' for using the code.
  5. *
  6. * https://github.com/muhku/FreeStreamer
  7. */
  8. #import <Foundation/Foundation.h>
  9. typedef struct _xmlDoc xmlDoc;
  10. typedef xmlDoc *xmlDocPtr;
  11. typedef struct _xmlNode xmlNode;
  12. typedef xmlNode *xmlNodePtr;
  13. /**
  14. * XML HTTP request error status.
  15. */
  16. typedef NS_ENUM(NSInteger, FSXMLHttpRequestError) {
  17. /**
  18. * No error.
  19. */
  20. FSXMLHttpRequestError_NoError = 0,
  21. /**
  22. * Connection failed.
  23. */
  24. FSXMLHttpRequestError_Connection_Failed,
  25. /**
  26. * Invalid HTTP status.
  27. */
  28. FSXMLHttpRequestError_Invalid_Http_Status,
  29. /**
  30. * XML parser failed.
  31. */
  32. FSXMLHttpRequestError_XML_Parser_Failed
  33. };
  34. /**
  35. * FSXMLHttpRequest is a class for retrieving data in the XML
  36. * format over a HTTP or HTTPS connection. It provides
  37. * the necessary foundation for parsing the retrieved XML data.
  38. * This class is not meant to be used directly but subclassed
  39. * to a specific requests.
  40. *
  41. * The usage pattern is the following:
  42. *
  43. * 1. Specify the URL with the url property.
  44. * 2. Define the onCompletion and onFailure handlers.
  45. * 3. Call the start method.
  46. */
  47. @interface FSXMLHttpRequest : NSObject {
  48. NSURLSessionTask *_task;
  49. xmlDocPtr _xmlDocument;
  50. NSDateFormatter *_dateFormatter;
  51. }
  52. /**
  53. * The URL of the request.
  54. */
  55. @property (nonatomic,copy) NSURL *url;
  56. /**
  57. * Called upon completion of the request.
  58. */
  59. @property (copy) void (^onCompletion)(void);
  60. /**
  61. * Called upon a failure.
  62. */
  63. @property (copy) void (^onFailure)(void);
  64. /**
  65. * If the request fails, contains the latest error status.
  66. */
  67. @property (readonly) FSXMLHttpRequestError lastError;
  68. /**
  69. * Starts the request.
  70. */
  71. - (void)start;
  72. /**
  73. * Cancels the request.
  74. */
  75. - (void)cancel;
  76. /**
  77. * Performs an XPath query on the parsed XML data.
  78. * Yields a parseXMLNode method call, which must be
  79. * defined in the subclasses.
  80. *
  81. * @param query The XPath query to be performed.
  82. */
  83. - (NSArray *)performXPathQuery:(NSString *)query;
  84. /**
  85. * Retrieves content for the given XML node.
  86. *
  87. * @param node The node for content retreval.
  88. */
  89. - (NSString *)contentForNode:(xmlNodePtr)node;
  90. /**
  91. * Retrieves content for the given XML node attribute.
  92. *
  93. * @param node The node for content retrieval.
  94. * @param attr The attribute from which the content is retrieved.
  95. */
  96. - (NSString *)contentForNodeAttribute:(xmlNodePtr)node attribute:(const char *)attr;
  97. /**
  98. * Retrieves date from the given XML node.
  99. *
  100. * @param node The node for retrieving the date.
  101. */
  102. - (NSDate *)dateFromNode:(xmlNodePtr)node;
  103. @end