Rework & clean de la lib DolibarrClient

This commit is contained in:
2023-11-10 16:31:06 +01:00
parent 1828d7a961
commit 3d94d4d803
9 changed files with 53 additions and 86 deletions

View File

@ -3,18 +3,12 @@
#include <ArduinoJson.h>
#include <iostream>
#define WAITING_WIFI_DELAY 1000
DolibarrClient::DolibarrClient(struct WifiConfig wifi_config, struct DolibarrConfig dolibarr_config) : wifi(wifi_config), dolibarr(dolibarr_config) {
DolibarrClient::DolibarrClient(struct DolibarrConfig dolibarr_config) : dolibarr(dolibarr_config) {
#if defined(DEBUG)
Serial.println(" --- Wifi configuration --- ");
Serial.println((std::string("SSID: ") + std::string(wifi_config.ssid)).c_str());
Serial.println((std::string("Password: ") + std::string(wifi_config.password)).c_str());
Serial.println(" --- Dolibarr configuration --- ");
Serial.println((std::string("Base URL: ") + std::string(dolibarr_config.url)).c_str());
Serial.println((std::string("Token: ") + std::string(dolibarr_config.api_key)).c_str());
#endif
this->initialize_wifi();
this->initialize_http_client();
}
@ -32,10 +26,10 @@ HTTPClient *DolibarrClient::build_url(const String& url) const {
int DolibarrClient::login() const {
HTTPClient *client = this->build_url(LOGIN_URL);
HTTPClient *client = this->build_url(API_LOGIN_URL);
int httpResponseCode = client->GET();
if (httpResponseCode > 0) {
StaticJsonDocument<LOGIN_JSON_SIZE> doc;
StaticJsonDocument<API_MAX_JSON_SIZE> doc;
DeserializationError error = deserializeJson(doc, client->getString().c_str());
if (error) {
delete client;
@ -48,24 +42,24 @@ int DolibarrClient::login() const {
return -1;
}
std::string replace_id(const char *str, const char *id) {
std::string url(str);
url.replace(url.find("{id}"), 4, id);
String replace_id(const char *str, const char *id) {
String url(str);
url.replace("{id}", id);
return url;
}
std::vector<models::Product> *DolibarrClient::list_products() const {
HTTPClient *client = this->build_url(LIST_PRODUCT_URL);
if (client->GET() == OK_RESPONSE) {
HTTPClient *client = this->build_url(API_LIST_PRODUCT_URL);
if (client->GET() == HTTP_CODE_OK) {
}
return nullptr;
}
models::Product *DolibarrClient::get_product_by_id(const char* id_product) const {
HTTPClient *client = this->build_url(replace_id(GET_PRODUCT_URL, id_product).c_str());
if (client->GET() == OK_RESPONSE) {
StaticJsonDocument<GET_PRODUCT_JSON_SIZE> doc;
HTTPClient *client = this->build_url(replace_id(API_GET_PRODUCT_URL, id_product).c_str());
if (client->GET() == HTTP_CODE_OK) {
StaticJsonDocument<API_MAX_JSON_SIZE> doc;
DeserializationError error = deserializeJson(doc, client->getString().c_str());
if (error) {
Serial.println("ERROR: ");
@ -87,9 +81,9 @@ models::Product *DolibarrClient::get_product_by_id(const char* id_product) const
}
std::vector<models::Warehouse> *DolibarrClient::list_warehouse() const {
HTTPClient *client = this->build_url(LIST_WAREHOUSE_URL);
if (client->GET() == OK_RESPONSE) {
StaticJsonDocument<LIST_WAREHOUSE_JSON_SIZE> doc;
HTTPClient *client = this->build_url(API_LIST_WAREHOUSE_URL);
if (client->GET() == HTTP_CODE_OK) {
StaticJsonDocument<API_MAX_JSON_SIZE> doc;
DeserializationError error = deserializeJson(doc, client->getString().c_str());
if (error) {
Serial.println("ERROR: ");
@ -111,34 +105,21 @@ std::vector<models::Warehouse> *DolibarrClient::list_warehouse() const {
}
int DolibarrClient::create_movement(models::CreateProductStock &stock) const {
HTTPClient *client = this->build_url(CREATE_STOCKS_MOVEMENTS_URL);
StaticJsonDocument<CREATE_STOCKS_MOVEMENTS_JSON_SIZE> doc;
HTTPClient *client = this->build_url("API_CREATE_STOCKS_MOVEMENTS_URL");
StaticJsonDocument<API_MAX_JSON_SIZE> doc;
std::string result;
doc["product_id"] = stock.product_id;
doc["warehouse_id"] = stock.warehouse_id;
doc["qty"] = stock.qty;
serializeJson(doc, result);
Serial.println(result.c_str());
if (client->POST(result.c_str()) == OK_RESPONSE) {
if (client->POST(result.c_str()) == HTTP_CODE_OK) {
delete client;
return 0;
}
return -1;
}
int DolibarrClient::initialize_wifi() const {
WiFiClass::mode(WIFI_STA); //Optional
WiFi.setSleep(false);
WiFi.begin(this->wifi.ssid, this->wifi.password);
Serial.print("Connecting ");
while(WiFiClass::status() != WL_CONNECTED){
delay(WAITING_WIFI_DELAY);
Serial.print(".");
}
Serial.println("Connected to the WiFi network");
return 0;
}
int DolibarrClient::initialize_http_client() {
this->httpClient = new HTTPClient();
if (this->login() == 0) {

View File

@ -6,25 +6,6 @@
#include <vector>
#include "DolibarrModels.h"
constexpr u_int32_t OK_RESPONSE = 200;
constexpr u_int32_t CREATED_RESPONSE = 201;
constexpr u_int32_t NO_CONTENT_RESPONSE = 204;
constexpr u_int32_t LOGIN_JSON_SIZE = 4096;
constexpr const char* LOGIN_URL = "/users/info";
constexpr u_int32_t LIST_PRODUCT_JSON_SIZE = 4096;
constexpr const char* LIST_PRODUCT_URL = "/products/";
constexpr u_int32_t GET_PRODUCT_JSON_SIZE = 4096;
constexpr const char* GET_PRODUCT_URL = "/products/{id}/";
constexpr u_int32_t GET_PRODUCT_STOCK_JSON_SIZE = 4096;
constexpr const char* GET_PRODUCT_STOCK_URL = "/products/{id}/stock/";
constexpr u_int32_t LIST_WAREHOUSE_JSON_SIZE = 4096;
constexpr const char* LIST_WAREHOUSE_URL = "/warehouses/";
constexpr u_int32_t LIST_STOCKS_MOVEMENTS_JSON_SIZE = 4096;
constexpr const char* LIST_STOCKS_MOVEMENTS_URL = "/stockmovements/?sortfield=t.rowid&sortorder=ASC&limit=100";
constexpr u_int32_t CREATE_STOCKS_MOVEMENTS_JSON_SIZE = 4096;
constexpr const char* CREATE_STOCKS_MOVEMENTS_URL = "/stockmovements/";
struct WifiConfig {
const char* ssid;
const char* password;
@ -37,7 +18,7 @@ struct DolibarrConfig {
class DolibarrClient {
public:
DolibarrClient(WifiConfig wifi_config, DolibarrConfig dolibarr_config);
DolibarrClient(DolibarrConfig dolibarr_config);
~DolibarrClient() = default;
int login() const;
std::vector<models::Warehouse> *list_warehouse() const;
@ -46,9 +27,7 @@ public:
int create_movement(models::CreateProductStock &stock) const;
private:
HTTPClient* httpClient{};
struct WifiConfig wifi;
struct DolibarrConfig dolibarr;
int initialize_wifi() const;
int initialize_http_client();
HTTPClient *build_url(const String& url) const;
};

View File

@ -1,5 +0,0 @@
//
// Created by nico on 15/09/23.
//
#include "DolibarrModels.h"