nomad-rs-api 0.0.1-alpha.2

Provides an ergonomic, type-safe interface for interacting with the HashiCorp Nomad API (https://developer.hashicorp.com/nomad)
Documentation
use crate::option::QueryOptions;
use crate::{ClientError, Nomad};
use reqwest::Method;

pub struct Endpoint<'a> {
    client: &'a Nomad,
}

impl<'a> Endpoint<'a> {
    /// Create a new `Endpoint` with the given `Nomad` client to interact with
    /// the status endpoints.
    pub fn new(client: &'a Nomad) -> Self {
        Self { client }
    }

    /// # Arguments
    /// `opts`: Optional query options for the request.
    ///
    /// # Returns
    /// A `Result` containing the leader's address as a `String` or an error if
    /// the request fails.
    pub async fn get_leader(&self, opts: Option<QueryOptions>) -> Result<String, ClientError> {
        let req = self.client.set_request_query_options(
            self.client.build_request(Method::GET, "/v1/status/leader"),
            &opts.unwrap_or_default(),
        );
        self.client.send_with_response::<String>(req).await
    }

    /// Get the list of peers in the Nomad cluster.
    ///
    /// # Returns
    /// A `Result` containing a vector of peer addresses as `String`s or an
    /// error if the request fails.
    pub async fn list_peers(&self) -> Result<Vec<String>, ClientError> {
        let req = self.client.build_request(Method::GET, "/v1/status/peers");
        self.client.send_with_response::<Vec<String>>(req).await
    }
}