Skip to content

Commit 2ac7738

Browse files
bors[bot]bidoubiwa
andauthored
Merge #379
379: Add possibility to have both query parameters and bodies on http requests r=bidoubiwa a=bidoubiwa Previously, it was only possible to provide one parameter to any HTTP request method. For example - `Post(something)` something would be resolved as body - `Get(something)` something would be resolved as query parameter The issue is raised when we need to use `query parameters` on a `POST` route for example. It was not planned. With this PR, it is now possible to provide both query parameters and bodies to: POST/PATCH/PUT routes While technically you can send a body to a `DELETE` requests, I did not add it as it is bad practice. Co-authored-by: Charlotte Vermandel <[email protected]>
2 parents 0878676 + 83411fe commit 2ac7738

File tree

5 files changed

+315
-179
lines changed

5 files changed

+315
-179
lines changed

src/client.rs

Lines changed: 77 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -130,10 +130,10 @@ impl Client {
130130
/// # });
131131
/// ```
132132
pub async fn list_all_indexes_raw(&self) -> Result<Value, Error> {
133-
let json_indexes = request::<(), Value>(
133+
let json_indexes = request::<(), (), Value>(
134134
&format!("{}/indexes", self.host),
135135
&self.api_key,
136-
Method::Get(()),
136+
Method::Get { query: () },
137137
200,
138138
)
139139
.await?;
@@ -166,10 +166,12 @@ impl Client {
166166
&self,
167167
indexes_query: &IndexesQuery<'_>,
168168
) -> Result<Value, Error> {
169-
let json_indexes = request::<&IndexesQuery, Value>(
169+
let json_indexes = request::<&IndexesQuery, (), Value>(
170170
&format!("{}/indexes", self.host),
171171
&self.api_key,
172-
Method::Get(indexes_query),
172+
Method::Get {
173+
query: indexes_query,
174+
},
173175
200,
174176
)
175177
.await?;
@@ -227,10 +229,10 @@ impl Client {
227229
/// ```
228230
/// If you use it directly from an [Index], you can use the method [Index::fetch_info], which is the equivalent method from an index.
229231
pub async fn get_raw_index(&self, uid: impl AsRef<str>) -> Result<Value, Error> {
230-
request::<(), Value>(
232+
request::<(), (), Value>(
231233
&format!("{}/indexes/{}", self.host, uid.as_ref()),
232234
&self.api_key,
233-
Method::Get(()),
235+
Method::Get { query: () },
234236
200,
235237
)
236238
.await
@@ -274,13 +276,16 @@ impl Client {
274276
uid: impl AsRef<str>,
275277
primary_key: Option<&str>,
276278
) -> Result<TaskInfo, Error> {
277-
request::<Value, TaskInfo>(
279+
request::<(), Value, TaskInfo>(
278280
&format!("{}/indexes", self.host),
279281
&self.api_key,
280-
Method::Post(json!({
281-
"uid": uid.as_ref(),
282-
"primaryKey": primary_key,
283-
})),
282+
Method::Post {
283+
query: (),
284+
body: json!({
285+
"uid": uid.as_ref(),
286+
"primaryKey": primary_key,
287+
}),
288+
},
284289
202,
285290
)
286291
.await
@@ -289,10 +294,10 @@ impl Client {
289294
/// Delete an index from its UID.
290295
/// To delete an [Index], use the [Index::delete] method.
291296
pub async fn delete_index(&self, uid: impl AsRef<str>) -> Result<TaskInfo, Error> {
292-
request::<(), TaskInfo>(
297+
request::<(), (), TaskInfo>(
293298
&format!("{}/indexes/{}", self.host, uid.as_ref()),
294299
&self.api_key,
295-
Method::Delete,
300+
Method::Delete { query: () },
296301
202,
297302
)
298303
.await
@@ -340,10 +345,10 @@ impl Client {
340345
/// # });
341346
/// ```
342347
pub async fn get_stats(&self) -> Result<ClientStats, Error> {
343-
request::<(), ClientStats>(
348+
request::<(), (), ClientStats>(
344349
&format!("{}/stats", self.host),
345350
&self.api_key,
346-
Method::Get(()),
351+
Method::Get { query: () },
347352
200,
348353
)
349354
.await
@@ -366,10 +371,10 @@ impl Client {
366371
/// # });
367372
/// ```
368373
pub async fn health(&self) -> Result<Health, Error> {
369-
request::<(), Health>(
374+
request::<(), (), Health>(
370375
&format!("{}/health", self.host),
371376
&self.api_key,
372-
Method::Get(()),
377+
Method::Get { query: () },
373378
200,
374379
)
375380
.await
@@ -422,10 +427,10 @@ impl Client {
422427
/// # });
423428
/// ```
424429
pub async fn get_keys_with(&self, keys_query: &KeysQuery) -> Result<KeysResults, Error> {
425-
let keys = request::<&KeysQuery, KeysResults>(
430+
let keys = request::<&KeysQuery, (), KeysResults>(
426431
&format!("{}/keys", self.host),
427432
&self.api_key,
428-
Method::Get(keys_query),
433+
Method::Get { query: keys_query },
429434
200,
430435
)
431436
.await?;
@@ -454,10 +459,10 @@ impl Client {
454459
/// # });
455460
/// ```
456461
pub async fn get_keys(&self) -> Result<KeysResults, Error> {
457-
let keys = request::<(), KeysResults>(
462+
let keys = request::<(), (), KeysResults>(
458463
&format!("{}/keys", self.host),
459464
&self.api_key,
460-
Method::Get(()),
465+
Method::Get { query: () },
461466
200,
462467
)
463468
.await?;
@@ -490,10 +495,10 @@ impl Client {
490495
/// # });
491496
/// ```
492497
pub async fn get_key(&self, key: impl AsRef<str>) -> Result<Key, Error> {
493-
request::<(), Key>(
498+
request::<(), (), Key>(
494499
&format!("{}/keys/{}", self.host, key.as_ref()),
495500
&self.api_key,
496-
Method::Get(()),
501+
Method::Get { query: () },
497502
200,
498503
)
499504
.await
@@ -525,10 +530,10 @@ impl Client {
525530
/// # });
526531
/// ```
527532
pub async fn delete_key(&self, key: impl AsRef<str>) -> Result<(), Error> {
528-
request::<(), ()>(
533+
request::<(), (), ()>(
529534
&format!("{}/keys/{}", self.host, key.as_ref()),
530535
&self.api_key,
531-
Method::Delete,
536+
Method::Delete { query: () },
532537
204,
533538
)
534539
.await
@@ -559,10 +564,13 @@ impl Client {
559564
/// # });
560565
/// ```
561566
pub async fn create_key(&self, key: impl AsRef<KeyBuilder>) -> Result<Key, Error> {
562-
request::<&KeyBuilder, Key>(
567+
request::<(), &KeyBuilder, Key>(
563568
&format!("{}/keys", self.host),
564569
&self.api_key,
565-
Method::Post(key.as_ref()),
570+
Method::Post {
571+
query: (),
572+
body: key.as_ref(),
573+
},
566574
201,
567575
)
568576
.await
@@ -595,10 +603,13 @@ impl Client {
595603
/// # });
596604
/// ```
597605
pub async fn update_key(&self, key: impl AsRef<KeyUpdater>) -> Result<Key, Error> {
598-
request::<&KeyUpdater, Key>(
606+
request::<(), &KeyUpdater, Key>(
599607
&format!("{}/keys/{}", self.host, key.as_ref().key),
600608
&self.api_key,
601-
Method::Patch(key.as_ref()),
609+
Method::Patch {
610+
body: key.as_ref(),
611+
query: (),
612+
},
602613
200,
603614
)
604615
.await
@@ -620,10 +631,10 @@ impl Client {
620631
/// # });
621632
/// ```
622633
pub async fn get_version(&self) -> Result<Version, Error> {
623-
request::<(), Version>(
634+
request::<(), (), Version>(
624635
&format!("{}/version", self.host),
625636
&self.api_key,
626-
Method::Get(()),
637+
Method::Get { query: () },
627638
200,
628639
)
629640
.await
@@ -721,10 +732,10 @@ impl Client {
721732
/// # });
722733
/// ```
723734
pub async fn get_task(&self, task_id: impl AsRef<u32>) -> Result<Task, Error> {
724-
request::<(), Task>(
735+
request::<(), (), Task>(
725736
&format!("{}/tasks/{}", self.host, task_id.as_ref()),
726737
&self.api_key,
727-
Method::Get(()),
738+
Method::Get { query: () },
728739
200,
729740
)
730741
.await
@@ -752,10 +763,10 @@ impl Client {
752763
&self,
753764
tasks_query: &TasksQuery<'_>,
754765
) -> Result<TasksResults, Error> {
755-
let tasks = request::<&TasksQuery, TasksResults>(
766+
let tasks = request::<&TasksQuery, (), TasksResults>(
756767
&format!("{}/tasks", self.host),
757768
&self.api_key,
758-
Method::Get(tasks_query),
769+
Method::Get { query: tasks_query },
759770
200,
760771
)
761772
.await?;
@@ -782,10 +793,10 @@ impl Client {
782793
/// # });
783794
/// ```
784795
pub async fn get_tasks(&self) -> Result<TasksResults, Error> {
785-
let tasks = request::<(), TasksResults>(
796+
let tasks = request::<(), (), TasksResults>(
786797
&format!("{}/tasks", self.host),
787798
&self.api_key,
788-
Method::Get(()),
799+
Method::Get { query: () },
789800
200,
790801
)
791802
.await?;
@@ -891,31 +902,55 @@ mod tests {
891902
mock("GET", path)
892903
.match_header("User-Agent", user_agent)
893904
.create(),
894-
request::<(), ()>(address, "", Method::Get(()), 200),
905+
request::<(), (), ()>(address, "", Method::Get { query: () }, 200),
895906
),
896907
(
897908
mock("POST", path)
898909
.match_header("User-Agent", user_agent)
899910
.create(),
900-
request::<(), ()>(address, "", Method::Post(()), 200),
911+
request::<(), (), ()>(
912+
address,
913+
"",
914+
Method::Post {
915+
query: (),
916+
body: {},
917+
},
918+
200,
919+
),
901920
),
902921
(
903922
mock("DELETE", path)
904923
.match_header("User-Agent", user_agent)
905924
.create(),
906-
request::<(), ()>(address, "", Method::Delete, 200),
925+
request::<(), (), ()>(address, "", Method::Delete { query: () }, 200),
907926
),
908927
(
909928
mock("PUT", path)
910929
.match_header("User-Agent", user_agent)
911930
.create(),
912-
request::<(), ()>(address, "", Method::Put(()), 200),
931+
request::<(), (), ()>(
932+
address,
933+
"",
934+
Method::Put {
935+
query: (),
936+
body: (),
937+
},
938+
200,
939+
),
913940
),
914941
(
915942
mock("PATCH", path)
916943
.match_header("User-Agent", user_agent)
917944
.create(),
918-
request::<(), ()>(address, "", Method::Patch(()), 200),
945+
request::<(), (), ()>(
946+
address,
947+
"",
948+
Method::Patch {
949+
query: (),
950+
body: (),
951+
},
952+
200,
953+
),
919954
),
920955
];
921956

src/dumps.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,13 @@ impl Client {
7070
/// # });
7171
/// ```
7272
pub async fn create_dump(&self) -> Result<TaskInfo, Error> {
73-
request::<(), TaskInfo>(
73+
request::<(), (), TaskInfo>(
7474
&format!("{}/dumps", self.host),
7575
&self.api_key,
76-
Method::Post(()),
76+
Method::Post {
77+
query: (),
78+
body: (),
79+
},
7780
202,
7881
)
7982
.await

0 commit comments

Comments
 (0)