Skip to main content

atuin_common/
api.rs

1use semver::Version;
2use serde::{Deserialize, Serialize};
3use std::borrow::Cow;
4use std::sync::LazyLock;
5use time::OffsetDateTime;
6
7// the usage of X- has been deprecated for quite along time, it turns out
8pub static ATUIN_HEADER_VERSION: &str = "Atuin-Version";
9pub static ATUIN_CARGO_VERSION: &str = env!("CARGO_PKG_VERSION");
10
11pub static ATUIN_VERSION: LazyLock<Version> =
12    LazyLock::new(|| Version::parse(ATUIN_CARGO_VERSION).expect("failed to parse self semver"));
13
14#[derive(Debug, Serialize, Deserialize)]
15pub struct UserResponse {
16    pub username: String,
17}
18
19#[derive(Debug, Serialize, Deserialize)]
20pub struct RegisterRequest {
21    pub email: String,
22    pub username: String,
23    pub password: String,
24}
25
26#[derive(Debug, Serialize, Deserialize)]
27pub struct RegisterResponse {
28    pub session: String,
29    /// Auth type: "hub" for Hub API tokens, "cli" for legacy CLI session tokens.
30    /// Old servers that don't return this field will deserialize as None.
31    #[serde(default)]
32    pub auth: Option<String>,
33}
34
35#[derive(Debug, Serialize, Deserialize)]
36pub struct DeleteUserResponse {}
37
38#[derive(Debug, Serialize, Deserialize)]
39pub struct ChangePasswordRequest {
40    pub current_password: String,
41    pub new_password: String,
42}
43
44#[derive(Debug, Serialize, Deserialize)]
45pub struct ChangePasswordResponse {}
46
47#[derive(Debug, Serialize, Deserialize)]
48pub struct LoginRequest {
49    pub username: String,
50    pub password: String,
51}
52
53#[derive(Debug, Serialize, Deserialize)]
54pub struct LoginResponse {
55    pub session: String,
56    /// Auth type: "hub" for Hub API tokens, "cli" for legacy CLI session tokens.
57    /// Old servers that don't return this field will deserialize as None.
58    #[serde(default)]
59    pub auth: Option<String>,
60}
61
62#[derive(Debug, Serialize, Deserialize)]
63pub struct AddHistoryRequest {
64    pub id: String,
65    #[serde(with = "time::serde::rfc3339")]
66    pub timestamp: OffsetDateTime,
67    pub data: String,
68    pub hostname: String,
69}
70
71#[derive(Debug, Serialize, Deserialize)]
72pub struct CountResponse {
73    pub count: i64,
74}
75
76#[derive(Debug, Serialize, Deserialize)]
77pub struct SyncHistoryRequest {
78    #[serde(with = "time::serde::rfc3339")]
79    pub sync_ts: OffsetDateTime,
80    #[serde(with = "time::serde::rfc3339")]
81    pub history_ts: OffsetDateTime,
82    pub host: String,
83}
84
85#[derive(Debug, Serialize, Deserialize)]
86pub struct SyncHistoryResponse {
87    pub history: Vec<String>,
88}
89
90#[derive(Debug, Serialize, Deserialize)]
91pub struct ErrorResponse<'a> {
92    pub reason: Cow<'a, str>,
93}
94
95#[derive(Debug, Serialize, Deserialize)]
96pub struct IndexResponse {
97    pub homage: String,
98    pub version: String,
99}
100
101#[derive(Debug, Serialize, Deserialize)]
102pub struct StatusResponse {
103    pub count: i64,
104    pub username: String,
105    pub deleted: Vec<String>,
106
107    // These could/should also go on the index of the server
108    // However, we do not request the server index as a part of normal sync
109    // I'd rather slightly increase the size of this response, than add an extra HTTP request
110    pub page_size: i64, // max page size supported by the server
111    pub version: String,
112}
113
114#[derive(Debug, Serialize, Deserialize)]
115pub struct DeleteHistoryRequest {
116    pub client_id: String,
117}
118
119#[derive(Debug, Serialize, Deserialize)]
120pub struct MessageResponse {
121    pub message: String,
122}
123
124#[derive(Debug, Serialize, Deserialize)]
125pub struct MeResponse {
126    pub username: String,
127}
128
129// Hub CLI authentication types
130
131/// Response from POST /auth/cli/code - generates a code for CLI auth
132#[derive(Debug, Serialize, Deserialize)]
133pub struct CliCodeResponse {
134    pub code: String,
135}
136
137/// Response from GET /auth/cli/verify?code=<code> - polls for authorization
138#[derive(Debug, Serialize, Deserialize)]
139pub struct CliVerifyResponse {
140    /// Session token, present only when authorization is complete
141    pub token: Option<String>,
142    pub success: Option<bool>,
143    pub error: Option<String>,
144}