chia_node/
harvester.rs

1use std::path::Path;
2
3use reqwest::ClientBuilder;
4use reqwest::Response;
5
6use crate::Error;
7use crate::util::load_pem_pair;
8
9mod model;
10pub use model::Plots;
11use model::GetPlotsResponse;
12
13pub struct Client {
14	host: String,
15	port: u16,
16	http: reqwest::Client
17}
18
19impl Client {
20	pub async fn new(host: &str, port: u16, key_file: impl AsRef<Path>, cert_file: impl AsRef<Path>) -> Result<Self, Error> {
21		let identity = load_pem_pair(key_file, cert_file).await?;
22		let http = ClientBuilder::new()
23			.danger_accept_invalid_certs(true)
24			//.danger_accept_invalid_hostnames(true)
25			.identity(identity)
26			.build()?;
27		Ok(Self{
28			host: host.to_string(),
29			port: port,
30			http: http
31		})
32	}
33
34	pub async fn get_plots(&self) -> Result<Plots, Error> {
35		let response = self.cmd("get_plots").await?;
36		let response: GetPlotsResponse = response.json().await?;
37		Ok(response.into())
38	}
39
40	async fn cmd(&self, command: &str) -> Result<Response, reqwest::Error> {
41		let url = self.make_url(command);
42		self.http.post(&url).header("Content-Type", "application/json").body("{}").send().await
43	}
44
45	fn make_url(&self, command: &str) -> String {
46		format!("https://{}:{}/{}", &self.host, self.port, &command)
47	}
48}
49