<commit_before>
#pragma once
#ifndef MENDER_CLIENT_HPP
#define MENDER_CLIENT_HPP

#include "auth_manager.hpp"
#include <net/inet4>
#include "../http_client/http/inc/request.hpp"
#include <string>
#include <botan/base64.h>

namespace mender {

  static const std::string api_prefx = "/api/devices/0.1/";

	class Client {
  public:
    using AuthCallback  = delegate<void(bool)>;
  public:
    Client(Auth_manager&&, const std::string& server, const uint16_t port = 0);
    Client(Auth_manager&&, net::tcp::Socket);
    void make_auth_request(AuthCallback);

  private:
    const std::string server_;
    Auth_manager am_;
    net::tcp::Socket cached_;

    std::string build_url(const std::string& server) const;

    std::string build_api_url(const std::string& server, const std::string& url) const;

    void send(std::unique_ptr<http::Request> req, std::string endpoint);

  }; // < class Client

  Client::Client(Auth_manager&& man, const std::string& server, const uint16_t port)
    : server_(server),
      am_{std::forward<Auth_manager>(man)},
      cached_{0, port}
  {

  }

  Client::Client(Auth_manager&& man, net::tcp::Socket socket)
    : server_{socket.address().to_string()},
      am_{std::forward<Auth_manager>(man)},
      cached_(std::move(socket))
  {

  }

  byte_seq base64(byte_seq data)
  {
    return data;
  }

  void Client::make_auth_request(AuthCallback cb)
  {
    auto auth = am_.make_auth_request();

    //printf("--Auth request--\nData:%.*s\nToken:%.*s\nSign:%s\n",
    //  auth.data.size(), (char*)auth.data.data(), auth.token.size(), (char*)auth.token.data(), Botan::base64_encode(auth.signature).c_str());

    using namespace std::string_literals;

    static auto&& stack = net::Inet4::ifconfig({ 10,0,0,42 },     // IP
    { 255,255,255,0 }, // Netmask
    { 10,0,0,1 },      // Gateway
    { 10,0,0,1 });     // DNS);

    std::string data{auth.data.begin(), auth.data.end()};
    auto req = "POST /api/devices/0.1/authentication/auth_requests HTTP/1.1\r\n"
               "Content-Type: application/json\r\n"
               "Authorization: Bearer" + std::string{auth.token.begin(), auth.token.end()} + "\r\n"
               "Content-Length: " + std::to_string(data.size()) + "\r\n"
               "X-MEN-Signature: " + Botan::base64_encode(auth.signature) + "\r\n\r\n"
               + std::string{data.begin(), data.end()} + "\r\n\r\n"s;

    stack.tcp().connect(stack.gateway(), 8080,
    [req](auto conn)
    {
      printf("Connected\n");
      conn->on_read(4096, [](auto buf, auto len) {
        printf("Reply:\n%.*s\n", len, (char*)buf.get());
      });

      printf("POST Request:\n%s\n", req.c_str());
      conn->write(req);
    });

    /*auto req = std::make_unique<http::Request>();
    req->add_body(std::string{data.begin(), data.end()});
    req->add_header("Content-Type"s, "application/json"s);
    req->add_header("Authorization"s, "Bearer " + std::string{auth.token.begin(), auth.token.end()});
    req->add_header("X-MEN-Signature"s, Botan::base64_encode(auth.signature));*/

    //send(std::move(req), "/authentication/auth_requests");
  }

  void Client::send(std::unique_ptr<http::Request> req, std::string endpoint)
  {
    static auto&& stack = net::Inet4::stack();

    if(cached_.address() == 0)
    {
      /*stack.resolve(server_,
      [this, endpoint, req_{std::move(req)}] // move into capture is c++14
      (auto addr)
      {
        //assert(addr != 0);
        cached_ = {addr, cached_.port()};
        if(addr != 0)
          send(std::move(req_), endpoint);
      });*/
      return;
    }

    stack.tcp().connect(cached_,
    [this, req{std::move(req)}] (auto conn)
    {
      conn->on_read(2048, [](auto, auto){});
      conn->write(req->to_string());
    });

  }
<commit_msg>Using IncludeOS http client + cleanups<commit_after>
#pragma once
#ifndef MENDER_CLIENT_HPP
#define MENDER_CLIENT_HPP

#include "auth_manager.hpp"
#include <net/inet4>
#include <string>
#include <botan/base64.h>
#include <net/http/client.hpp>

namespace mender {

  static const std::string api_prefx = "/api/devices/0.1/";

	class Client {
  public:
    using AuthCallback  = delegate<void(bool)>;
  public:
    Client(Auth_manager&&, const std::string& server, const uint16_t port = 0);
    Client(Auth_manager&&, net::tcp::Socket);
    void make_auth_request(AuthCallback);

  private:
    const std::string server_;
    Auth_manager am_;
    net::tcp::Socket cached_;
    std::unique_ptr<http::Client> httpclient_;

    std::string build_url(const std::string& server) const;

    std::string build_api_url(const std::string& server, const std::string& url) const;

    void send(std::unique_ptr<http::Request> req, std::string endpoint);

  }; // < class Client

  Client::Client(Auth_manager&& man, const std::string& server, const uint16_t port)
    : server_(server),
      am_{std::forward<Auth_manager>(man)},
      cached_{0, port}
  {
    static auto&& stack = net::Inet4::ifconfig(
      { 10,0,0,42 },     // IP
      { 255,255,255,0 }, // Netmask
      { 10,0,0,1 },      // Gateway
      { 10,0,0,1 });     // DNS);

    httpclient_ = std::make_unique<http::Client>(stack.tcp());
  }

  Client::Client(Auth_manager&& man, net::tcp::Socket socket)
    : server_{socket.address().to_string()},
      am_{std::forward<Auth_manager>(man)},
      cached_(std::move(socket))
  {
    static auto&& stack = net::Inet4::ifconfig(
      { 10,0,0,42 },     // IP
      { 255,255,255,0 }, // Netmask
      { 10,0,0,1 },      // Gateway
      { 10,0,0,1 });     // DNS);

    httpclient_ = std::make_unique<http::Client>(stack.tcp());
  }

  void Client::make_auth_request(AuthCallback cb)
  {
    auto auth = am_.make_auth_request();

    using namespace std::string_literals;
    using namespace http;

    std::string data{auth.data.begin(), auth.data.end()};

    auto req = httpclient_->create_request();
    req->set_method(POST);
    req->set_uri(URI{"/api/devices/0.1/authentication/auth_requests"});

    auto& header = req->header();
    header.add_field(header::Content_Type, "application/json");
    header.add_field(header::Authorization, "Bearer " + std::string{auth.token.begin(), auth.token.end()} );
    header.add_field("X-MEN-Signature", std::string{Botan::base64_encode(auth.signature)});
    req->add_body(std::string{data.begin(), data.end()});

    printf("Signature:\n%s\n", Botan::base64_encode(auth.signature).c_str());

    httpclient_->send(std::move(req), cached_,
      [](auto err, http::Response_ptr res)
      {
        if(!res)
          printf("No reply.\n");
        else
          printf("Reply:\n%s", res->to_string().c_str());

      }
    );
  }

  void Client::send(std::unique_ptr<http::Request> req, std::string endpoint)
  {
    static auto&& stack = net::Inet4::stack();

    if(cached_.address() == 0)
    {
      /*stack.resolve(server_,
      [this, endpoint, req_{std::move(req)}] // move into capture is c++14
      (auto addr)
      {
        //assert(addr != 0);
        cached_ = {addr, cached_.port()};
        if(addr != 0)
          send(std::move(req_), endpoint);
      });*/
      return;
    }

    stack.tcp().connect(cached_,
    [this, req{std::move(req)}] (auto conn)
    {
      conn->on_read(2048, [](auto, auto){});
      conn->write(req->to_string());
    });

  }

{'fraction_non_alphanumeric': 0.18613888508423504, 'fraction_numerical': 0.02040816326530612, 'mean_word_length': 3.257725947521866, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1712642', 'n_tokens_mistral': 2609, 'n_tokens_neox': 2482, 'n_words': 432}