Expose paginated event and market discovery with closed-state filters

This commit is contained in:
2026-12-03 12:00:52 +08:00
parent 6f620089b6
commit ddbb96c799
9 changed files with 433 additions and 6 deletions
+22
View File
@@ -267,3 +267,25 @@ sequenceDiagram
```
加载 [主题事件样例](../../docs/examples/topic.jsonl) 后即可按上图读取。事件与市场均支持按数字 ID 和按 slug 查询,缺失资源返回 404。路径段按 UTF-8 百分号编码,路径中的加号表示加号。当前详情入口支持无查询参数的 GET。
### 浏览目录
`GET /markets` 和 `GET /events` 返回数组。支持 limit、offset、ascending、order、active、closed,以及可重复的 id 和 slug。市场列表也支持重复的 condition_ids 和 clob_token_ids。同类标识取并集,提供多类标识时取交集。查询字符串中的加号需要编码成 %2B。
```mermaid
flowchart LR
H["market_http.rs · GET /markets 与 /events"] --> P["exchange/gamma_list.rs"] --> D["exchange/gamma_directory.rs"]
```
```mermaid
sequenceDiagram
participant C as 客户端
participant H as market-http
participant E as Exchange
C->>H: GET /events?limit=1&order=id&ascending=true
H->>E: gamma_list(Events),保留重复参数
E-->>C: 一个事件及其可见子盘
C->>H: GET /markets?slug=nvda-300&closed=any
H->>E: gamma_list(Markets),按 slug 查询开放或已关闭的盘口
E-->>C: 匹配市场数组
```
+33
View File
@@ -354,6 +354,28 @@ fn respond(mut req: Request, engine: &Mutex<Exchange>) -> std::io::Result<()> {
allow = Some("GET");
Err((405, "Method not allowed"))
}
(&Method::Get, "/markets" | "/events") => {
let resource = if path == "/events" {
exchange::DirectoryResource::Events
} else {
exchange::DirectoryResource::Markets
};
let result = serde_urlencoded::from_str::<Vec<(String, String)>>(params)
.map_err(|_| exchange::MarketDataError::Invalid("Invalid directory query".into()))
.and_then(|fields| {
engine
.lock()
.map_err(|_| exchange::MarketDataError::Unavailable)?
.gamma_list(resource, &fields)
});
match result {
Ok(page) => Ok(Value::Array(page)),
Err(exchange::MarketDataError::Invalid(message)) => {
return send(req, 422, json!({"error":message}), None)
}
Err(exchange::MarketDataError::Unavailable) => Err((500, "Directory unavailable")),
}
}
(&Method::Get, "/market-clarifications") => clarifications(engine, params),
(&Method::Get, "/prices-history") => {
let result = serde_urlencoded::from_str::<exchange::ClobHistoryQuery>(params)
@@ -455,6 +477,8 @@ fn respond(mut req: Request, engine: &Mutex<Exchange>) -> std::io::Result<()> {
| "/book"
| "/last-trade-price"
| "/prices-history"
| "/markets"
| "/events"
| "/market-clarifications"
| "/v2/prices-history",
) => {
@@ -638,6 +662,15 @@ mod tests {
assert_eq!(quoted["bestAsk"], json!(0.41));
assert_eq!(quoted["outcomePrices"], r#"["0.4","0.6"]"#);
let cases = [
("GET","/events?slug=nvda-closing-price-2027&closed=ANY",String::new(),200,Some(json!([topic]))),
("GET","/markets?condition_ids=nvda-200&clob_token_ids=eb%3ANO%3Anvda-200&closed=Any",String::new(),200,Some(json!([info]))),
("GET","/markets?active=true&closed=false&slug=map%2B2%2F1",String::new(),200,Some(json!([separate]))),
("GET","/markets?condition_ids=s&clob_token_ids=unknown&closed=any",String::new(),200,Some(json!([]))),
("GET","/markets?closed=any",String::new(),422,None),
("GET","/events?closed=open",String::new(),422,None),
("GET","/markets?limit=0",String::new(),200,Some(json!([]))),
("GET","/events?order=unknown",String::new(),422,None),
("POST","/markets",String::new(),405,None),
("GET",event_path.as_str(),String::new(),200,Some(topic.clone())),
("GET","/events/slug/nvda-closing-price-2027",String::new(),200,Some(topic)),
("GET",market_path.as_str(),String::new(),200,Some(info.clone())),