Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
time = { version = "0.3.7", features = ["serde-well-known", "formatting", "parsing"] }
jsonwebtoken = { version = "8", default-features = false }
yaup = "0.2.0"

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
futures = "0.3"
Expand Down
32 changes: 16 additions & 16 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ impl Client {
let json_indexes = request::<(), Vec<Value>>(
&format!("{}/indexes", self.host),
&self.api_key,
Method::Get,
Method::Get(()),
200,
)
.await?;
Expand Down Expand Up @@ -148,7 +148,7 @@ impl Client {
request::<(), Value>(
&format!("{}/indexes/{}", self.host, uid.as_ref()),
&self.api_key,
Method::Get,
Method::Get(()),
200,
)
.await
Expand Down Expand Up @@ -248,10 +248,10 @@ impl Client {
/// # });
/// ```
pub async fn get_stats(&self) -> Result<ClientStats, Error> {
request::<serde_json::Value, ClientStats>(
request::<(), ClientStats>(
&format!("{}/stats", self.host),
&self.api_key,
Method::Get,
Method::Get(()),
200,
)
.await
Expand All @@ -274,10 +274,10 @@ impl Client {
/// # });
/// ```
pub async fn health(&self) -> Result<Health, Error> {
request::<serde_json::Value, Health>(
request::<(), Health>(
&format!("{}/health", self.host),
&self.api_key,
Method::Get,
Method::Get(()),
200,
)
.await
Expand Down Expand Up @@ -337,7 +337,7 @@ impl Client {
let keys = request::<(), Keys>(
&format!("{}/keys", self.host),
&self.api_key,
Method::Get,
Method::Get(()),
200,
)
.await?;
Expand Down Expand Up @@ -371,7 +371,7 @@ impl Client {
request::<(), Key>(
&format!("{}/keys/{}", self.host, key.as_ref()),
&self.api_key,
Method::Get,
Method::Get(()),
200,
)
.await
Expand Down Expand Up @@ -498,7 +498,7 @@ impl Client {
request::<(), Version>(
&format!("{}/version", self.host),
&self.api_key,
Method::Get,
Method::Get(()),
200,
)
.await
Expand Down Expand Up @@ -600,7 +600,7 @@ impl Client {
request::<(), Task>(
&format!("{}/tasks/{}", self.host, task_id.as_ref()),
&self.api_key,
Method::Get,
Method::Get(()),
200,
)
.await
Expand Down Expand Up @@ -630,7 +630,7 @@ impl Client {
let tasks = request::<(), Tasks>(
&format!("{}/tasks", self.host),
&self.api_key,
Method::Get,
Method::Get(()),
200,
)
.await?;
Expand Down Expand Up @@ -733,31 +733,31 @@ mod tests {
mock("GET", path)
.match_header("User-Agent", user_agent)
.create(),
request::<String, ()>(address, "", Method::Get, 200),
request::<(), ()>(address, "", Method::Get(()), 200),
),
(
mock("POST", path)
.match_header("User-Agent", user_agent)
.create(),
request::<String, ()>(address, "", Method::Post("".to_string()), 200),
request::<(), ()>(address, "", Method::Post(()), 200),
),
(
mock("DELETE", path)
.match_header("User-Agent", user_agent)
.create(),
request::<String, ()>(address, "", Method::Delete, 200),
request::<(), ()>(address, "", Method::Delete, 200),
),
(
mock("PUT", path)
.match_header("User-Agent", user_agent)
.create(),
request::<String, ()>(address, "", Method::Put("".to_string()), 200),
request::<(), ()>(address, "", Method::Put(()), 200),
),
(
mock("PATCH", path)
.match_header("User-Agent", user_agent)
.create(),
request::<String, ()>(address, "", Method::Patch("".to_string()), 200),
request::<(), ()>(address, "", Method::Patch(()), 200),
),
];

Expand Down
2 changes: 1 addition & 1 deletion src/dumps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ impl Client {
request::<(), DumpInfo>(
&format!("{}/dumps/{}/status", self.host, dump_uid.as_ref()),
&self.api_key,
Method::Get,
Method::Get(()),
200,
)
.await
Expand Down
13 changes: 11 additions & 2 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub enum Error {
TenantTokensInvalidApiKey,
/// It is not possible to generate an already expired tenant token.
TenantTokensExpiredSignature,

/// When jsonwebtoken cannot generate the token successfully.
InvalidTenantToken(jsonwebtoken::errors::Error),

Expand All @@ -34,6 +34,8 @@ pub enum Error {
/// The http client encountered an error.
#[cfg(target_arch = "wasm32")]
HttpError(String),
// The library formating the query parameters encountered an error.
Yaup(yaup::Error),
}

#[derive(Debug, Clone, Deserialize)]
Expand Down Expand Up @@ -67,6 +69,12 @@ impl From<jsonwebtoken::errors::Error> for Error {
}
}

impl From<yaup::Error> for Error {
fn from(error: yaup::Error) -> Error {
Error::Yaup(error)
}
}

/// The type of error that was encountered.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
Expand Down Expand Up @@ -185,7 +193,8 @@ impl std::fmt::Display for Error {
Error::Timeout => write!(fmt, "A task did not succeed in time."),
Error::TenantTokensInvalidApiKey => write!(fmt, "The provided api_key is invalid."),
Error::TenantTokensExpiredSignature => write!(fmt, "The provided expires_at is already expired."),
Error::InvalidTenantToken(e) => write!(fmt, "Impossible to generate the token, jsonwebtoken encountered an error: {}", e)
Error::InvalidTenantToken(e) => write!(fmt, "Impossible to generate the token, jsonwebtoken encountered an error: {}", e),
Error::Yaup(e) => write!(fmt, "Internal Error: could not parse the query parameters: {}", e)
}
}
}
Expand Down
12 changes: 6 additions & 6 deletions src/indexes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ impl Index {
self.client.host, self.uid, uid
),
&self.client.api_key,
Method::Get,
Method::Get(()),
200,
)
.await
Expand Down Expand Up @@ -330,7 +330,7 @@ impl Index {
url.push_str("attributesToRetrieve=");
url.push_str(attributes_to_retrieve);
}
request::<(), Vec<T>>(&url, &self.client.api_key, Method::Get, 200).await
request::<(), Vec<T>>(&url, &self.client.api_key, Method::Get(()), 200).await
}

/// Add a list of [Document]s or replace them if they already exist.
Expand Down Expand Up @@ -742,7 +742,7 @@ impl Index {
uid.as_ref()
),
&self.client.api_key,
Method::Get,
Method::Get(()),
200,
)
.await
Expand Down Expand Up @@ -782,7 +782,7 @@ impl Index {
Ok(request::<(), AllTasks>(
&format!("{}/indexes/{}/tasks", self.client.host, self.uid),
&self.client.api_key,
Method::Get,
Method::Get(()),
200,
)
.await?
Expand All @@ -809,10 +809,10 @@ impl Index {
/// # });
/// ```
pub async fn get_stats(&self) -> Result<IndexStats, Error> {
request::<serde_json::Value, IndexStats>(
request::<(), IndexStats>(
&format!("{}/indexes/{}/stats", self.client.host, self.uid),
&self.client.api_key,
Method::Get,
Method::Get(()),
200,
)
.await
Expand Down
12 changes: 10 additions & 2 deletions src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use serde_json::{from_str, to_string};

#[derive(Debug)]
pub(crate) enum Method<T: Serialize> {
Get,
Get(T),
Post(T),
Patch(T),
Put(T),
Expand All @@ -26,7 +26,15 @@ pub(crate) async fn request<Input: Serialize, Output: DeserializeOwned + 'static
let user_agent = qualified_version();

let mut response = match &method {
Method::Get => {
Method::Get(query) => {
let query = yaup::to_string(query)?;

let url = if query.is_empty() {
url.to_string()
} else {
format!("{}?{}", url, query)
};

Request::get(url)
.header(header::AUTHORIZATION, auth)
.header(header::USER_AGENT, user_agent)
Expand Down
18 changes: 9 additions & 9 deletions src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ impl Index {
request::<(), Settings>(
&format!("{}/indexes/{}/settings", self.client.host, self.uid),
&self.client.api_key,
Method::Get,
Method::Get(()),
200,
)
.await
Expand Down Expand Up @@ -245,7 +245,7 @@ impl Index {
self.client.host, self.uid
),
&self.client.api_key,
Method::Get,
Method::Get(()),
200,
)
.await
Expand Down Expand Up @@ -274,7 +274,7 @@ impl Index {
self.client.host, self.uid
),
&self.client.api_key,
Method::Get,
Method::Get(()),
200,
)
.await
Expand Down Expand Up @@ -303,7 +303,7 @@ impl Index {
self.client.host, self.uid
),
&self.client.api_key,
Method::Get,
Method::Get(()),
200,
)
.await
Expand Down Expand Up @@ -332,7 +332,7 @@ impl Index {
self.client.host, self.uid
),
&self.client.api_key,
Method::Get,
Method::Get(()),
200,
)
.await
Expand Down Expand Up @@ -361,7 +361,7 @@ impl Index {
self.client.host, self.uid
),
&self.client.api_key,
Method::Get,
Method::Get(()),
200,
)
.await
Expand Down Expand Up @@ -390,7 +390,7 @@ impl Index {
self.client.host, self.uid
),
&self.client.api_key,
Method::Get,
Method::Get(()),
200,
)
.await
Expand Down Expand Up @@ -419,7 +419,7 @@ impl Index {
self.client.host, self.uid
),
&self.client.api_key,
Method::Get,
Method::Get(()),
200,
)
.await
Expand Down Expand Up @@ -448,7 +448,7 @@ impl Index {
self.client.host, self.uid
),
&self.client.api_key,
Method::Get,
Method::Get(()),
200,
)
.await
Expand Down