use super::Kalshi;
use crate::kalshi_error::*;
use crate::market::Event; use serde::{Deserialize, Serialize};
impl Kalshi {
#[allow(clippy::too_many_arguments)]
pub async fn get_events(
&self,
limit: Option<i64>,
cursor: Option<String>,
status: Option<String>,
series_ticker: Option<String>,
with_nested_markets: Option<bool>,
with_milestones: Option<bool>,
min_close_ts: Option<i64>,
) -> Result<(Option<String>, Vec<Event>), KalshiError> {
let path = "/events";
let mut params = vec![];
add_param!(params, "limit", limit);
add_param!(params, "cursor", cursor);
add_param!(params, "status", status);
add_param!(params, "series_ticker", series_ticker);
add_param!(params, "with_nested_markets", with_nested_markets);
add_param!(params, "with_milestones", with_milestones);
add_param!(params, "min_close_ts", min_close_ts);
let url = format!("{}{}", self.base_url, path);
let final_url = reqwest::Url::parse_with_params(&url, ¶ms)?;
let res: EventListResponse = self.client.get(final_url).send().await?.json().await?;
Ok((res.cursor, res.events))
}
pub async fn get_event(&self, event_ticker: &str) -> Result<Event, KalshiError> {
let path = format!("/events/{}", event_ticker);
self.signed_get(&path).await
}
pub async fn get_event_candlesticks(
&self,
series_ticker: &str,
event_ticker: &str,
start_ts: Option<i64>,
end_ts: Option<i64>,
period_interval: Option<String>,
) -> Result<Vec<Candlestick>, KalshiError> {
let path = format!(
"/series/{}/events/{}/candlesticks",
series_ticker, event_ticker
);
let mut params = vec![];
add_param!(params, "start_ts", start_ts);
add_param!(params, "end_ts", end_ts);
add_param!(params, "period_interval", period_interval);
let url = format!("{}{}", self.base_url, path);
let final_url = reqwest::Url::parse_with_params(&url, ¶ms)?;
let res: CandlestickResponse = self.client.get(final_url).send().await?.json().await?;
Ok(res.candlesticks)
}
pub async fn get_event_metadata(
&self,
event_ticker: &str,
) -> Result<EventMetadata, KalshiError> {
let path = format!("/events/{}/metadata", event_ticker);
self.signed_get(&path).await
}
pub async fn get_event_forecast_percentile_history(
&self,
event_ticker: &str,
) -> Result<ForecastPercentileHistory, KalshiError> {
let path = format!("/events/{}/forecast_percentile_history", event_ticker);
self.signed_get(&path).await
}
pub async fn get_multivariate_events(
&self,
limit: Option<i32>,
cursor: Option<String>,
series_ticker: Option<String>,
collection_ticker: Option<String>,
with_nested_markets: Option<bool>,
) -> Result<(Option<String>, Vec<Event>), KalshiError> {
if series_ticker.is_some() && collection_ticker.is_some() {
return Err(KalshiError::UserInputError(
"Cannot use both series_ticker and collection_ticker - these filters are mutually exclusive".to_string()
));
}
let mut params: Vec<(&str, String)> = Vec::with_capacity(5);
add_param!(params, "limit", limit);
add_param!(params, "cursor", cursor);
add_param!(params, "series_ticker", series_ticker);
add_param!(params, "collection_ticker", collection_ticker);
add_param!(params, "with_nested_markets", with_nested_markets);
let url = format!("{}/events/multivariate", self.base_url);
let final_url = reqwest::Url::parse_with_params(&url, ¶ms)?;
let res: EventListResponse = self.client.get(final_url).send().await?.json().await?;
Ok((res.cursor, res.events))
}
}
#[derive(Debug, Deserialize)]
struct EventListResponse {
cursor: Option<String>,
events: Vec<Event>,
}
#[derive(Debug, Deserialize)]
#[allow(dead_code)] struct SingleEventResponse {
event: Event,
}
#[derive(Debug, Deserialize)]
struct CandlestickResponse {
candlesticks: Vec<Candlestick>,
}
#[derive(Debug, Deserialize, Serialize)]
pub struct Candlestick {
pub ts: String,
pub open: Option<i64>,
pub high: Option<i64>,
pub low: Option<i64>,
pub close: Option<i64>,
pub volume: Option<i64>,
}
#[derive(Debug, Deserialize, Serialize)]
pub struct EventMetadata {
#[serde(flatten)]
pub fields: std::collections::HashMap<String, serde_json::Value>,
}
#[derive(Debug, Deserialize, Serialize)]
pub struct ForecastPercentileHistory {
pub history: Vec<ForecastDataPoint>,
}
#[derive(Debug, Deserialize, Serialize)]
pub struct ForecastDataPoint {
pub ts: String,
pub percentiles: std::collections::HashMap<String, f64>,
}