-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHTTPClient.cpp
More file actions
86 lines (68 loc) · 2.33 KB
/
Copy pathHTTPClient.cpp
File metadata and controls
86 lines (68 loc) · 2.33 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
85
86
#include "HTTPClient.h"
#include <iostream>
HTTPClient::HTTPClient()
:
mIOContext(std::make_unique<net::io_context>()),
mSSLContext(std::make_unique<ssl::context>(ssl::context::tlsv12_client))
{
mSSLContext->set_default_verify_paths();
mSSLContext->set_verify_mode(ssl::verify_peer);
}
HTTPClient::~HTTPClient() = default;
BString
HTTPClient::Post(const BString& url, const BString& jsonData, const BString& authHeader)
{
try {
// Hardcoded values for the fixed DeepSeek API URL
BString host = "api.deepseek.com";
BString target = "/v1/chat/completions";
return PerformHTTPSRequest(host, target, jsonData, authHeader);
} catch (const std::exception& e) {
BString errorMsg = "HTTP Request Error: ";
errorMsg += e.what();
return errorMsg;
}
}
BString
HTTPClient::PerformHTTPSRequest(const BString& host, const BString& target, const BString& jsonData,
const BString& authHeader)
{
try {
// Resolve the hostname
tcp::resolver resolver(*mIOContext);
auto const results = resolver.resolve(host.String(), "443");
// Create SSL stream
ssl::stream<tcp::socket> stream(*mIOContext, *mSSLContext);
// Set SNI Hostname
if (!SSL_set_tlsext_host_name(stream.native_handle(), host.String())) {
beast::error_code ec{static_cast<int>(::ERR_get_error()),
net::error::get_ssl_category()};
throw beast::system_error{ec};
}
// Connect to the server
net::connect(stream.next_layer(), results.begin(), results.end());
// Perform SSL handshake
stream.handshake(ssl::stream_base::client);
// Set up HTTP POST request
http::request<http::string_body> req{http::verb::post, target.String(), 11};
req.set(http::field::host, host.String());
req.set(http::field::user_agent, BOOST_BEAST_VERSION_STRING);
req.set(http::field::content_type, "application/json");
req.set(http::field::authorization, authHeader.String());
req.set(http::field::accept, "application/json");
req.body() = jsonData.String();
req.prepare_payload();
// Send the HTTP request
http::write(stream, req);
// Receive the HTTP response
beast::flat_buffer buffer;
http::response<http::string_body> res;
http::read(stream, buffer, res);
beast::error_code ec;
stream.shutdown(ec);
return BString(res.body().c_str());
} catch (const std::exception& e) {
std::cout << "HTTPS request failed: " << e.what() << std::endl;
throw;
}
}