To specify the parameters for the youtube API endpoint, you can use the url
crate like this:
let mut url = Url::parse("https://example.net")?;
url.query_pairs_mut().append_pair("foo", "bar");
url.query_pairs_mut().append_pair("key", "dkhdsihdsaiufds");
url.query_pairs_mut().append_pair("hello", "world");
println!("{}", url.as_str());
https://example.net/?foo=bar&key=dkhdsihdsaiufds&hello=world
playground
You can now send a request with this url like this:
use hyper_tls::HttpsConnector;
use hyper::{Body, Client};
let request = hyper::Request::get(url.as_str())
.body(Body::empty())?;
let https = HttpsConnector::new();
let client = Client::builder().build::<_, hyper::Body>(https);
let response = client.request(request).await?;
If the API endpoint also requires a request body, you can create it using serde
. For example, to define a comment resource, you do this:
use serde::Serialize;
#[derive(Serialize)]
#[serde(rename = "camelCase")]
struct CommentResource<'a> {
kind: &'a str,
etag: &'a str,
id: &'a str,
snippet: CommentResourceSnippet<'a>,
}
#[derive(Serialize)]
#[serde(rename = "camelCase")]
struct CommentResourceSnippet<'a> {
author_display_name: &'a str,
author_profile_image_url: &'a str,
...
}
Then to use a struct like this as body, you do this:
let comment = CommentResource {
kind: "youtube#comment",
...
};
let request = hyper::Request::post(url.as_str())
.body(Body::from(serde_json::to_vec(&comment)?))?;
let response = client.request(request).await?;
Note that you should try to reuse the Client
instead of creating a new one per request.