Fix: api_call changes. New headers for json + returning a Result now

This commit is contained in:
Óscar M. Lage 2021-11-15 18:37:53 +01:00
parent b12ce8dddc
commit 43f668a059

View File

@ -1,9 +1,41 @@
use std::path::PathBuf; use std::path::PathBuf;
use serde::{Serialize, Deserialize};
#[path = "./config.rs"] #[path = "./config.rs"]
mod config; mod config;
pub fn api_call(config_file: &PathBuf, endpoint: String) -> String { #[derive(Serialize, Deserialize, Debug)]
struct Project {
id: u32,
name: String,
slug: String,
description: Option<String>,
status: String,
user_id: u32,
paused: u32,
}
#[derive(Serialize, Deserialize, Debug)]
struct ProjectsResponse {
data: Vec<Project>,
error: String,
}
#[derive(Serialize, Deserialize, Debug)]
struct Task {
id: u32,
name: String,
description: Option<String>,
project_id: u32,
}
#[derive(Serialize, Deserialize, Debug)]
struct TasksResponse {
data: Vec<Task>,
error: String,
}
pub fn api_call(config_file: &PathBuf, endpoint: String) -> Result<reqwest::blocking::Response, reqwest::Error> {
// Config // Config
let mut default_config = config::Config::default(); let mut default_config = config::Config::default();
let config = default_config.parse(&config_file); let config = default_config.parse(&config_file);
@ -12,13 +44,11 @@ pub fn api_call(config_file: &PathBuf, endpoint: String) -> String {
let endpoint = format!("{}{}", &config.url, endpoint); let endpoint = format!("{}{}", &config.url, endpoint);
let client = reqwest::blocking::Client::new(); let client = reqwest::blocking::Client::new();
let res = client.get(endpoint) let res = client.get(endpoint)
.header("Content-type", "application/json")
.header("Accept", "application/json")
.header("Authorization", &config.key) .header("Authorization", &config.key)
.send(); .send();
res
match res {
Ok(res) => return res.text().unwrap(),
Err(e) => panic!("Problem with endpoint: {:?}", e),
};
} }
pub fn get_projects(config_file: &PathBuf) { pub fn get_projects(config_file: &PathBuf) {