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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
use ya_client_model::market::{Agreement, AgreementProposal, Demand, Proposal, RequestorEvent};
use crate::{web::default_on_timeout, web::WebClient, web::WebInterface, Result};
#[derive(Clone)]
pub struct MarketRequestorApi {
client: WebClient,
}
impl WebInterface for MarketRequestorApi {
const API_URL_ENV_VAR: &'static str = crate::market::MARKET_URL_ENV_VAR;
const API_SUFFIX: &'static str = ya_client_model::market::MARKET_API_PATH;
fn from_client(client: WebClient) -> Self {
MarketRequestorApi { client }
}
}
impl MarketRequestorApi {
pub async fn subscribe(&self, demand: &Demand) -> Result<String> {
self.client.post("demands").send_json(&demand).json().await
}
pub async fn get_demands(&self) -> Result<Vec<Demand>> {
self.client.get("demands").send().json().await
}
pub async fn unsubscribe(&self, subscription_id: &str) -> Result<String> {
let url = url_format!("demands/{subscription_id}", subscription_id);
self.client.delete(&url).send().json().await
}
#[rustfmt::skip]
pub async fn collect(
&self,
subscription_id: &str,
timeout: Option<f32>,
#[allow(non_snake_case)]
maxEvents: Option<i32>,
) -> Result<Vec<RequestorEvent>> {
let url = url_format!(
"demands/{subscription_id}/events",
subscription_id,
#[query] timeout,
#[query] maxEvents
);
self.client.get(&url).send().json().await.or_else(default_on_timeout)
}
pub async fn counter_proposal(
&self,
demand_proposal: &Proposal,
subscription_id: &str,
) -> Result<String> {
let proposal_id = demand_proposal.prev_proposal_id()?;
let url = url_format!(
"demands/{subscription_id}/proposals/{proposal_id}",
subscription_id,
proposal_id
);
self.client
.post(&url)
.send_json(&demand_proposal)
.json()
.await
}
pub async fn get_proposal(&self, subscription_id: &str, proposal_id: &str) -> Result<Proposal> {
let url = url_format!(
"demands/{subscription_id}/proposals/{proposal_id}",
subscription_id,
proposal_id
);
self.client.get(&url).send().json().await
}
pub async fn reject_proposal(
&self,
subscription_id: &str,
proposal_id: &str,
) -> Result<String> {
let url = url_format!(
"demands/{subscription_id}/proposals/{proposal_id}",
subscription_id,
proposal_id
);
self.client.delete(&url).send().json().await
}
pub async fn create_agreement(&self, agreement: &AgreementProposal) -> Result<String> {
self.client
.post("agreements")
.send_json(&agreement)
.json()
.await
}
pub async fn get_agreement(&self, agreement_id: &str) -> Result<Agreement> {
let url = url_format!("agreements/{agreement_id}", agreement_id);
self.client.get(&url).send().json().await
}
pub async fn confirm_agreement(&self, agreement_id: &str) -> Result<String> {
let url = url_format!("agreements/{agreement_id}/confirm", agreement_id);
self.client.post(&url).send().json().await
}
#[rustfmt::skip]
pub async fn wait_for_approval(
&self,
agreement_id: &str,
timeout: Option<f32>,
) -> Result<String> {
let url = url_format!(
"agreements/{agreement_id}/wait",
agreement_id,
#[query] timeout
);
self.client.post(&url).send().json().await
}
pub async fn cancel_agreement(&self, agreement_id: &str) -> Result<()> {
let url = url_format!("agreements/{agreement_id}", agreement_id);
self.client.delete(&url).send().json().await
}
pub async fn terminate_agreement(&self, agreement_id: &str) -> Result<String> {
let url = url_format!("agreements/{agreement_id}/terminate", agreement_id);
self.client.post(&url).send().json().await
}
}