Repository: juspay/hyperswitch
File: crates/router/src/routes/subscription.rs (Rust)

Code before:
    state: web::Data<AppState>,
    req: HttpRequest,
    query: web::Query<subscription_types::GetPlansQuery>,
) -> impl Responder {
    let flow = Flow::GetPlansForSubscription;
    let api_auth = auth::ApiKeyAuth::default();

    let profile_id = match extract_profile_id(&req) {
        Ok(profile_id) => profile_id,
        _ => {
            return HttpResponse::BadRequest().json(
                errors::api_error_response::ApiErrorResponse::InvalidDataValue {
                    field_name: X_PROFILE_ID,
                },
            )
        }
    };

    let auth_data = match auth::is_ephemeral_auth(req.headers(), api_auth) {
        Ok(auth) => auth,
        Err(err) => return crate::services::api::log_and_return_error_response(err),
    };

    Box::pin(oss_api::server_wrap(
        flow,
        state,
        &req,
        query.into_inner(),
        |state, auth: auth::AuthenticationData, query, _| {
            let merchant_context = domain::MerchantContext::NormalMerchant(Box::new(
                domain::Context(auth.merchant_account, auth.key_store),
            ));
            subscription::get_subscription_plans(state, merchant_context, profile_id.clone(), query)
        },
        &*auth_data,
        api_locking::LockAction::NotApplicable,
    ))
    .await
}

fn extract_profile_id(req: &HttpRequest) -> Result<common_utils::id_type::ProfileId, HttpResponse> {
    let header_value = req.headers().get(X_PROFILE_ID).ok_or_else(|| {
        HttpResponse::BadRequest().json(
            errors::api_error_response::ApiErrorResponse::MissingRequiredField {
                field_name: X_PROFILE_ID,
            },
        )
    })?;
    let profile_str = header_value.to_str().unwrap_or_default();
    if profile_str.is_empty() {
        return Err(HttpResponse::BadRequest().json(

Review comment (refactor):
```suggestion
    let profile_id = match extract_profile_id(&req) {
        Ok(profile_id) => profile_id,
        Err(response) => return response,
```

Code after:
                is_connected_allowed: false,
                is_platform_allowed: false,
            }),
            &auth::JWTAuth {
                permission: Permission::ProfileSubscriptionWrite,
            },
            req.headers(),
        ),
        api_locking::LockAction::NotApplicable,
    ))
    .await
}

#[instrument(skip_all)]
pub async fn get_subscription_plans(
    state: web::Data<AppState>,
    req: HttpRequest,
    query: web::Query<subscription_types::GetPlansQuery>,
) -> impl Responder {
    let flow = Flow::GetPlansForSubscription;
    let api_auth = auth::ApiKeyAuth::default();

    let profile_id = match extract_profile_id(&req) {
        Ok(profile_id) => profile_id,
        Err(response) => return response,
    };

    let auth_data = match auth::is_ephemeral_auth(req.headers(), api_auth) {
        Ok(auth) => auth,
        Err(err) => return crate::services::api::log_and_return_error_response(err),
    };
    Box::pin(oss_api::server_wrap(
        flow,
        state,
        &req,
        query.into_inner(),
        |state, auth: auth::AuthenticationData, query, _| {
            let merchant_context = domain::MerchantContext::NormalMerchant(Box::new(
                domain::Context(auth.merchant_account, auth.key_store),
            ));
            subscription::get_subscription_plans(state, merchant_context, profile_id.clone(), query)
        },
        &*auth_data,
        api_locking::LockAction::NotApplicable,
    ))
    .await
}

/// Add support for get subscription by id
#[instrument(skip_all)]
pub async fn get_subscription(