threema_client/
directory_api.rs1use crate::{ThreemaID, naclbox};
2use reqwest;
3use serde_json;
4use anyhow::{self,Context};
5use base64;
6
7#[derive(Debug, Clone)]
8pub struct Client{
9 pub server: String,
10 pub http: reqwest::Client,
11}
12
13impl Default for Client{
14 fn default() -> Self {
15 let mut headers = reqwest::header::HeaderMap::new();
16 headers.insert("Accept", reqwest::header::HeaderValue::from_static("application/json"));
17 let http = reqwest::Client::builder()
18 .default_headers(headers)
19 .user_agent("Threema ist cool")
20 .build().expect("Failed to build Directory API HTTP Client");
21 Client{server: "https://ds-apip.threema.ch/".to_string(), http}
22 }
23}
24
25impl Client{
26 pub async fn get_pubkey(&self, id: &ThreemaID) -> anyhow::Result<naclbox::PublicKey> {
27 let response : serde_json::Value = self.http
28 .get(format!("{}/identity/{}", self.server, id)).send().await?
29 .json().await?;
30 let kb64 = response.get("publicKey").context("response is missing publicKey")?.as_str().context("publicKey must be a string")?;
31 let buf = base64::decode(kb64)?;
32
33 naclbox::PublicKey::from_slice(&buf).context("pubkey has wrong length")
34 }
35}