client.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #ifndef KBF_HTTP_CLIENT_H
  2. #define KBF_HTTP_CLIENT_H
  3. #include <string>
  4. #include <memory>
  5. #include <esp_err.h>
  6. #include <esp_http_client.h>
  7. #include "kbf/http/common.h"
  8. using std::string;
  9. using std::shared_ptr;
  10. namespace kbf::http {
  11. /**
  12. * @brief HTTP Client.
  13. *
  14. * @note May only perform 1 request at a time, but can be (and should be) reused afterwards.
  15. */
  16. class Client {
  17. public:
  18. /** @brief Tag used for logging. */
  19. static constexpr const char *TAG = "kbf::http::Client";
  20. /**
  21. * @brief Constructor.
  22. *
  23. * @warning Async mode is only supported by IDF for HTTPS.
  24. *
  25. * @param async perform request asynchronously; default is false
  26. */
  27. explicit Client(bool async = false);
  28. /**
  29. * @brief Destructor.
  30. *
  31. * Calls esp_http_client_cleanup().
  32. */
  33. ~Client();
  34. /**
  35. * @brief Performs an HTTP GET.
  36. *
  37. * Calls onSuccess() when a response is received (even if it's a HTTP 4XX), or onError() on failure
  38. * (e.g. socket error)
  39. *
  40. * @param url_param url to fetch
  41. */
  42. shared_ptr<Response> get(const string &url_param);
  43. /**
  44. * @brief Called on HTTP_EVENT_ERROR event.
  45. *
  46. * @note This will be called for low-level errors (e.g. socket error). If the request went through normally,
  47. * but the server responded with an error code (HTTP 4XX), onSuccess() will be called instead.
  48. *
  49. * @param client reference to the Client that issued the request
  50. */
  51. void (*onError)(Client &client) = nullptr;
  52. /**
  53. * @brief Called after the response is received.
  54. *
  55. * @note "Success" here means that the request went through and a response was received.
  56. * This method will be called even if the server returned an error status (HTTP 4XX)
  57. *
  58. * @param client reference to the Client that issued the request
  59. * @param response struct containing the HTTP response data
  60. */
  61. void (*onSuccess)(Client &client, const Response &response) = nullptr;
  62. /**
  63. * @brief Returns whether a request is currently in progress.
  64. *
  65. * @return true if a request is currently in progress; false otherwise
  66. */
  67. [[nodiscard]] bool isRunning() const { return running; }
  68. private:
  69. void init();
  70. static void updateResponse(Client &client);
  71. /**
  72. * @brief Processes events from esp_http_client.
  73. *
  74. * @return
  75. */
  76. static esp_err_t handleHttpEvent(esp_http_client_event_t *);
  77. esp_http_client_handle_t handle{};
  78. string buffer;
  79. shared_ptr<Response> response;
  80. bool running = false;
  81. bool retry = false;
  82. bool async;
  83. };
  84. }
  85. #endif //KBF_HTTP_CLIENT_H