Skip to content

Commit

Permalink
Renamed Api::try_get to get_opt
Browse files Browse the repository at this point in the history
In accordance with feedback in the PR.

Signed-off-by: Teo Klestrup Röijezon <teo@nullable.se>
  • Loading branch information
nightkr committed Feb 11, 2022
1 parent bf21fbf commit cbf5f91
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
9 changes: 7 additions & 2 deletions kube-client/src/api/core_methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ where
/// Ok(())
/// }
/// ```
///
/// # Errors
///
/// This function assumes that the object is expected to always exist, and returns [`Error`] if it does not.
/// Consider using [`Api::get_opt`] if you need to handle missing objects.
pub async fn get(&self, name: &str) -> Result<K> {
let mut req = self.request.get(name).map_err(Error::BuildRequest)?;
req.extensions_mut().insert("get");
Expand All @@ -39,15 +44,15 @@ where
/// async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let client = Client::try_default().await?;
/// let pods: Api<Pod> = Api::namespaced(client, "apps");
/// if let Some(pod) = pods.try_get("blog").await? {
/// if let Some(pod) = pods.get_opt("blog").await? {
/// // Pod was found
/// } else {
/// // Pod was not found
/// }
/// Ok(())
/// }
/// ```
pub async fn try_get(&self, name: &str) -> Result<Option<K>> {
pub async fn get_opt(&self, name: &str) -> Result<Option<K>> {
match self.get(name).await {
Ok(obj) => Ok(Some(obj)),
Err(Error::Api(ErrorResponse { reason, .. })) if &reason == "NotFound" => Ok(None),
Expand Down
4 changes: 2 additions & 2 deletions kube/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -503,11 +503,11 @@ mod test {

#[tokio::test]
#[ignore] // needs cluster (lists cms)
async fn api_try_get_handles_404() -> Result<(), Box<dyn std::error::Error>> {
async fn api_get_opt_handles_404() -> Result<(), Box<dyn std::error::Error>> {
let client = Client::try_default().await?;
let api = Api::<ConfigMap>::default_namespaced(client);
assert_eq!(
api.try_get("this-cm-does-not-exist-ajklisdhfqkljwhreq").await?,
api.get_opt("this-cm-does-not-exist-ajklisdhfqkljwhreq").await?,
None
);
Ok(())
Expand Down

0 comments on commit cbf5f91

Please sign in to comment.