Kurlyk
Toggle main menu visibility
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
5
int
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.
20
kurlyk::http::auth::ApiKeyAuthProvider
auth_provider(
21
"key"
, api_key,
kurlyk::http::auth::ApiKeyPlacement::QUERY
);
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
47
kurlyk::deinit
();
48
return
0;
49
}
main
int main()
Definition
api_key_auth_provider_example.cpp:5
kurlyk::HttpClient
Concrete HTTP client for making requests to a specific host.
Definition
HttpClient.hpp:17
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::HttpClient::get
bool get(const std::string &path, const QueryParams &query, const Headers &headers, HttpResponseCallback callback)
Sends a GET request.
Definition
HttpClient.hpp:681
kurlyk::HttpRequest
Represents an HTTP request configuration.
Definition
HttpRequest.hpp:16
kurlyk::HttpRequest::url
std::string url
Full request URL.
Definition
HttpRequest.hpp:21
kurlyk::HttpRequest::headers
Headers headers
HTTP request headers.
Definition
HttpRequest.hpp:20
kurlyk::HttpRequest::method
std::string method
HTTP request method (e.g., "GET", "POST").
Definition
HttpRequest.hpp:22
kurlyk::http::auth::ApiKeyAuthProvider
Injects an API key into the request either as a header or query parameter.
Definition
ApiKeyAuthProvider.hpp:25
kurlyk::http::auth::ApiKeyAuthProvider::authorize
bool authorize(HttpRequest &request) const override
Modifies an HttpRequest in-place to include authentication credentials.
Definition
ApiKeyAuthProvider.hpp:45
kurlyk.hpp
Main header file for the Kurlyk library, providing HTTP and WebSocket support.
kurlyk::http::auth::ApiKeyPlacement::QUERY
@ QUERY
Appended as a query parameter to the request URL.
Definition
ApiKeyAuthProvider.hpp:20
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
api_key_auth_provider_example.cpp
Generated by
1.17.0