
#ifndef _FlexRestClient_h
#define _FlexRestClient_h

#include <Arduino.h>
#include <WiFi.h>

#include "RBPXPLRTERM_base.h"

#if defined(ENV_ESP32)
#include <WiFiClientSecure.h>
#elif defined(ENV_RP2040)
#include <WiFiClient.h>
#else
#error unrecognized ENV for FRC.include
#endif

class FlexRestClient
{
public:
    FlexRestClient(const char *host, const int port);
    int begin(const char *ssid, const char *pass);
    IPAddress getIpAddress();

    int get(const char *, int timeout);
    int post(const char *path, const char *body, int timeout);

    void setHeader(const char *header);
    void setContentType(const char *contentTypeValue);
    void write(const char *string);
    int request(const char *method, const char *path, const char *body, int timeout);
    int readResponse(int timeout);
    String getLastResponsePayload();

private:
    int port;
    int num_headers;
    const char *host;
    const char *headers[10];
    const char *contentType;
    String lastResponsePayload;
#if defined(ENV_ESP32)
    WiFiClientSecure client_s;
#elif defined(ENV_RP2040)
    WiFiClient client;
#else
#error unrecognized ENV for FRC_H.client
#endif
    void writeHeaders();
    void writeBody(const char *body);
};

#endif
