Skip to content

Commit

Permalink
Implement get api (#21)
Browse files Browse the repository at this point in the history
Signed-off-by: Wilfred Almeida <almeidawilfred642@gmail.com>
Co-authored-by: Jonxslays <51417989+Jonxslays@users.noreply.github.com>
  • Loading branch information
WilfredAlmeida and Jonxslays committed Aug 23, 2023
1 parent 00eaafd commit b11d2e5
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@
- Add `list_keys` method.
- Add various models supporting api service.
- Add various models supporting key service.
- Add `get_api` method.
29 changes: 29 additions & 0 deletions src/client.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use crate::models::CreateKeyRequest;
use crate::models::CreateKeyResponse;
use crate::models::GetApiRequest;
use crate::models::GetApiResponse;
use crate::models::ListKeysRequest;
use crate::models::ListKeysResponse;
use crate::models::RevokeKeyRequest;
Expand Down Expand Up @@ -211,4 +213,31 @@ impl Client {
pub async fn revoke_key(&self, req: RevokeKeyRequest) -> Wrapped<()> {
self.keys.revoke_key(&self.http, req).await
}

/// Retrieves information for the given api id.
///
/// # Arguments
/// - `req`: The get api request to send.
///
/// # Returns
/// A wrapper containing the response, or an [`HttpError`].
///
/// # Example
/// ```no_run
/// # async fn get() {
/// # use unkey::Client;
/// # use unkey::models::GetApiRequest;
/// # use unkey::models::Wrapped;
/// let c = Client::new("abc123");
/// let req = GetApiRequest::new("api_id");
///
/// match c.get_api(req).await {
/// Wrapped::Ok(res) => println!("{:?}", res),
/// Wrapped::Err(err) => println!("{:?}", err),
/// }
/// # }
/// ````
pub async fn get_api(&self, req: GetApiRequest) -> Wrapped<GetApiResponse> {
self.apis.get_api(&self.http, req).await
}
}
47 changes: 47 additions & 0 deletions src/models/apis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,50 @@ pub struct ListKeysResponse {
/// The total number of api keys.
pub total: usize,
}

#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct GetApiRequest {
/// The id of the api to get information for.
pub api_id: String,
}

/// An outgoing get api request.
impl GetApiRequest {
/// Creates a new get api request.
///
/// # Arguments
/// - `api_id`: The id of the api to get api information for.
///
/// # Returns
/// The new get api request.
///
/// # Example
/// ```
/// # use unkey::models::GetApiRequest;
/// let r = GetApiRequest::new("test");
///
/// assert_eq!(r.api_id, String::from("test"));
/// ```
#[must_use]
pub fn new<T: Into<String>>(api_id: T) -> Self {
Self {
api_id: api_id.into(),
}
}
}

/// An incoming get api response.
#[derive(Debug, Clone, Deserialize)]
pub struct GetApiResponse {
/// The id of the api.
#[serde(rename = "id")]
pub api_id: String,

/// The name of the api.
pub name: String,

/// The workspace id of the api.
#[serde(rename = "workspaceId")]
pub workspace_id: String,
}
1 change: 0 additions & 1 deletion src/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ pub(crate) static UPDATE_KEY: Route = Route::new(Method::PUT, "/keys/{}");
////////////////////////////////////////////////////////////////////////////////

/// The get api endpoint `GET /apis/{id}`
#[allow(unused)] // Temporary until we implement this method
pub(crate) static GET_API: Route = Route::new(Method::GET, "/apis/{}");

/// The list keys endpoint `GET /apis/{id}/keys`
Expand Down
17 changes: 17 additions & 0 deletions src/services/apis.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use crate::fetch;
use crate::models::GetApiRequest;
use crate::models::GetApiResponse;
use crate::models::ListKeysRequest;
use crate::models::ListKeysResponse;
use crate::models::Wrapped;
Expand Down Expand Up @@ -39,4 +41,19 @@ impl ApiService {

wrap_response(fetch!(http, route).await).await
}

/// Retrieves api information.
///
/// # Arguments
/// - `http`: The http service to use for the request.
/// - `req`: The request to send.
///
/// # Returns
/// A wrapper around the response, or an [`HttpError`].
pub async fn get_api(&self, http: &HttpService, req: GetApiRequest) -> Wrapped<GetApiResponse> {
let mut route = routes::GET_API.compile();
route.uri_insert(&req.api_id);

wrap_response(fetch!(http, route).await).await
}
}

0 comments on commit b11d2e5

Please sign in to comment.