Skip to main content

epochal_api/time_portal/
mod.rs

1//! `/v1/time-portal`: the web, and Linux sandboxes, held to a past instant. Every call is a method
2//! of [`TimePortal`], from [`Client::time_portal`].
3
4use chrono::{DateTime, Utc};
5use reqwest::{Method, RequestBuilder};
6
7use crate::client::{Client, with_as_of};
8
9pub mod entities;
10pub mod sandbox;
11pub mod web;
12
13const PREFIX: &str = "/v1/time-portal";
14
15/// The time portal held to one instant, from [`Client::time_portal`]: search, fetch and new boxes
16/// are held to [`as_of`](Self::as_of). Calls on an existing box take its id, and the box keeps the
17/// instant it was made with. Cheap to clone; it shares the client's connection pool.
18#[derive(Clone, Debug)]
19pub struct TimePortal {
20    client: Client,
21    as_of: DateTime<Utc>,
22}
23
24impl Client {
25    /// The time portal held to `as_of`: web search, web fetch and sandboxes as they stood then.
26    pub fn time_portal(&self, as_of: DateTime<Utc>) -> TimePortal {
27        TimePortal {
28            client: self.clone(),
29            as_of,
30        }
31    }
32}
33
34impl TimePortal {
35    /// The client its calls go through.
36    pub fn client(&self) -> &Client {
37        &self.client
38    }
39
40    /// The instant search, fetch and new boxes are held to.
41    pub fn as_of(&self) -> DateTime<Utc> {
42        self.as_of
43    }
44
45    fn request(&self, method: Method, path: &str) -> RequestBuilder {
46        self.client.request(method, &format!("{PREFIX}{path}"))
47    }
48
49    /// A request held to [`as_of`](Self::as_of) by `Accept-Datetime`.
50    fn held(&self, method: Method, path: &str) -> RequestBuilder {
51        with_as_of(self.request(method, path), self.as_of)
52    }
53}