Kurlyk
Loading...
Searching...
No Matches
api_key_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
5int main() {
6 kurlyk::init(true);
7 try {
8 // Example: Google Gemini API key in a query parameter.
9 // In a real application, load the key from a secure location.
10 const std::string api_key = "your_gemini_api_key_here";
11
12 kurlyk::Headers headers = { {"Content-Type", "application/json"} };
13
14 kurlyk::HttpClient client;
15 client.set_host("https://generativelanguage.googleapis.com");
16 client.set_rate_limit_rpm(10);
17 client.set_retry_attempts(1, 5000);
18
19 // Use ApiKeyAuthProvider to append the key as a query parameter.
22
23 kurlyk::HttpRequest request;
24 request.method = "GET";
25 request.url = "/v1beta/models/gemini-pro";
26 request.headers = headers;
27
28 auth_provider.authorize(request);
29
30 // After authorize, the URL should contain ?key=...
31 KURLYK_PRINT << "Authorized URL: " << request.url << std::endl;
32
33 auto future = client.get(request.url, {}, request.headers);
34 auto response = future.get();
35
36 if (response->ready && response->status_code == 200) {
37 KURLYK_PRINT << "Response: " << response->content << std::endl;
38 } else {
39 KURLYK_PRINT << "Error: " << response->status_code
40 << " - " << response->error_code.message() << std::endl;
41 }
42
43 } catch (const std::exception& e) {
44 KURLYK_PRINT << "Error: " << e.what() << std::endl;
45 }
46
48 return 0;
49}
Concrete HTTP client for making requests to a specific host.
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.
bool get(const std::string &path, const QueryParams &query, const Headers &headers, HttpResponseCallback callback)
Sends a GET request.
Represents an HTTP request configuration.
std::string url
Full request URL.
Headers headers
HTTP request headers.
std::string method
HTTP request method (e.g., "GET", "POST").
Injects an API key into the request either as a header or query parameter.
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.
@ QUERY
Appended as a query parameter to the request URL.
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