poem-web / poem

A full-featured and easy-to-use web framework with the Rust programming language.
Apache License 2.0
3.47k stars 282 forks source link

Are the following operations supported? #485

Open LuckyQinLin opened 1 year ago

LuckyQinLin commented 1 year ago

http://127.0.0.1:3000/api?id=6

How does this URL get the request parameters?

senden9 commented 1 year ago

This is a query parameter in poem_openapi. Documentation: https://docs.rs/poem-openapi/latest/poem_openapi/param/struct.Query.html Example:

use poem_openapi::param::Path;
use poem_openapi::param::Query;
use poem_openapi::payload::Json;
use poem_openapi::ApiResponse;
use poem_openapi::Enum;
use poem_openapi::Object;
use poem_openapi::OpenApi;

#[OpenApi]
impl ProductApi {
    /// Documentation comment
    ///
    /// Document stuff here ...
    #[oai(path = "/products", method = "get")]
    pub(crate) async fn get_products(
        &self,
        offset: Query<Option<u32>>,
        limit: Query<Option<u32>>,
        name: Query<Option<String>>,
        state_filter: Query<Option<ProductState>>,
    ) -> poem::Result<GetProductResponses> {
        let offset = offset.0.unwrap_or_default();
        let limit = limit.0.unwrap_or(10);
        let name = name.0.unwrap_or_default();
        let state_filter = state_filter.0.unwrap_or_default();
        todo!("Response etc...")
    }
}

/// Enum to filter products by theyr synced-state.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Enum)]
#[oai(rename_all = "camelCase")]
pub(crate) enum ProductState {
    /// Does not filter by product state.
    DoNotCare,
    /// Get only synced products.
    Synced,
    /// Get only unsynced products.
    Unsynced,
}

Note that this is not a minimal example. I just slapped all inports from my real source-file in here.