use std::collections::HashMap;
pub use marlowe_client::models;
use marlowe_client::{
apis::{
self,
configuration::Configuration,
default_api::{
ApplyInputsToContractError, CreateContractError, CreateContractSourcesError,
GetContractByIdError, GetContractSourceAdjacencyError, GetContractSourceByIdError,
GetContractSourceClosureError, GetContractTransactionByIdError,
GetNextStepsForContractError, GetPayoutByIdError, GetPayoutsError,
GetTransactionsForContractError, GetWithdrawalByIdError, GetWithdrawalsError,
HealthcheckError, SubmitContractError, SubmitContractTransactionError,
SubmitWithdrawalError, WithdrawPayoutsError,
},
},
models::{
ApplyInputsTxEnvelope, Contract, ContractHeader, ContractState, CreateTxEnvelope, Next,
PayoutHeader, PayoutState, PostContractSourceResponse, PostContractsRequest,
PostTransactionsRequest, PostWithdrawalsRequest, TextEnvelope, Tx, TxHeader,
WithdrawTxEnvelope, Withdrawal, WithdrawalHeader,
},
reqwest::header::HeaderMap,
};
pub use marlowe_client::apis::configuration;
pub use marlowe_client::apis::default_api::GetContractsError;
pub use marlowe_client::apis::Error;
pub type Headers = HashMap<String, String>;
pub fn init_client(base_path: String) -> Configuration {
let mut config = Configuration::new();
config.base_path = base_path;
config
}
fn header_map_to_hash_map(header_map: &HeaderMap) -> HashMap<String, String> {
header_map
.iter()
.map(|(name, value)| {
let name_str = name.as_str().to_string();
let value_str = value.to_str().unwrap_or_default().to_string();
(name_str, value_str)
})
.collect()
}
pub async fn get_contracts(
configuration: &Configuration,
role_currency: Option<Vec<String>>,
tag: Option<Vec<String>>,
party_address: Option<Vec<String>>,
party_role: Option<Vec<String>>,
range: Option<&str>,
) -> Result<(Vec<ContractHeader>, Headers), Error<GetContractsError>> {
apis::default_api::get_contracts(
configuration,
role_currency,
tag,
party_address,
party_role,
range,
)
.await
.map(|(contracts_data, headers)| {
let contracts = contracts_data
.results
.iter()
.map(|result| *(result.resource.clone()))
.collect::<Vec<models::ContractHeader>>();
(contracts, header_map_to_hash_map(&headers))
})
}
pub async fn create_contract(
configuration: &Configuration,
x_change_address: &str,
x_stake_address: Option<&str>,
x_address: Option<&str>,
x_collateral_utx_o: Option<&str>,
post_contracts_request: PostContractsRequest,
) -> Result<(CreateTxEnvelope, Headers), Error<CreateContractError>> {
apis::default_api::create_contract(
configuration,
x_change_address,
x_stake_address,
x_address,
x_collateral_utx_o,
Some(post_contracts_request),
)
.await
.map(|(create_raw_data, headers)| {
let create_data = *(create_raw_data.resource.clone());
(create_data, header_map_to_hash_map(&headers))
})
}
pub async fn upload_contract_sources(
configuration: &Configuration,
main: &str,
labelled_object: Option<Vec<crate::models::LabelledObject>>,
) -> Result<(PostContractSourceResponse, Headers), Error<CreateContractSourcesError>> {
apis::default_api::create_contract_sources(configuration, main, labelled_object)
.await
.map(|(sources_created, headers)| (sources_created, header_map_to_hash_map(&headers)))
}
pub async fn get_contract_source_by_id(
configuration: &Configuration,
contract_source_id: &str,
expand: Option<bool>,
) -> Result<(Contract, Headers), Error<GetContractSourceByIdError>> {
apis::default_api::get_contract_source_by_id(configuration, contract_source_id, expand)
.await
.map(|(contract, headers)| (contract, header_map_to_hash_map(&headers)))
}
pub async fn get_contract_source_adjacency(
configuration: &Configuration,
contract_source_id: &str,
) -> Result<(Vec<String>, Headers), Error<GetContractSourceAdjacencyError>> {
apis::default_api::get_contract_source_adjacency(configuration, contract_source_id)
.await
.map(|(ids, headers)| (ids.results, header_map_to_hash_map(&headers)))
}
pub async fn get_contract_source_closure(
configuration: &Configuration,
contract_source_id: &str,
) -> Result<(Vec<String>, Headers), Error<GetContractSourceClosureError>> {
apis::default_api::get_contract_source_closure(configuration, contract_source_id)
.await
.map(|(ids, headers)| (ids.results, header_map_to_hash_map(&headers)))
}
pub async fn get_contract_by_id(
configuration: &Configuration,
contract_id: &str,
) -> Result<(ContractState, Headers), Error<GetContractByIdError>> {
apis::default_api::get_contract_by_id(configuration, contract_id)
.await
.map(|(contract, headers)| (*contract.resource, header_map_to_hash_map(&headers)))
}
pub async fn submit_contract(
configuration: &Configuration,
contract_id: &str,
text_envelope: Option<TextEnvelope>,
) -> Result<Headers, Error<SubmitContractError>> {
apis::default_api::submit_contract(configuration, contract_id, text_envelope)
.await
.map(|headers| header_map_to_hash_map(&headers))
}
pub async fn get_next_steps_for_contract(
configuration: &Configuration,
contract_id: &str,
validity_start: &str,
validity_end: &str,
party: Option<Vec<String>>,
) -> Result<(Next, Headers), Error<GetNextStepsForContractError>> {
apis::default_api::get_next_steps_for_contract(
configuration,
contract_id,
validity_start,
validity_end,
party,
)
.await
.map(|(next, headers)| (next, header_map_to_hash_map(&headers)))
}
pub async fn get_transactions_for_contract(
configuration: &Configuration,
contract_id: &str,
range: Option<&str>,
) -> Result<(Vec<TxHeader>, Headers), Error<GetTransactionsForContractError>> {
apis::default_api::get_transactions_for_contract(configuration, contract_id, range)
.await
.map(|(response, headers)| {
(
response
.results
.iter()
.map(|result| *(result.resource.clone()))
.collect::<Vec<TxHeader>>(),
header_map_to_hash_map(&headers),
)
})
}
pub async fn apply_inputs_to_contract(
configuration: &Configuration,
contract_id: &str,
x_change_address: &str,
x_address: Option<&str>,
x_collateral_utx_o: Option<&str>,
post_transactions_request: Option<PostTransactionsRequest>,
) -> Result<(ApplyInputsTxEnvelope, Headers), Error<ApplyInputsToContractError>> {
apis::default_api::apply_inputs_to_contract(
configuration,
contract_id,
x_change_address,
x_address,
x_collateral_utx_o,
post_transactions_request,
)
.await
.map(|(response, headers)| (*(response.resource), header_map_to_hash_map(&headers)))
}
pub async fn get_contract_transaction_by_id(
configuration: &Configuration,
contract_id: &str,
transaction_id: &str,
) -> Result<(Tx, Headers), Error<GetContractTransactionByIdError>> {
apis::default_api::get_contract_transaction_by_id(configuration, contract_id, transaction_id)
.await
.map(|(tx, headers)| (*tx.resource, header_map_to_hash_map(&headers)))
}
pub async fn submit_contract_transaction(
configuration: &Configuration,
contract_id: &str,
transaction_id: &str,
text_envelope: Option<TextEnvelope>,
) -> Result<Headers, Error<SubmitContractTransactionError>> {
apis::default_api::submit_contract_transaction(
configuration,
contract_id,
transaction_id,
text_envelope,
)
.await
.map(|headers| (header_map_to_hash_map(&headers)))
}
pub async fn healthcheck(
configuration: &Configuration,
) -> Result<Headers, Error<HealthcheckError>> {
apis::default_api::healthcheck(configuration)
.await
.map(|headers| header_map_to_hash_map(&headers))
}
pub async fn get_payouts(
configuration: &Configuration,
contract_id: Option<Vec<String>>,
role_token: Option<Vec<String>>,
status: Option<&str>,
range: Option<&str>,
) -> Result<(Vec<PayoutHeader>, Headers), Error<GetPayoutsError>> {
apis::default_api::get_payouts(configuration, contract_id, role_token, status, range)
.await
.map(|(response, headers)| {
let payouts = response
.results
.iter()
.map(|result| *(result.resource.clone()))
.collect::<Vec<PayoutHeader>>();
(payouts, header_map_to_hash_map(&headers))
})
}
pub async fn get_payout_by_id(
configuration: &Configuration,
payout_id: &str,
) -> Result<(PayoutState, Headers), Error<GetPayoutByIdError>> {
apis::default_api::get_payout_by_id(configuration, payout_id)
.await
.map(|(response, headers)| {
(
*(response.resource.clone()),
header_map_to_hash_map(&headers),
)
})
}
pub async fn get_withdrawals(
configuration: &Configuration,
role_currency: Option<Vec<String>>,
range: Option<&str>,
) -> Result<(Vec<WithdrawalHeader>, Headers), Error<GetWithdrawalsError>> {
apis::default_api::get_withdrawals(configuration, role_currency, range)
.await
.map(|(response, headers)| {
let withdrawals = response
.results
.iter()
.map(|result| *(result.resource.clone()))
.collect::<Vec<WithdrawalHeader>>();
(withdrawals, header_map_to_hash_map(&headers))
})
}
pub async fn withdraw_payouts(
configuration: &Configuration,
x_change_address: &str,
x_address: Option<&str>,
x_collateral_utx_o: Option<&str>,
post_withdrawals_request: Option<PostWithdrawalsRequest>,
) -> Result<(WithdrawTxEnvelope, Headers), Error<WithdrawPayoutsError>> {
apis::default_api::withdraw_payouts(
configuration,
x_change_address,
x_address,
x_collateral_utx_o,
post_withdrawals_request,
)
.await
.map(|(response, headers)| (*(response.resource), header_map_to_hash_map(&headers)))
}
pub async fn get_withdrawal_by_id(
configuration: &Configuration,
withdrawal_id: &str,
) -> Result<(Withdrawal, Headers), Error<GetWithdrawalByIdError>> {
apis::default_api::get_withdrawal_by_id(configuration, withdrawal_id)
.await
.map(|(withdrawal, headers)| ((withdrawal, header_map_to_hash_map(&headers))))
}
pub async fn submit_withdrawal(
configuration: &Configuration,
withdrawal_id: &str,
text_envelope: Option<TextEnvelope>,
) -> Result<Headers, Error<SubmitWithdrawalError>> {
apis::default_api::submit_withdrawal(configuration, withdrawal_id, text_envelope)
.await
.map(|headers| (header_map_to_hash_map(&headers)))
}