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

View file

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