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 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
//! Provider part of the Market API use ya_client_model::market::{ Agreement, AgreementOperationEvent, NewOffer, NewProposal, Offer, Proposal, ProviderEvent, Reason, MARKET_API_PATH, }; use crate::{web::default_on_timeout, web::WebClient, web::WebInterface, Result}; use chrono::{DateTime, TimeZone}; use std::fmt::Display; /// Bindings for Provider part of the Market API. #[derive(Clone)] pub struct MarketProviderApi { client: WebClient, } impl WebInterface for MarketProviderApi { const API_URL_ENV_VAR: &'static str = crate::market::MARKET_URL_ENV_VAR; const API_SUFFIX: &'static str = MARKET_API_PATH; fn from_client(client: WebClient) -> Self { MarketProviderApi { client } } } impl MarketProviderApi { /// Publish Provider’s service capabilities (`Offer`) on the market to declare an /// interest in Demands meeting specified criteria. pub async fn subscribe(&self, offer: &NewOffer) -> Result<String> { self.client.post("offers").send_json(&offer).json().await } /// Fetches all active Offers which have been published by the Provider. pub async fn get_offers(&self) -> Result<Vec<Offer>> { self.client.get("offers").send().json().await } /// Stop subscription by invalidating a previously published Offer. /// /// Stop receiving Proposals. /// **Note**: this will terminate all pending `collect_demands` calls on this subscription. /// This implies, that client code should not `unsubscribe_offer` before it has received /// all expected/useful inputs from `collect_demands`. pub async fn unsubscribe(&self, subscription_id: &str) -> Result<()> { let url = url_format!("offers/{subscription_id}", subscription_id); self.client.delete(&url).send().json().await } /// Get events which have arrived from the market in response to the Offer /// published by the Provider via [`subscribe`](#method.subscribe). /// Returns collection of at most `max_events` `ProviderEvents` or times out. /// /// This is a blocking operation. It will not return until there is at /// least one new event. /// /// Returns Proposal related events: /// /// * `ProposalEvent` - Indicates that there is new Demand Proposal for /// this Offer. /// /// * `ProposalRejectedEvent` - Indicates that the Requestor has rejected /// our previous Proposal related to this Offer. This effectively ends a /// Negotiation chain - it explicitly indicates that the sender will not /// create another counter-Proposal. /// /// * `AgreementEvent` - Indicates that the Requestor is accepting our /// previous Proposal and ask for our approval of the Agreement. /// /// * `PropertyQueryEvent` - not supported yet. /// /// **Note**: When `collectOffers` is waiting, simultaneous call to /// `unsubscribeDemand` on the same `subscriptionId` should result in /// "Subscription does not exist" error returned from `collectOffers`. /// /// **Note**: Specification requires this endpoint to support list of /// specific Proposal Ids to listen for messages related only to specific /// Proposals. This is not covered yet. #[rustfmt::skip] pub async fn collect( &self, subscription_id: &str, timeout: Option<f32>, max_events: Option<i32>, ) -> Result<Vec<ProviderEvent>> { let url = url_format!( "offers/{subscription_id}/events", subscription_id, #[query] timeout, #[query] max_events, ); self.client.get(&url).send().json().await.or_else(default_on_timeout) } /// Fetches Proposal (Demand) with given id. pub async fn get_proposal(&self, subscription_id: &str, proposal_id: &str) -> Result<Proposal> { let url = url_format!( "offers/{subscription_id}/proposals/{proposal_id}", subscription_id, proposal_id, ); self.client.get(&url).send().json().await } /// Rejects Proposal (Demand). /// /// Effectively ends a Negotiation chain - it explicitly indicates that /// the sender will not create another counter-Proposal. pub async fn reject_proposal( &self, subscription_id: &str, proposal_id: &str, reason: &Option<Reason>, ) -> Result<()> { let url = url_format!( "offers/{subscription_id}/proposals/{proposal_id}/reject", subscription_id, proposal_id, ); self.client.post(&url).send_json(&reason).json().await } /// Responds with a bespoke Offer to received Demand. /// Creates and sends a modified version of original Offer (a /// counter-proposal) adjusted to previously received Proposal (ie. Demand). /// Changes Proposal state to `Draft`. Returns created Proposal id. pub async fn counter_proposal( &self, offer_proposal: &NewProposal, subscription_id: &str, proposal_id: &str, ) -> Result<String> { let url = url_format!( "offers/{subscription_id}/proposals/{proposal_id}", subscription_id, proposal_id, ); self.client .post(&url) .send_json(&offer_proposal) .json() .await } /// Approves Agreement proposed by the Reqestor. /// /// This is a blocking operation. The call may be aborted by Provider caller /// code. After the call is aborted or timed out, another `approve_agreement` /// call can be raised on the same `agreement_id`. /// /// It returns one of the following options: /// /// * `Ok` Agreement approved. Indicates that the approved Agreement has been /// successfully delivered to the Requestor and acknowledged. /// - The Requestor side has been notified about the Provider’s commitment. /// - The Provider is now ready to accept a request to start an Activity. /// - The Requestor’s corresponding `wait_for_approval` call returns `Ok` /// (Approved) **after** this endpoint on the Provider side. /// /// * `Err` - Indicates that Agreement is not approved. /// - `408` Agreement not approved within given timeout. Try again. /// - `410` Agreement approval failed permanently. /// Attached `ErrorMessage` contains further details: /// - `Rejected` - Indicates that the Provider himself has already /// called `reject_agreement`. /// - `Cancelled` - Indicates that before Provider approved this Agreement, /// the Requestor has called `cancel_agreement`, thus invalidating the /// Agreement. The Provider may attempt to return to the Negotiation phase /// by sending a new Proposal. /// - `Expired` - Indicates that Agreement validity period elapsed and it was /// not approved, rejected nor cancelled. /// - `Terminated` - Indicates that Agreement is already terminated. #[rustfmt::skip] pub async fn approve_agreement( &self, agreement_id: &str, app_session_id: Option<String>, timeout: Option<f32>, ) -> Result<()> { let url = url_format!( "agreements/{agreement_id}/approve", agreement_id, #[query] app_session_id, #[query] timeout, ); self.client.post(&url).send().json().await } /// Rejects Agreement proposed by the Requestor. /// /// The Requestor side is notified about the Provider’s decision to reject /// a negotiated agreement. This effectively stops the Agreement handshake. /// /// **Note**: Mutually exclusive with `approve_agreement`. pub async fn reject_agreement( &self, agreement_id: &str, reason: &Option<Reason>, ) -> Result<()> { let url = url_format!("agreements/{agreement_id}/reject", agreement_id); self.client.post(&url).send_json(&reason).json().await } /// Terminates approved Agreement. pub async fn terminate_agreement( &self, agreement_id: &str, reason: &Option<Reason>, ) -> Result<()> { let url = url_format!("agreements/{agreement_id}/terminate", agreement_id); self.client.post(&url).send_json(&reason).json().await } /// Fetches agreement with given agreement id. 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 } /// Collects events related to an Agreement. /// /// This is a blocking operation. It will not return until there is /// at least one new event. All events are appearing on both sides equally. /// /// Returns Agreement related events: /// /// * `AgreementApprovedEvent` - Indicates that the Agreement has been /// approved by the Provider. /// - The Provider is now ready to accept a request to start an /// Activity as described in the negotiated agreement. /// - The Providers’s corresponding `approveAgreement` call /// returns `Approved` after this event is emitted. /// /// * `AgreementRejectedEvent` - Indicates that the Provider has called /// `rejectAgreement`, which effectively stops the Agreement handshake. /// The Requestor may attempt to return to the Negotiation phase by /// sending a new Proposal. /// /// * `AgreementCancelledEvent` - Indicates that the Requestor has called /// `cancelAgreement`, which effectively stops the Agreement handshake. /// /// * `AgreementTerminatedEvent` - Indicates that the Agreement has been /// terminated by specified party (contains signature). #[rustfmt::skip] pub async fn collect_agreement_events<Tz>( &self, timeout: Option<f32>, after_timestamp: Option<&DateTime<Tz>>, max_events: Option<i32>, app_session_id: Option<String>, ) -> Result<Vec<AgreementOperationEvent>> where Tz: TimeZone, Tz::Offset: Display, { let after_timestamp = after_timestamp.map(|dt| dt.to_rfc3339()); let url = url_format!( "agreementEvents", #[query] timeout, #[query] after_timestamp, #[query] max_events, #[query] app_session_id, ); self.client.get(&url).send().json().await.or_else(default_on_timeout) } }