Skip to content

Commit 216638f

Browse files
authored
Add yaup to format query parameters (#309)
* Add yaup to format query parameters * Add missing parameter in GET method call * Remove dbg * Add comment on yaup error
1 parent 6f98caf commit 216638f

File tree

7 files changed

+54
-36
lines changed

7 files changed

+54
-36
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ serde = { version = "1.0", features = ["derive"] }
1919
serde_json = "1.0"
2020
time = { version = "0.3.7", features = ["serde-well-known", "formatting", "parsing"] }
2121
jsonwebtoken = { version = "8", default-features = false }
22+
yaup = "0.2.0"
2223

2324
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
2425
futures = "0.3"

src/client.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ impl Client {
8787
let json_indexes = request::<(), Vec<Value>>(
8888
&format!("{}/indexes", self.host),
8989
&self.api_key,
90-
Method::Get,
90+
Method::Get(()),
9191
200,
9292
)
9393
.await?;
@@ -148,7 +148,7 @@ impl Client {
148148
request::<(), Value>(
149149
&format!("{}/indexes/{}", self.host, uid.as_ref()),
150150
&self.api_key,
151-
Method::Get,
151+
Method::Get(()),
152152
200,
153153
)
154154
.await
@@ -248,10 +248,10 @@ impl Client {
248248
/// # });
249249
/// ```
250250
pub async fn get_stats(&self) -> Result<ClientStats, Error> {
251-
request::<serde_json::Value, ClientStats>(
251+
request::<(), ClientStats>(
252252
&format!("{}/stats", self.host),
253253
&self.api_key,
254-
Method::Get,
254+
Method::Get(()),
255255
200,
256256
)
257257
.await
@@ -274,10 +274,10 @@ impl Client {
274274
/// # });
275275
/// ```
276276
pub async fn health(&self) -> Result<Health, Error> {
277-
request::<serde_json::Value, Health>(
277+
request::<(), Health>(
278278
&format!("{}/health", self.host),
279279
&self.api_key,
280-
Method::Get,
280+
Method::Get(()),
281281
200,
282282
)
283283
.await
@@ -337,7 +337,7 @@ impl Client {
337337
let keys = request::<(), Keys>(
338338
&format!("{}/keys", self.host),
339339
&self.api_key,
340-
Method::Get,
340+
Method::Get(()),
341341
200,
342342
)
343343
.await?;
@@ -371,7 +371,7 @@ impl Client {
371371
request::<(), Key>(
372372
&format!("{}/keys/{}", self.host, key.as_ref()),
373373
&self.api_key,
374-
Method::Get,
374+
Method::Get(()),
375375
200,
376376
)
377377
.await
@@ -498,7 +498,7 @@ impl Client {
498498
request::<(), Version>(
499499
&format!("{}/version", self.host),
500500
&self.api_key,
501-
Method::Get,
501+
Method::Get(()),
502502
200,
503503
)
504504
.await
@@ -600,7 +600,7 @@ impl Client {
600600
request::<(), Task>(
601601
&format!("{}/tasks/{}", self.host, task_id.as_ref()),
602602
&self.api_key,
603-
Method::Get,
603+
Method::Get(()),
604604
200,
605605
)
606606
.await
@@ -630,7 +630,7 @@ impl Client {
630630
let tasks = request::<(), Tasks>(
631631
&format!("{}/tasks", self.host),
632632
&self.api_key,
633-
Method::Get,
633+
Method::Get(()),
634634
200,
635635
)
636636
.await?;
@@ -733,31 +733,31 @@ mod tests {
733733
mock("GET", path)
734734
.match_header("User-Agent", user_agent)
735735
.create(),
736-
request::<String, ()>(address, "", Method::Get, 200),
736+
request::<(), ()>(address, "", Method::Get(()), 200),
737737
),
738738
(
739739
mock("POST", path)
740740
.match_header("User-Agent", user_agent)
741741
.create(),
742-
request::<String, ()>(address, "", Method::Post("".to_string()), 200),
742+
request::<(), ()>(address, "", Method::Post(()), 200),
743743
),
744744
(
745745
mock("DELETE", path)
746746
.match_header("User-Agent", user_agent)
747747
.create(),
748-
request::<String, ()>(address, "", Method::Delete, 200),
748+
request::<(), ()>(address, "", Method::Delete, 200),
749749
),
750750
(
751751
mock("PUT", path)
752752
.match_header("User-Agent", user_agent)
753753
.create(),
754-
request::<String, ()>(address, "", Method::Put("".to_string()), 200),
754+
request::<(), ()>(address, "", Method::Put(()), 200),
755755
),
756756
(
757757
mock("PATCH", path)
758758
.match_header("User-Agent", user_agent)
759759
.create(),
760-
request::<String, ()>(address, "", Method::Patch("".to_string()), 200),
760+
request::<(), ()>(address, "", Method::Patch(()), 200),
761761
),
762762
];
763763

src/dumps.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ impl Client {
126126
request::<(), DumpInfo>(
127127
&format!("{}/dumps/{}/status", self.host, dump_uid.as_ref()),
128128
&self.api_key,
129-
Method::Get,
129+
Method::Get(()),
130130
200,
131131
)
132132
.await

src/errors.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ pub enum Error {
2424
TenantTokensInvalidApiKey,
2525
/// It is not possible to generate an already expired tenant token.
2626
TenantTokensExpiredSignature,
27-
27+
2828
/// When jsonwebtoken cannot generate the token successfully.
2929
InvalidTenantToken(jsonwebtoken::errors::Error),
3030

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

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

72+
impl From<yaup::Error> for Error {
73+
fn from(error: yaup::Error) -> Error {
74+
Error::Yaup(error)
75+
}
76+
}
77+
7078
/// The type of error that was encountered.
7179
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
7280
#[serde(rename_all = "snake_case")]
@@ -185,7 +193,8 @@ impl std::fmt::Display for Error {
185193
Error::Timeout => write!(fmt, "A task did not succeed in time."),
186194
Error::TenantTokensInvalidApiKey => write!(fmt, "The provided api_key is invalid."),
187195
Error::TenantTokensExpiredSignature => write!(fmt, "The provided expires_at is already expired."),
188-
Error::InvalidTenantToken(e) => write!(fmt, "Impossible to generate the token, jsonwebtoken encountered an error: {}", e)
196+
Error::InvalidTenantToken(e) => write!(fmt, "Impossible to generate the token, jsonwebtoken encountered an error: {}", e),
197+
Error::Yaup(e) => write!(fmt, "Internal Error: could not parse the query parameters: {}", e)
189198
}
190199
}
191200
}

src/indexes.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ impl Index {
264264
self.client.host, self.uid, uid
265265
),
266266
&self.client.api_key,
267-
Method::Get,
267+
Method::Get(()),
268268
200,
269269
)
270270
.await
@@ -330,7 +330,7 @@ impl Index {
330330
url.push_str("attributesToRetrieve=");
331331
url.push_str(attributes_to_retrieve);
332332
}
333-
request::<(), Vec<T>>(&url, &self.client.api_key, Method::Get, 200).await
333+
request::<(), Vec<T>>(&url, &self.client.api_key, Method::Get(()), 200).await
334334
}
335335

336336
/// Add a list of [Document]s or replace them if they already exist.
@@ -742,7 +742,7 @@ impl Index {
742742
uid.as_ref()
743743
),
744744
&self.client.api_key,
745-
Method::Get,
745+
Method::Get(()),
746746
200,
747747
)
748748
.await
@@ -782,7 +782,7 @@ impl Index {
782782
Ok(request::<(), AllTasks>(
783783
&format!("{}/indexes/{}/tasks", self.client.host, self.uid),
784784
&self.client.api_key,
785-
Method::Get,
785+
Method::Get(()),
786786
200,
787787
)
788788
.await?
@@ -809,10 +809,10 @@ impl Index {
809809
/// # });
810810
/// ```
811811
pub async fn get_stats(&self) -> Result<IndexStats, Error> {
812-
request::<serde_json::Value, IndexStats>(
812+
request::<(), IndexStats>(
813813
&format!("{}/indexes/{}/stats", self.client.host, self.uid),
814814
&self.client.api_key,
815-
Method::Get,
815+
Method::Get(()),
816816
200,
817817
)
818818
.await

src/request.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use serde_json::{from_str, to_string};
55

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

2828
let mut response = match &method {
29-
Method::Get => {
29+
Method::Get(query) => {
30+
let query = yaup::to_string(query)?;
31+
32+
let url = if query.is_empty() {
33+
url.to_string()
34+
} else {
35+
format!("{}?{}", url, query)
36+
};
37+
3038
Request::get(url)
3139
.header(header::AUTHORIZATION, auth)
3240
.header(header::USER_AGENT, user_agent)

src/settings.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ impl Index {
216216
request::<(), Settings>(
217217
&format!("{}/indexes/{}/settings", self.client.host, self.uid),
218218
&self.client.api_key,
219-
Method::Get,
219+
Method::Get(()),
220220
200,
221221
)
222222
.await
@@ -245,7 +245,7 @@ impl Index {
245245
self.client.host, self.uid
246246
),
247247
&self.client.api_key,
248-
Method::Get,
248+
Method::Get(()),
249249
200,
250250
)
251251
.await
@@ -274,7 +274,7 @@ impl Index {
274274
self.client.host, self.uid
275275
),
276276
&self.client.api_key,
277-
Method::Get,
277+
Method::Get(()),
278278
200,
279279
)
280280
.await
@@ -303,7 +303,7 @@ impl Index {
303303
self.client.host, self.uid
304304
),
305305
&self.client.api_key,
306-
Method::Get,
306+
Method::Get(()),
307307
200,
308308
)
309309
.await
@@ -332,7 +332,7 @@ impl Index {
332332
self.client.host, self.uid
333333
),
334334
&self.client.api_key,
335-
Method::Get,
335+
Method::Get(()),
336336
200,
337337
)
338338
.await
@@ -361,7 +361,7 @@ impl Index {
361361
self.client.host, self.uid
362362
),
363363
&self.client.api_key,
364-
Method::Get,
364+
Method::Get(()),
365365
200,
366366
)
367367
.await
@@ -390,7 +390,7 @@ impl Index {
390390
self.client.host, self.uid
391391
),
392392
&self.client.api_key,
393-
Method::Get,
393+
Method::Get(()),
394394
200,
395395
)
396396
.await
@@ -419,7 +419,7 @@ impl Index {
419419
self.client.host, self.uid
420420
),
421421
&self.client.api_key,
422-
Method::Get,
422+
Method::Get(()),
423423
200,
424424
)
425425
.await
@@ -448,7 +448,7 @@ impl Index {
448448
self.client.host, self.uid
449449
),
450450
&self.client.api_key,
451-
Method::Get,
451+
Method::Get(()),
452452
200,
453453
)
454454
.await

0 commit comments

Comments
 (0)