-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
55 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,17 @@ | ||
--- | ||
title: Crates IO API | ||
title: Crates IO API Helper | ||
--- | ||
|
||
In this tutorial, we are going to use the `crates_io_api` crate's [`AsyncClient`] to retrieve | ||
results from a search query to crates.io. | ||
results from a search query to crates.io, and make a helper module to simplify handling of the | ||
request and response for the purposes of the tutorial. | ||
|
||
[`AsyncClient`]: | ||
https://docs.rs/crates_io_api/latest/crates_io_api/struct.AsyncClient.html#method.new | ||
|
||
In order to initialize the client, you have to provide an email as the user agent. In the source | ||
code of this tutorial, we read this email from the environment variable | ||
`CRATES_TUI_TUTORIAL_APP_MYEMAIL` at run time. | ||
`CRATES_TUI_TUTORIAL_APP_MYEMAIL`. | ||
|
||
```rust | ||
let email = env!("CRATES_TUI_TUTORIAL_APP_MYEMAIL"); | ||
|
@@ -29,28 +30,33 @@ You can set up a environment variable for the current session by exporting a var | |
export [email protected] | ||
``` | ||
|
||
Or just hardcode your email into your working copy. | ||
Or just hardcode your email into your working copy of the source code | ||
|
||
```rust | ||
let email = "[email protected]"; | ||
``` | ||
|
||
::: | ||
|
||
Once you have created a client, you can make a query using the [`crates`] function: | ||
Once you have created a client, you can make a query using the [`AsyncClient::crates`] function: | ||
|
||
[`crates`]: https://docs.rs/crates_io_api/latest/crates_io_api/struct.AsyncClient.html#method.crates | ||
[`AsyncClient::crates`]: | ||
https://docs.rs/crates_io_api/latest/crates_io_api/struct.AsyncClient.html#method.crates | ||
|
||
```rust | ||
{{#include @code/crates-tui-tutorial-app/src/crates_io_api_helper.rs:crates_query}} | ||
``` | ||
|
||
This `crates` method takes a [`CratesQuery`] object that you need to construct. | ||
This `crates` method takes a [`CratesQuery`] object that you will need to construct. | ||
|
||
[`CratesQuery`]: https://docs.rs/crates_io_api/latest/crates_io_api/struct.CratesQuery.html | ||
|
||
This `CratesQuery` object can be built with the following parameters | ||
|
||
- Search query string | ||
- Page number | ||
- Page size | ||
- Sort order | ||
- Search query: `String` | ||
- Page number: `u64` | ||
- Page size: `u64` | ||
- Sort order: `crates_io_api::Sort` | ||
|
||
To make the code easier to manage, we'll store everything we need to construct a `CratesQuery` in a | ||
`SearchParameters` struct: | ||
|
@@ -68,8 +74,10 @@ pub struct SearchParameters { | |
} | ||
``` | ||
|
||
A clone of `Arc<Mutex<Vec<crates_io_api::Crate>>>` will be passed into the `async` task within which | ||
it will be populated with the results of the query after it is complete. | ||
You'll notice that we also added a `crates` field to the `SearchParameters`. This `crates` field | ||
will hold a clone of `Arc<Mutex<Vec<crates_io_api::Crate>>>` that will be passed into the `async` | ||
task. Inside this `async` task it will be populated with the results of the query once the query is | ||
completed. | ||
|
||
You can construct the query using `crates_io_api`'s [`CratesQueryBuilder`]: | ||
|
||
|
@@ -80,8 +88,14 @@ You can construct the query using `crates_io_api`'s [`CratesQueryBuilder`]: | |
{{#include @code/crates-tui-tutorial-app/src/bin/part-helper.rs:create_query}} | ||
``` | ||
|
||
Once the request is completed, you can update the `Arc<Mutex<Vec<crates_io_api::Crate>>>` with the | ||
response: | ||
```rust | ||
{{#include @code/crates-tui-tutorial-app/src/bin/part-helper.rs:crates_query}} | ||
|
||
{{#include @code/crates-tui-tutorial-app/src/bin/part-helper.rs:crates_response}} | ||
``` | ||
|
||
Once the request is completed, you can clear the existing results and update the | ||
`Arc<Mutex<Vec<crates_io_api::Crate>>>` (i.e. the `search_params.crates` field) with the response: | ||
|
||
```rust | ||
{{#include @code/crates-tui-tutorial-app/src/bin/part-helper.rs:update_state}} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters