allow sending access key

This commit is contained in:
Robin Appelman 2025-05-03 16:01:12 +02:00
commit 35db814441
2 changed files with 36 additions and 23 deletions

30
flake.lock generated
View file

@ -2,11 +2,11 @@
"nodes": {
"crane": {
"locked": {
"lastModified": 1733688869,
"narHash": "sha256-KrhxxFj1CjESDrL5+u/zsVH0K+Ik9tvoac/oFPoxSB8=",
"lastModified": 1742394900,
"narHash": "sha256-vVOAp9ahvnU+fQoKd4SEXB2JG2wbENkpqcwlkIXgUC0=",
"owner": "ipetkov",
"repo": "crane",
"rev": "604637106e420ad99907cae401e13ab6b452e7d9",
"rev": "70947c1908108c0c551ddfd73d4f750ff2ea67cd",
"type": "github"
},
"original": {
@ -22,11 +22,11 @@
]
},
"locked": {
"lastModified": 1734957624,
"narHash": "sha256-RbvX9lf9lWQwG9vTXkscOiWTrKf8lzjyeOvW/v8IuBY=",
"lastModified": 1745844883,
"narHash": "sha256-4t9eR291RIgYo29MvRQ04QgmjZNad3e5sBr40tLhLTM=",
"owner": "nix-community",
"repo": "flakelight",
"rev": "8c226ea0166784b02d4a58fbb015f9c01250221e",
"rev": "78ad7409722c554effa360208417244c54ef398f",
"type": "github"
},
"original": {
@ -44,11 +44,11 @@
"rust-overlay": "rust-overlay"
},
"locked": {
"lastModified": 1735052218,
"narHash": "sha256-I30wh6G8fSUO4EseexxiDXcxyUhXR6C8BvEeKn6xyfE=",
"lastModified": 1745623947,
"narHash": "sha256-7Jt9a6YjBeRPso1VaRsH6ZnqNzt+MlcmVM7MNBSLVEI=",
"owner": "icewind1991",
"repo": "mill-scale",
"rev": "7e45bb598ff63a8416ee3c26743b20644563bd93",
"rev": "45597aa1134a3ea397c4441e36ac1d836a5de000",
"type": "github"
},
"original": {
@ -59,11 +59,11 @@
},
"nixpkgs": {
"locked": {
"lastModified": 1734875076,
"narHash": "sha256-Pzyb+YNG5u3zP79zoi8HXYMs15Q5dfjDgwCdUI5B0nY=",
"lastModified": 1746183838,
"narHash": "sha256-kwaaguGkAqTZ1oK0yXeQ3ayYjs8u/W7eEfrFpFfIDFA=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "1807c2b91223227ad5599d7067a61665c52d1295",
"rev": "bf3287dac860542719fe7554e21e686108716879",
"type": "github"
},
"original": {
@ -88,11 +88,11 @@
]
},
"locked": {
"lastModified": 1733884434,
"narHash": "sha256-8GXR9kC07dyOIshAyfZhG11xfvBRSZzYghnZ2weOKJU=",
"lastModified": 1742697269,
"narHash": "sha256-Lpp0XyAtIl1oGJzNmTiTGLhTkcUjwSkEb0gOiNzYFGM=",
"owner": "oxalica",
"repo": "rust-overlay",
"rev": "d0483df44ddf0fd1985f564abccbe568e020ddf2",
"rev": "01973c84732f9275c50c5f075dd1f54cc04b3316",
"type": "github"
},
"original": {

View file

@ -31,6 +31,7 @@ pub struct ApiClient {
base_timeout: Duration,
client: Client,
base_url: Url,
access_key: Option<String>,
}
impl Default for ApiClient {
@ -84,9 +85,15 @@ impl ApiClient {
base_timeout: timeout,
client: Client::builder().timeout(timeout).build()?,
base_url,
access_key: None,
})
}
/// Set access key used to access private demos
pub fn set_access_key(&mut self, access_key: String) {
self.access_key = Some(access_key);
}
fn url<P: AsRef<str>>(&self, path: P) -> Result<Url, Error> {
self.base_url
.join(path.as_ref())
@ -179,9 +186,13 @@ impl ApiClient {
return Err(Error::InvalidPage);
}
Ok(self
.client
.get(url)
let mut req = self.client.get(url);
if let Some(access_key) = &self.access_key {
req = req.header("ACCESS_KEY", access_key.as_str());
}
Ok(req
.query(&[("page", page)])
.query(&params)
.send()
@ -215,11 +226,13 @@ impl ApiClient {
/// ```
#[instrument]
pub async fn get(&self, demo_id: u32) -> Result<Demo, Error> {
let response = self
.client
.get(self.url(format!("/demos/{}", demo_id))?)
.send()
.await?;
let mut req = self.client.get(self.url(format!("/demos/{}", demo_id))?);
if let Some(access_key) = &self.access_key {
req = req.header("ACCESS-KEY", access_key.as_str());
}
let response = req.send().await?;
if response.status() == StatusCode::NOT_FOUND {
return Err(Error::DemoNotFound(demo_id));