Skip to content

Commit d5a1b43

Browse files
curquizaMubelotix
andauthored
Update health method (#119)
* Update health method * Update src/client.rs Co-authored-by: Mubelotix <[email protected]> * Update src/client.rs Co-authored-by: Mubelotix <[email protected]> * Fix tests Co-authored-by: Mubelotix <[email protected]>
1 parent 719b420 commit d5a1b43

File tree

4 files changed

+31
-32
lines changed

4 files changed

+31
-32
lines changed

.github/workflows/pre-release-tests.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ jobs:
1717
runs-on: ubuntu-latest
1818
steps:
1919
- uses: actions/checkout@v2
20+
- name: rustup update
21+
run: rustup update
22+
- name: cargo version
23+
run: cargo --version
2024
- name: Build
2125
run: cargo build --verbose
2226
- name: Get the latest MeiliSearch RC

.github/workflows/tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ jobs:
1818
# Will still run for each push to bump-meilisearch-v*
1919
if: github.event_name != 'pull_request' || !startsWith(github.base_ref, 'bump-meilisearch-v')
2020
name: integration-tests
21-
runs-on: ubuntu-latest
21+
runs-on: ubuntu-18.04
2222
steps:
2323
- uses: actions/checkout@v2
2424
- name: Build

src/client.rs

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -180,30 +180,17 @@ impl<'a> Client<'a> {
180180
/// #
181181
/// # futures::executor::block_on(async move {
182182
/// let client = Client::new("http://localhost:7700", "masterKey");
183-
///
184-
/// match client.health().await {
185-
/// Ok(()) => println!("server is operational"),
186-
/// Err(Error::MeiliSearchError { error_code: ErrorCode::Maintenance, .. }) => {
187-
/// eprintln!("server is in maintenance")
188-
/// },
189-
/// Err(e) => panic!("should never happen: {}", e),
190-
/// }
183+
/// let health = client.health().await.unwrap();
191184
/// # });
192185
/// ```
193-
pub async fn health(&self) -> Result<(), Error> {
194-
let r = request::<(), ()>(
186+
pub async fn health(&self) -> Result<Health, Error> {
187+
request::<serde_json::Value, Health>(
195188
&format!("{}/health", self.host),
196189
self.apikey,
197190
Method::Get,
198-
204,
191+
200,
199192
)
200-
.await;
201-
match r {
202-
// This shouldn't be an error; The status code is 200, but the request
203-
// function only supports one successful error code for some reason
204-
Err(Error::Empty) => Ok(()),
205-
e => e,
206-
}
193+
.await
207194
}
208195

209196
/// Get the private and public key.
@@ -257,6 +244,21 @@ pub struct ClientStats {
257244
pub indexes: HashMap<String, IndexStats>,
258245
}
259246

247+
/// Health of the MeiliSearch server.
248+
///
249+
/// Example:
250+
///
251+
/// ```
252+
/// # use meilisearch_sdk::{client::*, indexes::*, errors::Error};
253+
/// Health {
254+
/// status: "available".to_string(),
255+
/// };
256+
/// ```
257+
#[derive(Deserialize)]
258+
pub struct Health {
259+
pub status: String,
260+
}
261+
260262
#[derive(Deserialize)]
261263
#[serde(rename_all = "camelCase")]
262264
pub struct Keys {
@@ -265,13 +267,16 @@ pub struct Keys {
265267
}
266268

267269
/// Version of a MeiliSearch server.
270+
///
268271
/// Example:
269-
/// ```text
272+
///
273+
/// ```
274+
/// # use meilisearch_sdk::{client::*, indexes::*, errors::Error};
270275
/// Version {
271276
/// commit_sha: "b46889b5f0f2f8b91438a08a358ba8f05fc09fc1".to_string(),
272277
/// build_date: "2019-11-15T09:51:54.278247+00:00".to_string(),
273278
/// pkg_version: "0.1.1".to_string(),
274-
/// }
279+
/// };
275280
/// ```
276281
#[derive(Deserialize)]
277282
#[serde(rename_all = "camelCase")]

src/errors.rs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,6 @@ pub enum Error {
2323
/// This Meilisearch sdk generated an invalid request (which was not sent).
2424
/// It probably comes from an invalid API key resulting in an invalid HTTP header.
2525
InvalidRequest,
26-
/// An erroring status code, but no body
27-
// This is a hack to make Client::health work, since the request module
28-
// treats anything other than the expected status as an error. Since 204 is
29-
// specified, a successful status of 200 is treated as an error with an
30-
// empty body.
31-
Empty,
3226

3327
/// The http client encountered an error.
3428
#[cfg(not(target_arch = "wasm32"))]
@@ -268,8 +262,7 @@ impl std::fmt::Display for Error {
268262
Error::UnreachableServer => write!(fmt, "The MeiliSearch server can't be reached."),
269263
Error::InvalidRequest => write!(fmt, "Unable to generate a valid HTTP request. It probably comes from an invalid API key."),
270264
Error::ParseError(e) => write!(fmt, "Error parsing response JSON: {}", e),
271-
Error::HttpError(e) => write!(fmt, "HTTP request failed: {}", e),
272-
Error::Empty => write!(fmt, "An error occured without a message"),
265+
Error::HttpError(e) => write!(fmt, "HTTP request failed: {}", e)
273266
}
274267
}
275268
}
@@ -278,9 +271,6 @@ impl std::error::Error for Error {}
278271

279272
impl From<&serde_json::Value> for Error {
280273
fn from(json: &serde_json::Value) -> Error {
281-
if json.is_null() {
282-
return Error::Empty;
283-
}
284274

285275
let message = json
286276
.get("message")

0 commit comments

Comments
 (0)