Compare commits

...

2 Commits

3 changed files with 12 additions and 10 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);

View File

@ -127,7 +127,7 @@ OTAUpdater::UpdateInfo OTAUpdater::fetchUpdateInfo(const char *softwareVersion,
headerData.add("x-ESP8266-STA-MAC",DictionaryHelper::StringEntity(WiFi.macAddress().c_str()));
headerData.add("User-Agent",DictionaryHelper::StringEntity("ESP8266-http-Update"));
if(httpClient.sendHttpQuery(HttpClient::HttpRequestMethod::GET, &getData, NULL, &headerData) == 0)
if(httpClient.sendHttpQuery(HttpClient::HttpRequestMethod::GET, &getData, NULL, &headerData) == HttpClient::HttpQueryStatus::SUCCESS)
{
HttpConstants::HTTP_CODE result = httpClient.isReplyAvailable(1000);
switch(result)