Skip to content

Commit

Permalink
Remove device from url (#196)
Browse files Browse the repository at this point in the history
* remove device from url

* remove newline

* fmt

* remove log

* Updated README

* fixed reading of README

* fixed version

* updated lock file
  • Loading branch information
lassemand authored Aug 27, 2024
1 parent f3c03eb commit 770c40c
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 20 deletions.
2 changes: 1 addition & 1 deletion notification-server/Cargo.lock

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

2 changes: 1 addition & 1 deletion notification-server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
authors = ["Concordium AG [email protected]"]
edition = "2021"
name = "notification-server"
version = "0.1.10"
version = "0.1.11"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

Expand Down
5 changes: 3 additions & 2 deletions notification-server/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,11 @@ Should conflicts occur upon subscription updates, then only the preferences are
### Example:

```shell
curl -X PUT "http://localhost:3030/api/v1/device/<device_token>/subscription" \
curl -X PUT "http://localhost:3030/api/v1/subscription" \
-H "Content-Type: application/json" \
-d '{
"preferences": ["cis2-tx", "ccd-tx"],
"accounts": ["4FmiTW2L2AccyR9VjzsnpWFSAcohXWf7Vf797i36y526mqiEcp"]
"accounts": ["4FmiTW2L2AccyR9VjzsnpWFSAcohXWf7Vf797i36y526mqiEcp"],
"device_token": "<device_token>"
}'
```
27 changes: 14 additions & 13 deletions notification-server/src/bin/api.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use anyhow::Context;
use axum::{
extract::{Json, Path, State},
extract::{Json, State},
http::StatusCode,
response::IntoResponse,
routing::put,
Expand Down Expand Up @@ -113,7 +113,6 @@ lazy_static! {
/// - Device token validation failure.
/// - Database errors during subscription update.
async fn process_device_subscription(
device: String,
subscription: DeviceSubscription,
state: Arc<AppState>,
) -> Result<String, (StatusCode, String)> {
Expand All @@ -130,7 +129,6 @@ async fn process_device_subscription(
"Duplicate preferences found".to_string(),
));
}

if subscription.accounts.len() > MAX_RESOURCES_LENGTH {
return Err((
StatusCode::BAD_REQUEST,
Expand All @@ -142,8 +140,7 @@ async fn process_device_subscription(
.accounts
.iter()
.map(|account| {
AccountAddress::from_str(account).map_err(|e| {
error!("Failed to parse account address: {}", e);
AccountAddress::from_str(account).map_err(|_| {
(
StatusCode::BAD_REQUEST,
"Failed to parse account address".to_string(),
Expand All @@ -152,7 +149,11 @@ async fn process_device_subscription(
})
.collect();

if let Err(err) = state.google_cloud.validate_device_token(&device).await {
if let Err(err) = state
.google_cloud
.validate_device_token(&subscription.device_token)
.await
{
let (status, message) = match err {
NotificationError::InvalidArgumentError => {
(StatusCode::BAD_REQUEST, "Invalid device token".to_string())
Expand Down Expand Up @@ -181,7 +182,11 @@ async fn process_device_subscription(
state
.db_connection
.prepared
.upsert_subscription(decoded_accounts, subscription.preferences, &device)
.upsert_subscription(
decoded_accounts,
subscription.preferences,
&subscription.device_token,
)
.await
.map_err(|e| {
error!("Database error: {}", e);
Expand All @@ -195,13 +200,12 @@ async fn process_device_subscription(
}

async fn upsert_account_device(
Path(device): Path<String>,
State(state): State<Arc<AppState>>,
Json(subscription): Json<DeviceSubscription>,
) -> impl IntoResponse {
info!("Subscribing accounts {:?} to device a device", subscription);
let response: Result<String, (StatusCode, String)> =
process_device_subscription(device, subscription, state).await;
process_device_subscription(subscription, state).await;
match response {
Ok(message) => (StatusCode::OK, Json(json!({ "message": message }))),
Err((status_code, message)) => {
Expand Down Expand Up @@ -250,10 +254,7 @@ async fn main() -> anyhow::Result<()> {
});

let app = Router::new()
.route(
"/api/v1/device/:device/subscription",
put(upsert_account_device),
)
.route("/api/v1/subscription", put(upsert_account_device))
.with_state(app_state);
let listener = tokio::net::TcpListener::bind(args.listen_address).await?;
axum::serve(listener, app).await?;
Expand Down
8 changes: 5 additions & 3 deletions notification-server/src/models/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@ use std::collections::HashSet;

#[derive(Debug, Deserialize)]
pub struct DeviceSubscription {
pub preferences: Vec<Preference>,
pub accounts: Vec<String>,
pub preferences: Vec<Preference>,
pub accounts: Vec<String>,
pub device_token: String,
}

impl DeviceSubscription {
pub fn new(preferences: Vec<Preference>, accounts: Vec<String>) -> Self {
pub fn new(preferences: Vec<Preference>, accounts: Vec<String>, device_token: String) -> Self {
Self {
preferences,
accounts,
device_token,
}
}
}
Expand Down

0 comments on commit 770c40c

Please sign in to comment.