-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNetworkConnection.cpp
More file actions
84 lines (64 loc) · 2.46 KB
/
NetworkConnection.cpp
File metadata and controls
84 lines (64 loc) · 2.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include "NetworkConnection.h"
std::string NetworkConnection::IP = "";
void NetworkConnection::setIP(std::string serverIP) {
IP = serverIP;
}
// Make a POST request to the server to send data
// TODO: Wrap in try catch? Here or in call...
// TODO: Return bool
void NetworkConnection::postData(const std::string &url, const nlohmann::json &jsonData) {
httplib::Headers headers = {
{"Content-Type", "application/json"}
};
auto postRes = client->Post(url, headers, jsonData.dump(), "application/json");
if (postRes.error() == httplib::Error::Connection) { // If the server has no connection
std::cerr << "POST: Unable to reach server " << std::endl;
return;
}
std::cout << std::endl << "postRes: " << postRes << std::endl;
if (postRes) {
std::cout << std::endl << "POST: " << std::endl;
std::cout << "Status: " << postRes->status << std::endl;
std::cout << "Body: " << postRes->body << std::endl;
}
}
// Make a GET request to the server to get data
// TODO: Wrap in try catch? Here or in call...
nlohmann::json NetworkConnection::getData(const std::string &url) {
auto getRes = client->Get(url);
std::string contentType;
nlohmann::json responseData;
// If there is a connection error return early
if (getRes.error() == httplib::Error::Connection) {
responseData["status"] = 504; // Connection timeout
return responseData;
} else if (!getRes) {
std::cerr << "The get response is bad" << std::endl;
responseData["status"] = 504; // Connection timeout
return responseData;
}
for (const auto& header : getRes->headers) {
if (header.first == "Content-Type") {
contentType = header.second;
break;
}
}
if (!getRes->body.empty()) {
responseData["status"] = getRes->status;
if (!contentType.empty()) {
responseData["contentType"] = contentType;
} else {
std::cerr << "Warning: contentType not specified" << std::endl;
}
if (!contentType.empty() && contentType == "application/json") {
// Parse the JSON text into a json object
nlohmann::json jsonData = nlohmann::json::parse(getRes->body);
responseData["body"] = jsonData;
} else {
responseData["body"] = getRes->body;
}
} else {
responseData["status"] = 504; // Connection timeout
}
return responseData;
}