我正在尝试编写一个与 Todoist 的 REST API 通信的库。这个想法是库公开了一个 TodoistAPI 结构,其中包含一个 reqwest::Client 和一个 base_url。有一个new()
函数可以返回一个实例化的 TodoistAPI 结构,客户端的默认标头中有一个不记名令牌(由使用我的库的程序提供)。
但是,我遇到了一个问题,当实际使用客户端发出 API 请求时,根本没有设置默认标头。
TodoistAPI
结构、方法new
和get_projects
方法。
#[derive(Debug)]
pub struct TodoistAPI{
base_url: Url,
client: reqwest::Client
}
impl TodoistAPI {
#[allow(dead_code)]
pub fn new(token: &str) -> Result<TodoistAPI, TodoistAPIError> {
let mut headers = header::HeaderMap::new();
let header_token_value = header::HeaderValue::from_str(token).map_err(TodoistAPIError::InvalidHeaderValue)?;
headers.insert(header::HeaderName::from_bytes(b"Bearer").map_err(TodoistAPIError::InvalidHeaderName)?, header_token_value);
let client = reqwest::Client::builder()
.default_headers(headers)
.build().map_err(TodoistAPIError::Error)?;
println!("{:#?}", client);
let base_url = Url::parse(BASE_URL).map_err(TodoistAPIError::UrlParseError)?;
return Ok(TodoistAPI{ base_url, client })
}
#[allow(dead_code)]
pub async fn get_projects(&self) -> Result<Vec<Project>, TodoistAPIError> {
let url = self.base_url.join("projects").map_err(TodoistAPIError::UrlParseError)?;
let request_builder = self.client.request(reqwest::Method::GET, url);
println!("{:#?}", request_builder);
let request = request_builder.build().map_err(TodoistAPIError::Error)?;
println!("{:#?}", request);
let response = self.client.execute(request).await.map_err(TodoistAPIError::Error)?;
println!("Status: {}", response.status());
println!("STatus: {:#?}", response.text().await.map_err(TodoistAPIError::Error)?);
let url = self.base_url.join("projects").map_err(TodoistAPIError::UrlParseError)?;
let projects = self.client.get(url)
.send()
.await.map_err(TodoistAPIError::Error)?
.json::<Vec<Project>>()
.await.map_err(TodoistAPIError::Error)?;
return Ok(projects);
}
}
一个小的 CLI 程序,它从环境变量中获取令牌并调用该get_projects
方法。
use structopt::StructOpt;
use oxidoist_api::TodoistAPI;
use oxidoist_api::Project;
use oxidoist_api::TodoistAPIError;
use std::env;
#[derive(StructOpt, Debug)]
struct Cli {
verb: String, //get, add, complete, etc.
datatype: String, //project, task, section, etc.
}
#[tokio::main]
async fn main() -> Result<(), TodoistAPIError> {
let args = Cli::from_args();
let token = env::var("TODOIST_API_KEY").unwrap();
let todoist_api_object = TodoistAPI::new(token.as_str()).unwrap();
if args.verb == "get" {
if args.datatype == "projects" {
let projects: Vec<Project> = todoist_api_object.get_projects().await?;
println!("{:?}", projects);
}
}
Ok(())
}
这些println!
语句导致以下输出(带有一些明显经过编辑的私人信息)。
Client {
accepts: Accepts,
proxies: [
Proxy(
System(
{},
),
None,
),
],
referer: true,
default_headers: {
"accept": "*/*",
"bearer": "REDACTED",
},
}
RequestBuilder {
method: GET,
url: Url {
scheme: "https",
host: Some(
Domain(
"api.todoist.com",
),
),
port: None,
path: "/rest/v1/projects",
query: None,
fragment: None,
},
headers: {},
}
Request {
method: GET,
url: Url {
scheme: "https",
host: Some(
Domain(
"api.todoist.com",
),
),
port: None,
path: "/rest/v1/projects",
query: None,
fragment: None,
},
headers: {},
}
Status: 400 Bad Request
STatus: "Empty token\n"
Error: Error(reqwest::Error { kind: Decode, source: Error("expected value", line: 1, column: 1) })
我真的被难住了。我正在阅读的所有内容都表明我做得对,但默认标头绝对不会添加到从客户端生成的请求中。