remove unnecessary enum and try_into

This commit is contained in:
Bryan Stitt 2022-08-21 09:47:38 +00:00
parent bda666eb6c
commit 94bc6fef8c
4 changed files with 13 additions and 10 deletions

View File

@ -15,7 +15,7 @@ pub async fn public_proxy_web3_rpc(
Extension(app): Extension<Arc<Web3ProxyApp>>,
ClientIp(ip): ClientIp,
) -> FrontendResult {
let _ip: IpAddr = rate_limit_by_ip(&app, ip).await?.try_into()?;
let _ip: IpAddr = rate_limit_by_ip(&app, ip).await?;
let protocol = Protocol::HTTP;
let user_id = 0;
@ -67,7 +67,7 @@ pub async fn user_proxy_web3_rpc(
Extension(app): Extension<Arc<Web3ProxyApp>>,
Path(user_key): Path<Uuid>,
) -> FrontendResult {
let user_id: u64 = rate_limit_by_user_key(&app, user_key).await?.try_into()?;
let user_id: u64 = rate_limit_by_user_key(&app, user_key).await?;
let protocol = Protocol::HTTP;

View File

@ -52,11 +52,14 @@ impl TryFrom<RequestFrom> for u64 {
}
}
pub async fn rate_limit_by_ip(app: &Web3ProxyApp, ip: IpAddr) -> RateLimitFrontendResult {
pub async fn rate_limit_by_ip(
app: &Web3ProxyApp,
ip: IpAddr,
) -> Result<IpAddr, FrontendErrorResponse> {
let rate_limit_result = app.rate_limit_by_ip(ip).await?;
match rate_limit_result {
RateLimitResult::AllowedIp(x) => Ok(x.into()),
RateLimitResult::AllowedIp(x) => Ok(x),
RateLimitResult::AllowedUser(_) => panic!("only ips or errors are expected here"),
rate_limit_result => {
let _: RequestFrom = rate_limit_result.try_into()?;
@ -70,12 +73,12 @@ pub async fn rate_limit_by_user_key(
app: &Web3ProxyApp,
// TODO: change this to a Ulid
user_key: Uuid,
) -> RateLimitFrontendResult {
) -> Result<u64, FrontendErrorResponse> {
let rate_limit_result = app.rate_limit_by_key(user_key).await?;
match rate_limit_result {
RateLimitResult::AllowedIp(_) => panic!("only user keys or errors are expected here"),
RateLimitResult::AllowedUser(x) => Ok(x.into()),
RateLimitResult::AllowedUser(x) => Ok(x),
rate_limit_result => {
let _: RequestFrom = rate_limit_result.try_into()?;

View File

@ -38,7 +38,7 @@ pub async fn get_login(
// TODO: allow ENS names here?
Path(mut params): Path<HashMap<String, String>>,
) -> FrontendResult {
let _ip: IpAddr = rate_limit_by_ip(&app, ip).await?.try_into()?;
let _ip: IpAddr = rate_limit_by_ip(&app, ip).await?;
// at first i thought about checking that user_address is in our db
// but theres no need to separate the registration and login flows
@ -134,7 +134,7 @@ pub async fn post_login(
Json(payload): Json<PostLogin>,
Query(query): Query<PostLoginQuery>,
) -> FrontendResult {
let _ip: IpAddr = rate_limit_by_ip(&app, ip).await?.try_into()?;
let _ip: IpAddr = rate_limit_by_ip(&app, ip).await?;
let mut new_user = true; // TODO: check the database

View File

@ -33,7 +33,7 @@ pub async fn public_websocket_handler(
ClientIp(ip): ClientIp,
ws_upgrade: Option<WebSocketUpgrade>,
) -> FrontendResult {
let _ip: IpAddr = rate_limit_by_ip(&app, ip).await?.try_into()?;
let _ip: IpAddr = rate_limit_by_ip(&app, ip).await?;
let user_id = 0;
let protocol = Protocol::Websocket;
@ -57,7 +57,7 @@ pub async fn user_websocket_handler(
Path(user_key): Path<Uuid>,
ws_upgrade: Option<WebSocketUpgrade>,
) -> FrontendResult {
let user_id: u64 = rate_limit_by_user_key(&app, user_key).await?.try_into()?;
let user_id: u64 = rate_limit_by_user_key(&app, user_key).await?;
let protocol = Protocol::Websocket;