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

main
Óscar M. Lage 2021-11-15 18:37:53 +01:00
parent b12ce8dddc
commit 43f668a059
1 changed files with 36 additions and 6 deletions

View File

@ -1,9 +1,41 @@
use std::path::PathBuf;
use serde::{Serialize, Deserialize};
#[path = "./config.rs"]
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
let mut default_config = config::Config::default();
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 client = reqwest::blocking::Client::new();
let res = client.get(endpoint)
.header("Content-type", "application/json")
.header("Accept", "application/json")
.header("Authorization", &config.key)
.send();
match res {
Ok(res) => return res.text().unwrap(),
Err(e) => panic!("Problem with endpoint: {:?}", e),
};
res
}
pub fn get_projects(config_file: &PathBuf) {