Moved an internally used enum only to the private section of the class, changed the return type of the sendHttpQuery function from int to an enum which gives more details and remove the usage of magic numbers (line at which the return occured)

This commit is contained in:
anschrammh 2025-11-04 21:37:11 +01:00
parent f44b7486b8
commit 9d4edfc0f0
2 changed files with 11 additions and 9 deletions

View File

@ -86,7 +86,7 @@ boolean HttpClient::connectByHostOrIp()
return _connectionStatus == SUCCESSFUL;
}
int HttpClient::sendHttpQuery(const char *resource, HttpRequestMethod method, Dictionary<DictionaryHelper::StringEntity> *getData, Dictionary<DictionaryHelper::StringEntity> *postData, Dictionary<DictionaryHelper::StringEntity> *headerData)
HttpClient::HttpQueryStatus HttpClient::sendHttpQuery(const char *resource, HttpRequestMethod method, Dictionary<DictionaryHelper::StringEntity> *getData, Dictionary<DictionaryHelper::StringEntity> *postData, Dictionary<DictionaryHelper::StringEntity> *headerData)
{
if(resource != NULL) //We overwrite the resource if it has been already defined
{
@ -101,7 +101,7 @@ int HttpClient::sendHttpQuery(const char *resource, HttpRequestMethod method, Di
return sendHttpQuery(method, getData, postData, headerData);
}
int HttpClient::sendHttpQuery(HttpRequestMethod method, Dictionary<DictionaryHelper::StringEntity> *getData, Dictionary<DictionaryHelper::StringEntity> *postData, Dictionary<DictionaryHelper::StringEntity> *headerData)
HttpClient::HttpQueryStatus HttpClient::sendHttpQuery(HttpRequestMethod method, Dictionary<DictionaryHelper::StringEntity> *getData, Dictionary<DictionaryHelper::StringEntity> *postData, Dictionary<DictionaryHelper::StringEntity> *headerData)
{
//We reset this two flags
_httpCode = HTTP_CODE::UNDEFINED_CODE;
@ -117,7 +117,7 @@ int HttpClient::sendHttpQuery(HttpRequestMethod method, Dictionary<DictionaryHel
if(!connected() || _connectionStatus == FAILED)
{
if(_retries == _maxRetries) return -__LINE__;
if(_retries == _maxRetries) return HttpQueryStatus::ERR_CONN_MAX_RETRY;
if(_connectionStatus == FAILED)
{
@ -138,7 +138,7 @@ int HttpClient::sendHttpQuery(HttpRequestMethod method, Dictionary<DictionaryHel
Serial.printf("Failed to reconnect\n");
#endif
stop();
return -__LINE__;
return HttpQueryStatus::ERR_CONN;
}
else
_retries = 0;
@ -174,12 +174,12 @@ int HttpClient::sendHttpQuery(HttpRequestMethod method, Dictionary<DictionaryHel
Serial.printf("Http verb unspecified\n");
#endif
if(!_keepAlive)stop();
return -__LINE__;
return HttpQueryStatus::UNKNOWN_HTTP_METHOD;
break;
}
}
return 0;
return HttpQueryStatus::SUCCESS;
}
HttpClient::HTTP_CODE HttpClient::isReplyAvailable(uint16_t timeout)

View File

@ -17,7 +17,7 @@ namespace HttpClientHelper
class HttpClient : public WiFiClient, public HttpConstants
{
public:
enum ConnectionStatus { NO_ATTEMPT = 0, SUCCESSFUL, FAILED };
enum HttpQueryStatus { SUCCESS = 0, ERR_CONN, ERR_CONN_MAX_RETRY, UNKNOWN_HTTP_METHOD };
HttpClient(const char *address, uint16_t port = 80, uint32_t timeout = 5000);
HttpClient(const char *address, const char *resource, uint16_t port = 80, uint32_t timeout = 5000);
@ -25,13 +25,13 @@ class HttpClient : public WiFiClient, public HttpConstants
HttpClient(const HttpClient &object);
virtual ~HttpClient();
int sendHttpQuery(const char *resource,
HttpQueryStatus sendHttpQuery(const char *resource,
HttpRequestMethod method = HttpRequestMethod::GET,
Dictionary<DictionaryHelper::StringEntity> *getData = NULL,
Dictionary<DictionaryHelper::StringEntity> *postData = NULL,
Dictionary<DictionaryHelper::StringEntity> *headerData = NULL);
int sendHttpQuery(HttpRequestMethod method = HttpRequestMethod::GET,
HttpQueryStatus sendHttpQuery(HttpRequestMethod method = HttpRequestMethod::GET,
Dictionary<DictionaryHelper::StringEntity> *getData = NULL,
Dictionary<DictionaryHelper::StringEntity> *postData = NULL,
Dictionary<DictionaryHelper::StringEntity> *headerData = NULL);
@ -46,6 +46,8 @@ class HttpClient : public WiFiClient, public HttpConstants
uint16_t readHttpBody(uint8_t *buffer, uint32_t size);
protected:
private:
enum ConnectionStatus { NO_ATTEMPT = 0, SUCCESSFUL, FAILED };
boolean connectByHostOrIp();
void sendUriWithGetParams(Dictionary<DictionaryHelper::StringEntity> *data);
void sendPostData(Dictionary<DictionaryHelper::StringEntity> *data);