SNode.C
Loading...
Searching...
No Matches
jsonclient.cpp
Go to the documentation of this file.
1/*
2 * SNode.C - a slim toolkit for network communication
3 * Copyright (C) Volker Christian <me@vchrist.at>
4 * 2020, 2021, 2022, 2023, 2024, 2025
5 * Json Middleware 2020, 2021 Marlene Mayr, Anna Moser, Matteo Prock, Eric Thalhammer
6 * Github <MarleneMayr><moseranna><MatteoMatteoMatteo><peregrin-tuk>
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22#include "core/SNodeC.h"
23#include "web/http/legacy/in/Client.h"
24
25#ifndef DOXYGEN_SHOULD_SKIP_THIS
26
27#include "log/Logger.h"
28
29#include <string>
30
31#endif /* DOXYGEN_SHOULD_SKIP_THIS */
32
33int main(int argc, char* argv[]) {
34 core::SNodeC::init(argc, argv);
35
36 using Client = web::http::legacy::in::Client;
37 using Request = Client::Request;
38 using Response = Client::Response;
39 using SocketAddress = Client::SocketAddress;
40
41 const Client jsonClient(
42 "legacy",
43 [](const std::shared_ptr<Request>& req) {
44 VLOG(1) << "-- OnRequest";
45 req->method = "POST";
46 req->url = "/index.html";
47 req->type("application/json");
48 req->set("Connection", "close");
49 req->send("{\"userId\":1,\"schnitzel\":\"good\",\"hungry\":false}",
50 []([[maybe_unused]] const std::shared_ptr<Request>& req, const std::shared_ptr<Response>& res) {
51 VLOG(1) << "-- OnResponse";
52 VLOG(1) << " Status:";
53 VLOG(1) << " " << res->httpVersion;
54 VLOG(1) << " " << res->statusCode;
55 VLOG(1) << " " << res->reason;
56
57 VLOG(1) << " Headers:";
58 for (const auto& [field, value] : res->headers) {
59 VLOG(1) << " " << field + " = " + value;
60 }
61
62 VLOG(1) << " Cookies:";
63 for (const auto& [name, cookie] : res->cookies) {
64 VLOG(1) << " " + name + " = " + cookie.getValue();
65 for (const auto& [option, value] : cookie.getOptions()) {
66 VLOG(1) << " " + option + " = " + value;
67 }
68 }
69
70 res->body.push_back(0);
71 VLOG(1) << " Body:\n----------- start body -----------" << res->body.data()
72 << "------------ end body ------------";
73 });
74 },
75 []([[maybe_unused]] const std::shared_ptr<Request>& req) {
76 LOG(INFO) << " -- OnRequestEnd";
77 });
78
79 jsonClient.connect("localhost",
80 8080,
81 [instanceName = jsonClient.getConfig().getInstanceName()](
82 const SocketAddress& socketAddress,
83 const core::socket::State& state) { // example.com:81 simulate connect timeout
84 switch (state) {
85 case core::socket::State::OK:
86 VLOG(1) << instanceName << ": connected to '" << socketAddress.toString() << "'";
87 break;
88 case core::socket::State::DISABLED:
89 VLOG(1) << instanceName << ": disabled";
90 break;
91 case core::socket::State::ERROR:
92 LOG(ERROR) << instanceName << ": " << socketAddress.toString() << ": " << state.what();
93 break;
94 case core::socket::State::FATAL:
95 LOG(FATAL) << instanceName << ": " << socketAddress.toString() << ": " << state.what();
96 break;
97 }
98 });
99 /*
100 jsonClient.connect("localhost",
101 8080,
102 [instanceName = jsonClient.getConfig().getInstanceName()](
103 const SocketAddress& socketAddress,
104 const core::socket::State& state) { // example.com:81 simulate connnect timeout
105 switch (state) {
106 case core::socket::State::OK:
107 VLOG(1) << instanceName << ": connected to '" << socketAddress.toString() << "'";
108 break;
109 case core::socket::State::DISABLED:
110 VLOG(1) << instanceName << ": disabled";
111 break;
112 case core::socket::State::ERROR:
113 LOG(ERROR) << instanceName << ": " << socketAddress.toString() << ": " << state.what();
114 break;
115 case core::socket::State::FATAL:
116 LOG(FATAL) << instanceName << ": " << socketAddress.toString() << ": " << state.what();
117 break;
118 }
119 });
120 */
121 /*
122 jsonClient.post("localhost", 8080, "/index.html", "{\"userId\":1,\"schnitzel\":\"good\",\"hungry\":false}", [](int err) {
123 if (err != 0) {
124 PLOG(ERROR) << "OnError: " << err;
125 }
126 });
127 */
128
129 return core::SNodeC::start();
130}
int main(int argc, char *argv[])