Kurlyk
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
7using json = nlohmann::json;
8
9std::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
22int 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
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
65 return 0;
66}
nlohmann::json json
std::string load_api_key(const std::string &filename)
Concrete HTTP client for making requests to a specific host.
void set_verbose(bool verbose)
Enables or disables verbose output.
bool post(const std::string &path, const QueryParams &query, const Headers &headers, const std::string &content, HttpResponseCallback callback)
Sends a POST request.
void set_retry_attempts(long retry_attempts, long retry_delay_ms)
Sets retry attempts and delay between retries for HTTP requests.
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).
void set_host(const std::string &host)
Sets the host URL for the HTTP client.
Injects an Authorization: Bearer <token> header.
bool authorize(HttpRequest &request) const override
Modifies an HttpRequest in-place to include authentication credentials.
Main header file for the Kurlyk library, providing HTTP and WebSocket support.
void init(const bool use_async=true)
Initializes the Kurlyk library, setting up necessary managers and the network worker.
Definition runtime.hpp:13
void deinit()
Deinitializes the Kurlyk library, stopping async processing or cleaning up synchronous state.
Definition runtime.hpp:26
utils::CaseInsensitiveMultimap Headers
Alias for HTTP headers, providing a case-insensitive unordered multimap.
#define KURLYK_PRINT