-
Notifications
You must be signed in to change notification settings - Fork 348
Expand file tree
/
Copy pathevents.rs
More file actions
114 lines (95 loc) · 3 KB
/
Copy pathevents.rs
File metadata and controls
114 lines (95 loc) · 3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
use anyhow::Result;
use clap::{Args, Subcommand};
use polymarket_client_sdk::gamma::{
self,
types::request::{EventByIdRequest, EventBySlugRequest, EventTagsRequest, EventsRequest},
};
use super::is_numeric_id;
use crate::output::OutputFormat;
use crate::output::events::{print_event, print_events};
use crate::output::tags::print_tags;
#[derive(Args)]
pub struct EventsArgs {
#[command(subcommand)]
pub command: EventsCommand,
}
#[derive(Subcommand)]
pub enum EventsCommand {
/// List events with optional filters
List {
/// Filter by active status
#[arg(long)]
active: Option<bool>,
/// Filter by closed status
#[arg(long)]
closed: Option<bool>,
/// Max results
#[arg(long, default_value = "25")]
limit: i32,
/// Pagination offset
#[arg(long)]
offset: Option<i32>,
/// Sort field (e.g. volume, liquidity, `created_at`)
#[arg(long)]
order: Option<String>,
/// Sort ascending instead of descending
#[arg(long)]
ascending: bool,
/// Filter by tag slug (e.g. "politics", "crypto")
#[arg(long)]
tag: Option<String>,
},
/// Get a single event by ID or slug
Get {
/// Event ID (numeric) or slug
id: String,
},
/// Get tags for an event
Tags {
/// Event ID
id: String,
},
}
pub async fn execute(client: &gamma::Client, args: EventsArgs, output: OutputFormat) -> Result<()> {
match args.command {
EventsCommand::List {
active,
closed,
limit,
offset,
order,
ascending,
tag,
} => {
let resolved_closed = closed.or_else(|| active.map(|a| !a));
let request = EventsRequest::builder()
.limit(limit)
.maybe_closed(resolved_closed)
.maybe_offset(offset)
.maybe_ascending(if ascending { Some(true) } else { None })
.maybe_tag_slug(tag)
// EventsRequest::order is Vec<String>; into_iter on Option yields 0 or 1 items.
.order(order.into_iter().collect())
.build();
let events = client.events(&request).await?;
print_events(&events, &output)?;
}
EventsCommand::Get { id } => {
let is_numeric = is_numeric_id(&id);
let event = if is_numeric {
let req = EventByIdRequest::builder().id(id).build();
client.event_by_id(&req).await?
} else {
let req = EventBySlugRequest::builder().slug(id).build();
client.event_by_slug(&req).await?
};
print_event(&event, &output)?;
}
EventsCommand::Tags { id } => {
let req = EventTagsRequest::builder().id(id).build();
let tags = client.event_tags(&req).await?;
print_tags(&tags, &output)?;
}
}
Ok(())
}