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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
//! Operation contracts for the API Keys API API (v2).
//!
//! Auto-generated from the GCP Discovery Document.
//! **Do not edit manually** — modify the manifest and re-run codegen.
//!
//! These are the raw HTTP operations with correct URLs, methods,
//! and parameter ordering. The hand-written `api/apikeys.rs` wraps
//! these with ergonomic builders, operation polling, etc.
use crate::types::apikeys::*;
use crate::{GcpHttpClient, Result};
/// Raw HTTP operations for the API Keys API API.
///
/// These methods encode the correct URL paths, HTTP methods, and
/// parameter ordering from the GCP Discovery Document.
/// They are `pub(crate)` — use the ergonomic wrappers in
/// [`super::apikeys::ApikeysClient`] instead.
pub struct ApikeysOps<'a> {
pub(crate) client: &'a GcpHttpClient,
}
impl<'a> ApikeysOps<'a> {
pub(crate) fn new(client: &'a GcpHttpClient) -> Self {
Self { client }
}
fn base_url(&self) -> &str {
#[cfg(any(test, feature = "test-support"))]
{
if let Some(ref base) = self.client.base_url {
return base.trim_end_matches('/');
}
}
"https://apikeys.googleapis.com"
}
/// Lists the API keys owned by a project. The key string of the API key isn't included in
/// the response. NOTE: Key is a global resource; hence the only supported value for
/// location is `global`.
///
/// **GCP API**: `GET v2/{+parent}/keys`
///
/// # Path Parameters
/// - `parent` — Required. Lists all API keys associated with this project. *(required)*
///
/// # Query Parameters
/// - `pageSize` — Optional. Specifies the maximum number of results to be returned at a time.
/// - `pageToken` — Optional. Requests a specific page of results.
/// - `showDeleted` — Optional. Indicate that keys deleted in the past 30 days should also be returned.
///
/// # Response
/// [`V2ListKeysResponse`]
#[allow(dead_code)]
pub(crate) async fn list_keys(
&self,
parent: &str,
page_size: &str,
page_token: &str,
show_deleted: &str,
) -> Result<V2ListKeysResponse> {
let url = format!("{}/v2/{}/keys", self.base_url(), parent,);
let url = crate::append_query_params(
url,
&[
("pageSize", page_size),
("pageToken", page_token),
("showDeleted", show_deleted),
],
);
let response = self.client.get(&url).await?;
serde_json::from_slice(&response).map_err(|e| crate::GcpError::InvalidResponse {
message: format!("Failed to parse list_keys response: {e}"),
body: Some(String::from_utf8_lossy(&response).to_string()),
})
}
/// Gets the metadata for an API key. The key string of the API key isn't included in the
/// response. NOTE: Key is a global resource; hence the only supported value for location is
/// `global`.
///
/// **GCP API**: `GET v2/{+name}`
///
/// # Path Parameters
/// - `name` — Required. The resource name of the API key to get. *(required)*
///
/// # Response
/// [`V2Key`]
#[allow(dead_code)]
pub(crate) async fn get_key(&self, name: &str) -> Result<V2Key> {
let url = format!("{}/v2/{}", self.base_url(), name,);
let response = self.client.get(&url).await?;
serde_json::from_slice(&response).map_err(|e| crate::GcpError::InvalidResponse {
message: format!("Failed to parse get_key response: {e}"),
body: Some(String::from_utf8_lossy(&response).to_string()),
})
}
}
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test]
async fn test_list_keys() {
let mut mock = crate::MockClient::new();
mock.expect_get("/v2/test-parent/keys?pageSize=test-pageSize&pageToken=test-pageToken&showDeleted=test-showDeleted")
.returning_json(serde_json::to_value(V2ListKeysResponse::fixture()).unwrap());
let client = crate::GcpHttpClient::from_mock(mock);
let ops = ApikeysOps::new(&client);
let result = ops
.list_keys(
"test-parent",
"test-pageSize",
"test-pageToken",
"test-showDeleted",
)
.await;
assert!(result.is_ok());
}
#[tokio::test]
async fn test_get_key() {
let mut mock = crate::MockClient::new();
mock.expect_get("/v2/test-name")
.returning_json(serde_json::to_value(V2Key::fixture()).unwrap());
let client = crate::GcpHttpClient::from_mock(mock);
let ops = ApikeysOps::new(&client);
let result = ops.get_key("test-name").await;
assert!(result.is_ok());
}
}