Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Payment API: examples adjusted for event API #879

Merged
merged 1 commit into from
Dec 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 29 additions & 7 deletions core/payment/examples/cancel_invoice.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
use bigdecimal::BigDecimal;
use chrono::Utc;
use std::time::Duration;
use ya_client::payment::{PaymentProviderApi, PaymentRequestorApi};
use ya_client::web::WebClient;
use structopt::StructOpt;
use ya_client::payment::PaymentApi;
use ya_client::web::{rest_api_url, WebClient};
use ya_client_model::payment::{
Acceptance, DocumentStatus, EventType, NewAllocation, NewDebitNote, NewInvoice,
};
use ya_core_model::payment::local as pay;
use ya_service_bus::typed as bus;

#[derive(Clone, Debug, StructOpt)]
struct Args {
#[structopt(long)]
app_session_id: Option<String>,
}

async fn assert_requested_amount(
payer_addr: &str,
payee_addr: &str,
Expand Down Expand Up @@ -39,9 +46,19 @@ async fn main() -> anyhow::Result<()> {
std::env::set_var("RUST_LOG", log_level);
env_logger::init();

let client = WebClient::builder().build();
let provider: PaymentProviderApi = client.interface()?;
let requestor: PaymentRequestorApi = client.interface()?;
let args: Args = Args::from_args();

// Create requestor / provider PaymentApi
let provider_url = format!("{}provider/", rest_api_url()).parse().unwrap();
let provider: PaymentApi = WebClient::builder()
.api_url(provider_url)
.build()
.interface()?;
let requestor_url = format!("{}requestor/", rest_api_url()).parse().unwrap();
let requestor: PaymentApi = WebClient::builder()
.api_url(requestor_url)
.build()
.interface()?;

let debit_note = NewDebitNote {
activity_id: "activity1".to_string(),
Expand Down Expand Up @@ -107,12 +124,17 @@ async fn main() -> anyhow::Result<()> {

log::info!("Listening for invoice cancelled event...");
let mut events = requestor
.get_invoice_events(Some(&now), Some(Duration::from_secs(5)))
.get_invoice_events(
Some(&now),
Some(Duration::from_secs(5)),
None,
args.app_session_id.clone(),
)
.await?;
assert_eq!(events.len(), 1);
let event = events.pop().unwrap();
assert_eq!(&event.invoice_id, &invoice.invoice_id);
assert_eq!(&event.event_type, &EventType::Cancelled);
assert!(matches!(&event.event_type, &EventType::Cancelled));
log::info!("Event received and verified.");

log::info!("Verifying invoice status...");
Expand Down
34 changes: 22 additions & 12 deletions core/payment/examples/debit_note_flow.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,36 @@
use bigdecimal::BigDecimal;
use chrono::Utc;
use std::time::Duration;
use structopt::StructOpt;
use ya_client::payment::PaymentApi;
use ya_client::web::{rest_api_url, WebClient};
use ya_client_model::payment::{
Acceptance, DocumentStatus, NewAllocation, NewDebitNote, PAYMENT_API_PATH,
};
use ya_client_model::payment::{Acceptance, DocumentStatus, NewAllocation, NewDebitNote};

#[derive(Clone, Debug, StructOpt)]
struct Args {
#[structopt(long)]
app_session_id: Option<String>,
}

#[actix_rt::main]
async fn main() -> anyhow::Result<()> {
let log_level = std::env::var("RUST_LOG").unwrap_or("debit_note_flow=debug,info".to_owned());
std::env::set_var("RUST_LOG", log_level);
env_logger::init();

let args: Args = Args::from_args();

// Create requestor / provider PaymentApi
let rest_api_url = format!("{}{}", rest_api_url(), PAYMENT_API_PATH);
let provider_url = format!("{}provider/", &rest_api_url);
std::env::set_var("YAGNA_PAYMENT_URL", provider_url);
let provider: PaymentApi = WebClient::builder().build().interface()?;
let requestor_url = format!("{}requestor/", &rest_api_url);
std::env::set_var("YAGNA_PAYMENT_URL", requestor_url);
let requestor: PaymentApi = WebClient::builder().build().interface()?;
let provider_url = format!("{}provider/", rest_api_url()).parse().unwrap();
let provider: PaymentApi = WebClient::builder()
.api_url(provider_url)
.build()
.interface()?;
let requestor_url = format!("{}requestor/", rest_api_url()).parse().unwrap();
let requestor: PaymentApi = WebClient::builder()
.api_url(requestor_url)
.build()
.interface()?;

let debit_note_date = Utc::now();

Expand All @@ -46,7 +56,7 @@ async fn main() -> anyhow::Result<()> {
Some(&debit_note_date),
Some(Duration::from_secs(10)),
None,
None,
args.app_session_id.clone(),
)
.await
.unwrap();
Expand Down Expand Up @@ -146,7 +156,7 @@ async fn main() -> anyhow::Result<()> {
log::info!("Waiting for payment...");
let timeout = Some(Duration::from_secs(300)); // Should be enough for GNT transfer
let mut payments = provider
.get_payments(Some(&now), timeout, None, None)
.get_payments(Some(&now), timeout, None, args.app_session_id.clone())
.await?;
assert_eq!(payments.len(), 1);
let payment = payments.pop().unwrap();
Expand Down
22 changes: 15 additions & 7 deletions core/payment/examples/get_accounts.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use structopt::StructOpt;
use ya_client::payment::{PaymentProviderApi, PaymentRequestorApi};
use ya_client::web::WebClient;
use ya_client::payment::PaymentApi;
use ya_client::web::{rest_api_url, WebClient};
use ya_client_model::payment::Account;

#[derive(Clone, Debug, StructOpt)]
Expand All @@ -21,16 +21,24 @@ async fn main() -> anyhow::Result<()> {

let args: Args = Args::from_args();

let client = WebClient::builder().build();
let provider: PaymentProviderApi = client.interface()?;
let requestor: PaymentRequestorApi = client.interface()?;
// Create requestor / provider PaymentApi
let provider_url = format!("{}provider/", rest_api_url()).parse().unwrap();
let provider: PaymentApi = WebClient::builder()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

did you change the post-fix'ing? i thought i tested this as well... :S

.api_url(provider_url)
.build()
.interface()?;
let requestor_url = format!("{}requestor/", rest_api_url()).parse().unwrap();
let requestor: PaymentApi = WebClient::builder()
.api_url(requestor_url)
.build()
.interface()?;

log::info!("Checking provider account...");
let provider_account = Account {
platform: args.platform.clone(),
address: args.provider_addr,
};
let provider_accounts = provider.get_accounts().await?;
let provider_accounts = provider.get_provider_accounts().await?;
assert!(provider_accounts
.iter()
.any(|account| account == &provider_account));
Expand All @@ -41,7 +49,7 @@ async fn main() -> anyhow::Result<()> {
platform: args.platform.clone(),
address: args.requestor_addr,
};
let requestor_accounts = requestor.get_accounts().await?;
let requestor_accounts = requestor.get_requestor_accounts().await?;
assert!(requestor_accounts
.iter()
.any(|account| account == &requestor_account));
Expand Down
38 changes: 24 additions & 14 deletions core/payment/examples/invoice_flow.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,36 @@
use bigdecimal::BigDecimal;
use chrono::Utc;
use std::time::Duration;
use structopt::StructOpt;
use ya_client::payment::PaymentApi;
use ya_client::web::{rest_api_url, WebClient};
use ya_client_model::payment::{
Acceptance, DocumentStatus, NewAllocation, NewInvoice, PAYMENT_API_PATH,
};
use ya_client_model::payment::{Acceptance, DocumentStatus, NewAllocation, NewInvoice};

#[derive(Clone, Debug, StructOpt)]
struct Args {
#[structopt(long)]
app_session_id: Option<String>,
}

#[actix_rt::main]
async fn main() -> anyhow::Result<()> {
let log_level = std::env::var("RUST_LOG").unwrap_or("invoice_flow=debug,info".to_owned());
std::env::set_var("RUST_LOG", log_level);
env_logger::init();

let args: Args = Args::from_args();

// Create requestor / provider PaymentApi
let rest_api_url = format!("{}{}", rest_api_url(), PAYMENT_API_PATH);
let provider_url = format!("{}provider/", &rest_api_url);
std::env::set_var("YAGNA_PAYMENT_URL", provider_url);
let provider: PaymentApi = WebClient::builder().build().interface()?;
let requestor_url = format!("{}requestor/", &rest_api_url);
std::env::set_var("YAGNA_PAYMENT_URL", requestor_url);
let requestor: PaymentApi = WebClient::builder().build().interface()?;
let provider_url = format!("{}provider/", rest_api_url()).parse().unwrap();
let provider: PaymentApi = WebClient::builder()
.api_url(provider_url)
.build()
.interface()?;
let requestor_url = format!("{}requestor/", rest_api_url()).parse().unwrap();
let requestor: PaymentApi = WebClient::builder()
.api_url(requestor_url)
.build()
.interface()?;

let invoice_date = Utc::now();

Expand All @@ -45,7 +55,7 @@ async fn main() -> anyhow::Result<()> {
Some(&invoice_date),
Some(Duration::from_secs(10)),
None,
None,
args.app_session_id.clone(),
)
.await
.unwrap();
Expand Down Expand Up @@ -101,7 +111,7 @@ async fn main() -> anyhow::Result<()> {
Some(&invoice_events_received.first().unwrap().event_date),
Some(Duration::from_secs(10)),
None,
None,
args.app_session_id.clone(),
)
.await
.unwrap();
Expand All @@ -110,7 +120,7 @@ async fn main() -> anyhow::Result<()> {
log::info!("Waiting for payment...");
let timeout = Some(Duration::from_secs(300)); // Should be enough for GNT transfer
let mut payments = provider
.get_payments(Some(&now), timeout, None, None)
.get_payments(Some(&now), timeout, None, args.app_session_id.clone())
.await?;
assert_eq!(payments.len(), 1);
let payment = payments.pop().unwrap();
Expand All @@ -127,7 +137,7 @@ async fn main() -> anyhow::Result<()> {
Some(&invoice_events_accepted.first().unwrap().event_date),
Some(Duration::from_secs(10)),
None,
None,
args.app_session_id.clone(),
)
.await
.unwrap();
Expand Down
18 changes: 11 additions & 7 deletions core/payment/examples/market_decoration.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use bigdecimal::BigDecimal;
use futures::StreamExt;
use ya_client::payment::PaymentRequestorApi;
use ya_client::web::WebClient;
use ya_client::payment::PaymentApi;
use ya_client::web::{rest_api_url, WebClient};
use ya_client_model::payment::NewAllocation;

#[actix_rt::main]
Expand All @@ -10,13 +10,17 @@ async fn main() -> anyhow::Result<()> {
std::env::set_var("RUST_LOG", log_level);
env_logger::init();

let requestor_url = format!("{}requestor/", rest_api_url()).parse().unwrap();
let client = match std::env::var("YAGNA_APPKEY").ok() {
Some(token) => WebClient::with_token(&token),
None => WebClient::builder().build(),
Some(token) => WebClient::builder()
.api_url(requestor_url)
.auth_token(&token)
.build(),
None => WebClient::builder().api_url(requestor_url).build(),
};
let requestor: PaymentRequestorApi = client.interface()?;
let requestor: PaymentApi = client.interface()?;

let accounts = requestor.get_accounts().await?;
let accounts = requestor.get_requestor_accounts().await?;

let allocation_ids = futures::stream::iter(accounts)
.then(move |account| {
Expand All @@ -41,7 +45,7 @@ async fn main() -> anyhow::Result<()> {
.await;

log::info!("Decorating demand...");
let requestor: PaymentRequestorApi = client.interface()?;
let requestor: PaymentApi = client.interface()?;
let decoration = requestor.get_demand_decorations(allocation_ids).await?;
log::info!("Properties: {:?}", decoration.properties);
log::info!("Constraints: {:?}", decoration.constraints);
Expand Down
17 changes: 9 additions & 8 deletions core/payment/examples/payment_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ struct Args {
requestor_addr: Option<String>,
#[structopt(long, default_value = "agreement_id")]
agreement_id: String,
#[structopt(long)]
app_session_id: Option<String>,
}

pub async fn start_dummy_driver() -> anyhow::Result<()> {
Expand Down Expand Up @@ -265,7 +267,7 @@ async fn main() -> anyhow::Result<()> {
approved_date: None,
state: market::agreement::State::Proposal,
timestamp: Utc::now(),
app_session_id: None,
app_session_id: args.app_session_id,
proposed_signature: None,
approved_signature: None,
committed_signature: None,
Expand Down Expand Up @@ -294,19 +296,18 @@ async fn main() -> anyhow::Result<()> {
role: "".to_string(),
};

let provider_api_scope = Scope::new("/provider")
let provider_api_scope = Scope::new(&format!("provider/{}", PAYMENT_API_PATH))
.data(db.clone())
.extend(ya_payment::api::api_scope)
.wrap(DummyAuth::new(provider_identity));
let requestor_api_scope = Scope::new("/requestor")
let requestor_api_scope = Scope::new(&format!("requestor/{}", PAYMENT_API_PATH))
.data(db.clone())
.extend(ya_payment::api::api_scope)
.wrap(DummyAuth::new(requestor_identity));
let payment_service = Scope::new(PAYMENT_API_PATH)
.data(db.clone())
.service(provider_api_scope)
.service(requestor_api_scope);
App::new()
.wrap(middleware::Logger::default())
.service(payment_service)
.service(provider_api_scope)
.service(requestor_api_scope)
})
.bind(rest_addr)?
.run()
Expand Down
Loading