edc_connector_client/api/
contract_negotiations.rs

1use crate::{
2    client::EdcConnectorClientInternal,
3    types::{
4        context::{WithContext, WithContextRef},
5        contract_negotiation::{
6            ContractNegotiation, ContractNegotiationState, ContractRequest, NegotiationState,
7            TerminateNegotiation,
8        },
9        query::Query,
10        response::IdResponse,
11    },
12    EdcResult,
13};
14
15pub struct ContractNegotiationApi<'a>(&'a EdcConnectorClientInternal);
16
17impl<'a> ContractNegotiationApi<'a> {
18    pub(crate) fn new(client: &'a EdcConnectorClientInternal) -> ContractNegotiationApi<'a> {
19        ContractNegotiationApi(client)
20    }
21
22    pub async fn initiate(
23        &self,
24        contract_request: &ContractRequest,
25    ) -> EdcResult<IdResponse<String>> {
26        let url = self.get_endpoint(&[]);
27        self.0
28            .post::<_, WithContext<IdResponse<String>>>(
29                url,
30                &WithContextRef::odrl_context(contract_request),
31            )
32            .await
33            .map(|ctx| ctx.inner)
34    }
35
36    pub async fn get(&self, id: &str) -> EdcResult<ContractNegotiation> {
37        let url = self.get_endpoint(&[id]);
38        self.0
39            .get::<WithContext<ContractNegotiation>>(url)
40            .await
41            .map(|ctx| ctx.inner)
42    }
43
44    pub async fn get_state(&self, id: &str) -> EdcResult<ContractNegotiationState> {
45        let url = self.get_endpoint(&[id]);
46        self.0
47            .get::<WithContext<NegotiationState>>(url)
48            .await
49            .map(|ctx| ctx.inner.state().clone())
50    }
51
52    pub async fn terminate(&self, id: &str, reason: &str) -> EdcResult<()> {
53        let url = self.get_endpoint(&[id, "terminate"]);
54
55        let request = TerminateNegotiation {
56            id: id.to_string(),
57            reason: reason.to_string(),
58        };
59        self.0
60            .post_no_response(url, &WithContextRef::default_context(&request))
61            .await
62            .map(|_| ())
63    }
64
65    pub async fn query(&self, query: Query) -> EdcResult<Vec<ContractNegotiation>> {
66        let url = self.get_endpoint(&["request"]);
67        self.0
68            .post::<_, Vec<WithContext<ContractNegotiation>>>(
69                url,
70                &WithContextRef::default_context(&query),
71            )
72            .await
73            .map(|results| results.into_iter().map(|ctx| ctx.inner).collect())
74    }
75
76    fn get_endpoint(&self, paths: &[&str]) -> String {
77        [self.0.management_url.as_str(), "v3", "contractnegotiations"]
78            .into_iter()
79            .chain(paths.iter().copied())
80            .collect::<Vec<_>>()
81            .join("/")
82    }
83}