Skip to content

Commit

Permalink
refactor: remove unneeded calls to warp::Filter::boxed
Browse files Browse the repository at this point in the history
Signed-off-by: ljedrz <ljedrz@gmail.com>
  • Loading branch information
ljedrz committed May 14, 2020
1 parent 8d43627 commit 3309597
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 57 deletions.
52 changes: 18 additions & 34 deletions crates/interledger-api/src/routes/accounts.rs
Expand Up @@ -76,13 +76,13 @@ where
+ 'static,
{
// TODO can we make any of the Filters const or put them in once_cell?
let with_store = warp::any().map(move || store.clone()).boxed();
let with_incoming_handler = warp::any().map(move || incoming_handler.clone()).boxed();
let with_store = warp::any().map(move || store.clone());
let with_incoming_handler = warp::any().map(move || incoming_handler.clone());

// Helper filters
let admin_auth_header = format!("Bearer {}", admin_api_token);
let admin_auth_header_clone = admin_auth_header.clone();
let with_admin_auth_header = warp::any().map(move || admin_auth_header.clone()).boxed();
let with_admin_auth_header = warp::any().map(move || admin_auth_header.clone());
let admin_only = warp::header::<SecretString>("authorization")
.and_then(move |authorization: SecretString| {
let admin_auth_header = admin_auth_header_clone.clone();
Expand All @@ -96,17 +96,15 @@ where
})
// This call makes it so we do not pass on a () value on
// success to the next filter, it just gets rid of it
.untuple_one()
.boxed();
.untuple_one();

// Converts an account username to an account id or errors out
let account_username_to_id = warp::path::param::<Username>()
.and(with_store.clone())
.and_then(move |username: Username, store: S| async move {
let id = store.get_account_id_from_username(&username).await?;
Ok::<_, Rejection>(id)
})
.boxed();
});

let is_authorized_user = move |store: S, path_username: Username, auth_string: SecretString| {
async move {
Expand Down Expand Up @@ -152,8 +150,7 @@ where
Ok::<Uuid, Rejection>(account.id())
}
},
)
.boxed();
);

// Checks if the account has provided a valid password (same as admin-or-auth call, minus one call, can we refactor them together?)
let authorized_user_only = warp::path::param::<Username>()
Expand All @@ -164,8 +161,7 @@ where
let account = is_authorized_user(store, path_username, auth_string).await?;
Ok::<A, Rejection>(account)
},
)
.boxed();
);

// POST /accounts
let btp_clone = btp.clone();
Expand All @@ -186,8 +182,7 @@ where
connect_to_external_services(handler, account.clone(), store_clone, btp).await?;
Ok::<Json, Rejection>(warp::reply::json(&account))
}
})
.boxed();
});

// GET /accounts
let get_accounts = warp::get()
Expand All @@ -198,8 +193,7 @@ where
.and_then(|store: S| async move {
let accounts = store.get_all_accounts().await?;
Ok::<Json, Rejection>(warp::reply::json(&accounts))
})
.boxed();
});

// PUT /accounts/:username
let btp_clone = btp.clone();
Expand Down Expand Up @@ -227,8 +221,7 @@ where

Ok::<Json, Rejection>(warp::reply::json(&account))
}
})
.boxed();
});

// GET /accounts/:username
let get_account = warp::get()
Expand All @@ -241,8 +234,7 @@ where
let accounts = store.get_accounts(vec![id]).await?;

Ok::<Json, Rejection>(warp::reply::json(&accounts[0]))
})
.boxed();
});

// GET /accounts/:username/balance
let get_account_balance = warp::get()
Expand All @@ -268,8 +260,7 @@ where
"asset_code": asset_code,
})))
}
})
.boxed();
});

// DELETE /accounts/:username
let btp_clone = btp.clone();
Expand All @@ -287,8 +278,7 @@ where
btp.close_connection(&id);
Ok::<Json, Rejection>(warp::reply::json(&account))
}
})
.boxed();
});

// PUT /accounts/:username/settings
let outgoing_handler_clone = outgoing_handler;
Expand Down Expand Up @@ -322,8 +312,7 @@ where
.await?;
Ok::<Json, Rejection>(warp::reply::json(&modified_account))
}
})
.boxed();
});

// (Websocket) /accounts/:username/payments/incoming
let incoming_payment_notifications = warp::path("accounts")
Expand All @@ -337,8 +326,7 @@ where
ws.on_upgrade(move |ws: warp::ws::WebSocket| {
notify_user(ws, id, store).map(|result| result.unwrap())
})
})
.boxed();
});

// POST /accounts/:username/payments
let post_payments = warp::post()
Expand Down Expand Up @@ -372,8 +360,7 @@ where
Ok::<Json, Rejection>(warp::reply::json(&json!(receipt)))
}
},
)
.boxed();
);

// GET /accounts/:username/spsp
let server_secret_clone = server_secret.clone();
Expand All @@ -396,8 +383,7 @@ where
.generate_http_response(),
)
}
})
.boxed();
});

// GET /.well-known/pay
// This is the endpoint a [Payment Pointer](https://github.com/interledger/rfcs/blob/master/0026-payment-pointers/0026-payment-pointers.md)
Expand Down Expand Up @@ -433,8 +419,7 @@ where
))
}
}
})
.boxed();
});

get_spsp
.or(get_spsp_well_known)
Expand All @@ -447,7 +432,6 @@ where
.or(put_account_settings)
.or(incoming_payment_notifications)
.or(post_payments)
.boxed()
}

fn notify_user(
Expand Down
27 changes: 9 additions & 18 deletions crates/interledger-api/src/routes/node_settings.rs
Expand Up @@ -59,9 +59,8 @@ where
})
// This call makes it so we do not pass on a () value on
// success to the next filter, it just gets rid of it
.untuple_one()
.boxed();
let with_store = warp::any().map(move || store.clone()).boxed();
.untuple_one();
let with_store = warp::any().map(move || store.clone());

// GET /
let get_root = warp::get()
Expand All @@ -73,8 +72,7 @@ where
ilp_address: store.get_ilp_address(),
version: node_version.clone(),
})
})
.boxed();
});

// PUT /rates
let put_rates = warp::put()
Expand All @@ -86,8 +84,7 @@ where
.and_then(|rates: ExchangeRates, store: S| async move {
store.set_exchange_rates(rates.0.clone())?;
Ok::<_, Rejection>(warp::reply::json(&rates))
})
.boxed();
});

// GET /rates
let get_rates = warp::get()
Expand All @@ -97,8 +94,7 @@ where
.and_then(|store: S| async move {
let rates = store.get_all_exchange_rates()?;
Ok::<_, Rejection>(warp::reply::json(&rates))
})
.boxed();
});

// GET /routes
// Response: Map of ILP Address prefix -> Username
Expand All @@ -123,8 +119,7 @@ where

Ok::<Json, Rejection>(warp::reply::json(&routes))
}
})
.boxed();
});

// PUT /routes/static
// Body: Map of ILP Address prefix -> Username
Expand Down Expand Up @@ -158,8 +153,7 @@ where
.await?;
Ok::<Json, Rejection>(warp::reply::json(&routes))
}
})
.boxed();
});

// PUT /routes/static/:prefix
// Body: Username
Expand All @@ -182,8 +176,7 @@ where
store.set_static_route(prefix, account_id).await?;
Ok::<String, Rejection>(username.to_string())
}
})
.boxed();
});

// PUT /settlement/engines
let put_settlement_engines = warp::put()
Expand Down Expand Up @@ -226,8 +219,7 @@ where
}
}
Ok::<Json, Rejection>(warp::reply::json(&asset_to_url_map_clone))
})
.boxed();
});

get_root
.or(put_rates)
Expand All @@ -236,7 +228,6 @@ where
.or(put_static_routes)
.or(put_static_route)
.or(put_settlement_engines)
.boxed()
}

#[cfg(test)]
Expand Down
2 changes: 1 addition & 1 deletion crates/interledger-http/src/server.rs
Expand Up @@ -115,7 +115,7 @@ where
) -> impl warp::Filter<Extract = (impl warp::Reply,), Error = warp::Rejection> + Clone {
let store = self.store.clone();
let incoming = self.incoming.clone();
let with_store = warp::any().map(move || store.clone()).boxed();
let with_store = warp::any().map(move || store.clone());
let with_incoming = warp::any().map(move || incoming.clone());
warp::post()
.and(warp::path("accounts"))
Expand Down
4 changes: 2 additions & 2 deletions crates/interledger-settlement/src/api/node_api.rs
Expand Up @@ -130,7 +130,7 @@ where
O: OutgoingService<A> + Clone + Send + Sync + 'static,
A: SettlementAccount + Account + Send + Sync + 'static,
{
let with_store = warp::any().map(move || store.clone()).boxed();
let with_store = warp::any().map(move || store.clone());
let idempotency = warp::header::optional::<String>("idempotency-key");
let account_id_filter = warp::path("accounts").and(warp::path::param::<String>()); // account_id

Expand All @@ -147,7 +147,7 @@ where

// POST /accounts/:account_id/messages (optional idempotency-key header)
// Body is a Vec<u8> object
let with_outgoing_handler = warp::any().map(move || outgoing_handler.clone()).boxed();
let with_outgoing_handler = warp::any().map(move || outgoing_handler.clone());
let messages_endpoint = account_id_filter.and(warp::path("messages"));
let messages = warp::post()
.and(messages_endpoint)
Expand Down
4 changes: 2 additions & 2 deletions crates/interledger-settlement/src/core/engines_api.rs
Expand Up @@ -154,8 +154,8 @@ where
E: SettlementEngine + Clone + Send + Sync + 'static,
S: IdempotentStore + Clone + Send + Sync + 'static,
{
let with_store = warp::any().map(move || store.clone()).boxed();
let with_engine = warp::any().map(move || engine.clone()).boxed();
let with_store = warp::any().map(move || store.clone());
let with_engine = warp::any().map(move || engine.clone());
let idempotency = warp::header::optional::<String>("idempotency-key");
let account_id = warp::path("accounts").and(warp::path::param::<String>()); // account_id

Expand Down

0 comments on commit 3309597

Please sign in to comment.