forked from jmug/stoatchat
feat: initial work on tenor gif searching
This commit is contained in:
committed by
Angelo Kontaxis
parent
bfe4018e43
commit
b0c977b324
64
crates/services/gifbox/src/api.rs
Normal file
64
crates/services/gifbox/src/api.rs
Normal file
@@ -0,0 +1,64 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use axum::{extract::{Query, State}, routing::get, Json, Router};
|
||||
use revolt_result::{create_error, Result};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use utoipa::ToSchema;
|
||||
|
||||
use crate::tenor::{Tenor, types};
|
||||
|
||||
pub async fn router() -> Router<Tenor> {
|
||||
Router::new()
|
||||
.route("/", get(root))
|
||||
.route("/search", get(search))
|
||||
}
|
||||
|
||||
/// Successful root response
|
||||
#[derive(Serialize, Debug, ToSchema)]
|
||||
pub struct RootResponse {
|
||||
message: &'static str,
|
||||
version: &'static str,
|
||||
}
|
||||
|
||||
/// Capture crate version from Cargo
|
||||
static CRATE_VERSION: &str = env!("CARGO_PKG_VERSION");
|
||||
|
||||
/// Root response from service
|
||||
#[utoipa::path(
|
||||
get,
|
||||
path = "/",
|
||||
responses(
|
||||
(status = 200, description = "Echo response", body = RootResponse)
|
||||
)
|
||||
)]
|
||||
async fn root() -> Json<RootResponse> {
|
||||
Json(RootResponse {
|
||||
message: "Gifbox lives on!",
|
||||
version: CRATE_VERSION,
|
||||
})
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct SearchQueryParams {
|
||||
pub query: String,
|
||||
pub locale: String,
|
||||
pub position: Option<String>
|
||||
}
|
||||
|
||||
#[utoipa::path(
|
||||
get,
|
||||
path = "/search",
|
||||
responses(
|
||||
(status = 200, description = "Search results", body = SearchResponse)
|
||||
)
|
||||
)]
|
||||
async fn search(
|
||||
Query(params): Query<SearchQueryParams>,
|
||||
State(tenor): State<Tenor>,
|
||||
) -> Result<Json<Arc<types::SearchResponse>>> {
|
||||
// Todo
|
||||
tenor.search(¶ms.query, ¶ms.locale, params.position.as_deref())
|
||||
.await
|
||||
.map_err(|_| create_error!(InternalError))
|
||||
.map(Json)
|
||||
}
|
||||
Reference in New Issue
Block a user