|
| 1 | +use std::convert::Infallible; |
| 2 | +use std::sync::Arc; |
| 3 | +use std::time::Duration; |
| 4 | + |
| 5 | +use axum::response::sse::Event; |
| 6 | +use axum::response::{IntoResponse, Sse}; |
| 7 | +use axum::{routing::get, Router}; |
| 8 | +use axum::{ |
| 9 | + extract::{Path, Query, State}, |
| 10 | + http::StatusCode, |
| 11 | + Json, |
| 12 | +}; |
| 13 | +use enstate_shared::core::lookup_data::LookupInfo; |
| 14 | +use enstate_shared::core::Profile; |
| 15 | +use ethers_core::types::Address; |
| 16 | +use futures::future::join_all; |
| 17 | +use serde::Deserialize; |
| 18 | +use tokio_stream::wrappers::UnboundedReceiverStream; |
| 19 | +use tracing::info; |
| 20 | + |
| 21 | +use crate::models::bulk::{BulkResponse, ListResponse}; |
| 22 | +use crate::models::sse::SSEResponse; |
| 23 | +use crate::routes::{ |
| 24 | + http_simple_status_error, profile_http_error_mapper, validate_bulk_input, FreshQuery, Qs, |
| 25 | + RouteError, |
| 26 | +}; |
| 27 | + |
| 28 | + |
| 29 | +pub fn setup_v2_router(state: Arc<crate::AppState>) -> Router<Arc<crate::AppState>> { |
| 30 | + Router::new() |
| 31 | + .route("/discover/search", get(discovery_search)).with_state(state) |
| 32 | +} |
| 33 | + |
| 34 | +#[derive(Deserialize)] |
| 35 | +pub struct SearchQuery { |
| 36 | + s: String, |
| 37 | +} |
| 38 | + |
| 39 | +/// /a/{address} |
| 40 | +/// |
| 41 | +/// Here is an example of a valid request that looks up an address: |
| 42 | +/// ```url |
| 43 | +/// /a/0x225f137127d9067788314bc7fcc1f36746a3c3B5 |
| 44 | +/// ``` |
| 45 | +#[utoipa::path( |
| 46 | + get, |
| 47 | + tag = "Single Profile", |
| 48 | + path = "/v2/discover/search", |
| 49 | + responses( |
| 50 | + (status = 200, description = "Successfully found address.", body = ENSProfile), |
| 51 | + (status = BAD_REQUEST, description = "Invalid address.", body = ErrorResponse), |
| 52 | + (status = NOT_FOUND, description = "No name was associated with this address.", body = ErrorResponse), |
| 53 | + (status = UNPROCESSABLE_ENTITY, description = "Reverse record not owned by this address.", body = ErrorResponse), |
| 54 | + ), |
| 55 | + params( |
| 56 | + ("address" = String, Path, description = "Address to lookup name data for"), |
| 57 | + ) |
| 58 | +)] |
| 59 | +pub async fn discovery_search( |
| 60 | + Query(query): Query<SearchQuery>, |
| 61 | + State(state): State<Arc<crate::AppState>>, |
| 62 | +) -> Result<Json<Vec<Profile>>, RouteError> { |
| 63 | + |
| 64 | + info!("query: {:?}", query.s); |
| 65 | + |
| 66 | + if let Some(discovery) = &state.service.discovery { |
| 67 | + let profiles = discovery.query_search(&state.service, query.s).await.unwrap(); |
| 68 | + return Ok(Json(profiles)); |
| 69 | + } |
| 70 | + // get_bulk( |
| 71 | + // Qs(AddressGetBulkQuery { |
| 72 | + // fresh: query, |
| 73 | + // addresses: vec![address], |
| 74 | + // }), |
| 75 | + // State(state), |
| 76 | + // ) |
| 77 | + // .await |
| 78 | + // .map(|mut res| { |
| 79 | + // Result::from(res.0.response.remove(0)) |
| 80 | + // .map(Json) |
| 81 | + // .map_err(RouteError::from) |
| 82 | + // })? |
| 83 | + todo!() |
| 84 | +} |
0 commit comments