// ICS console HTTPS demo - C++Builder equivalent of OverbyteIcsConHttp.dpr
#pragma hdrstop
#include <System.hpp>
#include <SysUtils.hpp>
#include <tchar.h>
#include <stdio.h>
#include <memory>

#include "OverbyteIcsSslHttpRest.hpp"
#include "OverbyteIcsTypes.hpp"

#pragma argsused
int _tmain(int argc, _TCHAR *argv[])
{
    const String copyRight = " ConHttp" + String(IcsVerLitCopyright);
    const String url = "https://wiki.overbyte.eu/wiki/index.php/Main_Page";

    printf("%s\n\n", AnsiString(copyRight).c_str());
    try {
        // Owner nullptr: unique_ptr frees it, so no VCL Owner may free it as well.
        std::unique_ptr<TSslHttpRest> httpCli(new TSslHttpRest(nullptr));
        printf("Querying %s\n", AnsiString(url).c_str());

        // Synchronous request: returns when the request is complete.
        int statCode = httpCli->RestRequest(httpGET, url, false, "");
        printf("Request Done, Status Code: %d\n", statCode);

        // ResponseOctet is raw bytes; write them unchanged (may hold NULs, so use fwrite).
        AnsiString body = httpCli->ResponseOctet;
        if (body.Length() > 0)
            fwrite(body.c_str(), 1, body.Length(), stdout);
    }
    catch (Exception &E) {
        printf("Failed: %s\n", AnsiString(E.Message).c_str());
    }

    printf("Hit ENTER key to return quit program...\n");
    getchar();
    return 0;
}
