Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ thiserror = { version = "2.0" }
tokio = "1.44.1"
toml = "0.8.20"
unindent = "0.2.4"
urlencoding = "2.1.3"

# Workspace members
atrium-core = { path = "atrium-core" }
Expand Down
1 change: 1 addition & 0 deletions atrium-client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ serde = { workspace = true }
thiserror = { workspace = true }
tokio = { workspace = true }
toml = { workspace = true }
urlencoding = { workspace = true }

[dev-dependencies]
insta = { workspace = true }
Expand Down
20 changes: 9 additions & 11 deletions atrium-client/src/client.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use base64::Engine;
use base64::prelude::BASE64_STANDARD;
use error_stack::Result;
use error_stack::ResultExt;
use error_stack::bail;
Expand All @@ -22,24 +20,24 @@ pub struct Client {
}

impl Client {
pub async fn get(&self, key: &[u8]) -> Result<Option<Vec<u8>>, Error> {
pub async fn get(&self, key: &str) -> Result<Option<Vec<u8>>, Error> {
do_get(self, key).await
}

pub async fn put(&self, key: &[u8], value: &[u8]) -> Result<(), Error> {
pub async fn put(&self, key: &str, value: &[u8]) -> Result<(), Error> {
do_put(self, key, value).await
}

pub async fn delete(&self, key: &[u8]) -> Result<(), Error> {
pub async fn delete(&self, key: &str) -> Result<(), Error> {
do_delete(self, key).await
}
}

async fn do_get(client: &Client, key: &[u8]) -> Result<Option<Vec<u8>>, Error> {
async fn do_get(client: &Client, key: &str) -> Result<Option<Vec<u8>>, Error> {
let make_error = || Error::Other("failed to get".to_string());
let mut url = Url::parse(&client.endpoint).change_context_lazy(make_error)?;

let encoded_key = BASE64_STANDARD.encode(key);
let encoded_key = urlencoding::encode(key);

url = url.join(&encoded_key).change_context_lazy(make_error)?;

Expand All @@ -57,11 +55,11 @@ async fn do_get(client: &Client, key: &[u8]) -> Result<Option<Vec<u8>>, Error> {
}
}

async fn do_put(client: &Client, key: &[u8], value: &[u8]) -> Result<(), Error> {
async fn do_put(client: &Client, key: &str, value: &[u8]) -> Result<(), Error> {
let make_error = || Error::Other("failed to get".to_string());
let mut url = Url::parse(&client.endpoint).change_context_lazy(make_error)?;

let encoded_key = BASE64_STANDARD.encode(key);
let encoded_key = urlencoding::encode(key);

url = url.join(&encoded_key).change_context_lazy(make_error)?;

Expand All @@ -79,11 +77,11 @@ async fn do_put(client: &Client, key: &[u8], value: &[u8]) -> Result<(), Error>
}
}

async fn do_delete(client: &Client, key: &[u8]) -> Result<(), Error> {
async fn do_delete(client: &Client, key: &str) -> Result<(), Error> {
let make_error = || Error::Other("failed to get".to_string());
let mut url = Url::parse(&client.endpoint).change_context_lazy(make_error)?;

let encoded_key = BASE64_STANDARD.encode(key);
let encoded_key = urlencoding::encode(key);

url = url.join(&encoded_key).change_context_lazy(make_error)?;

Expand Down
1 change: 1 addition & 0 deletions atrium-server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ serde = { workspace = true }
thiserror = { workspace = true }
tokio = { workspace = true }
toml = { workspace = true }
urlencoding = { workspace = true }

[dev-dependencies]
insta = { workspace = true }
Expand Down
14 changes: 6 additions & 8 deletions atrium-server/src/server.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use std::sync::Arc;

use atrium_core::Config;
use base64::Engine;
use base64::prelude::BASE64_STANDARD;
use poem::Body;
use poem::Endpoint;
use poem::EndpointExt;
Expand Down Expand Up @@ -87,12 +85,12 @@ struct GetParams {

#[handler]
pub async fn get(Data(ctx): Data<&Arc<Context>>, Query(params): Query<GetParams>) -> Response {
let Ok(key) = BASE64_STANDARD.decode(params.key) else {
let Ok(key) = urlencoding::decode(&params.key) else {
return Response::builder()
.status(StatusCode::BAD_REQUEST)
.body("Bad request");
};
let value = ctx.engine.get(&key).await;
let value = ctx.engine.get(key.as_bytes()).await;

match value {
Some(value) => Response::builder()
Expand All @@ -116,15 +114,15 @@ pub async fn put(
Query(params): Query<PutParams>,
body: Body,
) -> Response {
let Ok(key) = BASE64_STANDARD.decode(params.key) else {
let Ok(key) = urlencoding::decode(&params.key) else {
return Response::builder()
.status(StatusCode::BAD_REQUEST)
.body("Bad request");
};
let put_result = body
.into_bytes()
.await
.map(|bytes| ctx.engine.put(&key, &bytes));
.map(|bytes| ctx.engine.put(key.as_bytes(), &bytes));

match put_result {
Ok(_) => Response::builder()
Expand All @@ -146,11 +144,11 @@ pub async fn delete(
Data(ctx): Data<&Arc<Context>>,
Query(params): Query<DeleteParams>,
) -> Response {
let Ok(key) = BASE64_STANDARD.decode(params.key) else {
let Ok(key) = urlencoding::decode(&params.key) else {
return Response::builder()
.status(StatusCode::BAD_REQUEST)
.body("Bad request");
};
ctx.engine.delete(&key);
ctx.engine.delete(key.as_bytes());
Response::builder().status(StatusCode::NO_CONTENT).body("")
}
Loading