Kurlyk
Toggle main menu visibility
Loading...
Searching...
No Matches
bearer_token_auth_provider_example.cpp
Go to the documentation of this file.
1
#define KURLYK_AUTO_INIT 0
2
#include <
kurlyk.hpp
>
3
#include <iostream>
4
#include <fstream>
5
#include <nlohmann/json.hpp>
6
7
using
json
= nlohmann::json;
8
9
std::string
load_api_key
(
const
std::string& filename) {
10
std::ifstream file(filename);
11
if
(!file) {
12
throw
std::runtime_error(
"Failed to open the API key file."
);
13
}
14
std::string api_key;
15
std::getline(file, api_key);
16
if
(api_key.empty()) {
17
throw
std::runtime_error(
"API key is missing."
);
18
}
19
return
api_key;
20
}
21
22
int
main
() {
23
kurlyk::init
(
true
);
24
try
{
25
const
std::string api_key =
load_api_key
(
"openai_api_key.txt"
);
26
27
kurlyk::Headers
headers = { {
"Content-Type"
,
"application/json"
} };
28
29
json
request_body = {
30
{
"model"
,
"gpt-3.5-turbo"
},
31
{
"messages"
, {
32
{{
"role"
,
"user"
}, {
"content"
, u8
"Hello, ChatGPT!"
}}
33
}},
34
{
"max_tokens"
, 50}
35
};
36
37
std::string host =
"https://neuroapi.host"
;
38
kurlyk::HttpClient
client;
39
client.
set_host
(host);
40
client.
set_rate_limit_rpm
(3);
41
client.
set_retry_attempts
(1, 5000);
42
client.
set_verbose
(
true
);
43
44
kurlyk::http::auth::BearerTokenAuthProvider
auth_provider(api_key);
45
auth_provider.
authorize
(headers);
46
47
KURLYK_PRINT
<<
"Request body: "
<< request_body.dump(4) << std::endl;
48
49
auto
future = client.
post
(
"/v1/chat/completions"
, {}, headers, request_body.dump());
50
51
auto
response = future.get();
52
if
(response->ready && response->status_code == 200) {
53
KURLYK_PRINT
<<
"Response: "
<< response->content << std::endl;
54
}
else
{
55
KURLYK_PRINT
<<
"Error: "
<< response->status_code
56
<<
" - "
<< response->error_code.message() << std::endl;
57
KURLYK_PRINT
<<
"Response: "
<< response->content << std::endl;
58
}
59
60
}
catch
(
const
std::exception& e) {
61
KURLYK_PRINT
<<
"Error: "
<< e.what() << std::endl;
62
}
63
64
kurlyk::deinit
();
65
return
0;
66
}
json
nlohmann::json json
Definition
bearer_token_auth_provider_example.cpp:7
main
int main()
Definition
bearer_token_auth_provider_example.cpp:22
load_api_key
std::string load_api_key(const std::string &filename)
Definition
bearer_token_auth_provider_example.cpp:9
kurlyk::HttpClient
Concrete HTTP client for making requests to a specific host.
Definition
HttpClient.hpp:17
kurlyk::HttpClient::set_verbose
void set_verbose(bool verbose)
Enables or disables verbose output.
Definition
HttpClient.hpp:563
kurlyk::HttpClient::post
bool post(const std::string &path, const QueryParams &query, const Headers &headers, const std::string &content, HttpResponseCallback callback)
Sends a POST request.
Definition
HttpClient.hpp:696
kurlyk::HttpClient::set_retry_attempts
void set_retry_attempts(long retry_attempts, long retry_delay_ms)
Sets retry attempts and delay between retries for HTTP requests.
Definition
HttpClient.hpp:504
kurlyk::HttpClient::set_rate_limit_rpm
void set_rate_limit_rpm(long requests_per_minute, RateLimitType type=RateLimitType::RL_GENERAL, bool sequential=false)
Sets the rate limit based on requests per minute (RPM).
Definition
HttpClient.hpp:314
kurlyk::HttpClient::set_host
void set_host(const std::string &host)
Sets the host URL for the HTTP client.
Definition
HttpClient.hpp:182
kurlyk::http::auth::BearerTokenAuthProvider
Injects an Authorization: Bearer <token> header.
Definition
BearerTokenAuthProvider.hpp:17
kurlyk::http::auth::BearerTokenAuthProvider::authorize
bool authorize(HttpRequest &request) const override
Modifies an HttpRequest in-place to include authentication credentials.
Definition
BearerTokenAuthProvider.hpp:30
kurlyk.hpp
Main header file for the Kurlyk library, providing HTTP and WebSocket support.
kurlyk::init
void init(const bool use_async=true)
Initializes the Kurlyk library, setting up necessary managers and the network worker.
Definition
runtime.hpp:13
kurlyk::deinit
void deinit()
Deinitializes the Kurlyk library, stopping async processing or cleaning up synchronous state.
Definition
runtime.hpp:26
kurlyk::Headers
utils::CaseInsensitiveMultimap Headers
Alias for HTTP headers, providing a case-insensitive unordered multimap.
Definition
CaseInsensitiveMultimap.hpp:62
KURLYK_PRINT
#define KURLYK_PRINT
Definition
print_utils.hpp:8
examples
bearer_token_auth_provider_example.cpp
Generated by
1.17.0