1use crate::Client;
2use crate::Message;
3use crate::Result;
4use std::time::Duration;
5
6#[derive(Clone, Debug)]
7pub struct HttpClient {
8 client: reqwest::Client,
9 host: String,
10}
11
12impl Default for HttpClient {
13 fn default() -> Self {
14 HttpClient {
15 client: reqwest::Client::builder()
16 .connect_timeout(Duration::new(10, 0))
17 .build()
18 .unwrap(),
19 host: "https://api.june.so".to_owned(),
20 }
21 }
22}
23
24impl HttpClient {
25 pub fn new(client: reqwest::Client, host: String) -> HttpClient {
26 HttpClient { client, host }
27 }
28}
29
30#[async_trait::async_trait]
31impl Client for HttpClient {
32 async fn send(&self, write_key: String, msg: Message) -> Result<()> {
33 let path = match msg {
34 Message::Identify(_) => "/sdk/identify",
35 Message::Track(_) => "/sdk/track",
36 Message::Page(_) => "/sdk/page",
37 Message::Screen(_) => "/sdk/screen",
38 Message::Group(_) => "/sdk/group",
39 Message::Alias(_) => "/sdk/alias",
40 Message::Batch(_) => "/sdk/batch",
41 };
42
43 let _ = self
44 .client
45 .post(&format!("{}{}", self.host, path))
46 .header("Authorization", format!("Basic {}", write_key))
47 .header("Content-Type", "application/json")
48 .json(&msg)
49 .send()
50 .await?
51 .error_for_status()?;
52
53 Ok(())
54 }
55}