From f5cc09d38f3762939843a44080ade1ecae903a40 Mon Sep 17 00:00:00 2001 From: maaktweluit <10008353+maaktweluit@users.noreply.github.com> Date: Thu, 17 Dec 2020 13:35:29 +0100 Subject: [PATCH 1/3] Moved all query string serialization in payment api to 'serde_qs' --- core/payment/src/api.rs | 89 ----------------------------- core/payment/src/api/allocations.rs | 11 ++-- core/payment/src/api/debit_notes.rs | 36 ++++++------ core/payment/src/api/invoices.rs | 41 +++++++------ core/payment/src/api/payments.rs | 12 ++-- 5 files changed, 56 insertions(+), 133 deletions(-) diff --git a/core/payment/src/api.rs b/core/payment/src/api.rs index 8019f22ef4..069eff1299 100644 --- a/core/payment/src/api.rs +++ b/core/payment/src/api.rs @@ -1,6 +1,4 @@ use actix_web::Scope; -use chrono::{DateTime, Utc}; -use serde::{Deserialize, Deserializer}; use ya_client_model::payment::PAYMENT_API_PATH; use ya_persistence::executor::DbExecutor; use ya_service_api_web::scope::ExtendableScope; @@ -27,90 +25,3 @@ pub fn web_scope(db: &DbExecutor) -> Scope { // TODO: TEST // Scope::new(PAYMENT_API_PATH).extend(api_scope).data(db.clone()) } - -pub const DEFAULT_ACK_TIMEOUT: f64 = 60.0; // seconds -pub const DEFAULT_EVENT_TIMEOUT: f64 = 0.0; // seconds - -#[inline(always)] -pub(crate) fn default_ack_timeout() -> f64 { - DEFAULT_ACK_TIMEOUT -} - -#[inline(always)] -pub(crate) fn default_event_timeout() -> f64 { - DEFAULT_EVENT_TIMEOUT -} - -#[derive(Deserialize)] -pub struct DebitNoteId { - pub debit_note_id: String, -} - -#[derive(Deserialize)] -#[serde(rename_all = "camelCase")] -pub struct DebitNotePaymentsParams { - pub debit_note_id: String, - #[serde(default)] - pub max_items: Option, - #[serde(default)] - pub after_timestamp: Option>, -} - -#[derive(Deserialize)] -pub struct InvoiceId { - pub invoice_id: String, -} - -#[derive(Deserialize)] -pub struct AllocationId { - pub allocation_id: String, -} - -#[derive(Deserialize)] -pub struct PaymentId { - pub payment_id: String, -} - -#[derive(Deserialize)] -pub struct Timeout { - #[serde(default = "default_ack_timeout")] - pub timeout: f64, -} - -#[derive(Deserialize)] -#[serde(rename_all = "camelCase")] -pub struct EventParams { - #[serde(default = "default_event_timeout")] - pub poll_timeout: f64, - #[serde(default)] - pub after_timestamp: Option>, - #[serde(default)] - pub max_events: Option, - #[serde(default)] - pub app_session_id: Option, -} - -#[derive(Deserialize)] -pub struct FilterParams { - #[serde(rename = "maxItems", default)] - pub max_items: Option, - #[serde(rename = "afterTimestamp", default)] - pub after_timestamp: Option>, -} - -#[derive(Deserialize)] -pub struct AllocationIds { - #[serde( - rename = "allocationIds", - deserialize_with = "deserialize_comma_separated" - )] - pub allocation_ids: Vec, -} - -fn deserialize_comma_separated<'de, D>(deserializer: D) -> Result, D::Error> -where - D: Deserializer<'de>, -{ - let s: &str = Deserialize::deserialize(deserializer)?; - Ok(s.split(",").map(str::to_string).collect()) -} diff --git a/core/payment/src/api/allocations.rs b/core/payment/src/api/allocations.rs index fa85a2d220..fd2a8adf37 100644 --- a/core/payment/src/api/allocations.rs +++ b/core/payment/src/api/allocations.rs @@ -15,7 +15,6 @@ use ya_service_api_web::middleware::Identity; use ya_service_bus::{typed as bus, RpcEndpoint}; // Local uses -use crate::api::*; use crate::dao::*; use crate::error::{DbError, Error}; use crate::utils::response; @@ -80,7 +79,7 @@ async fn create_allocation( async fn get_allocations( db: Data, - query: Query, + query: Query, id: Identity, ) -> HttpResponse { let node_id = id.identity; @@ -95,7 +94,7 @@ async fn get_allocations( async fn get_allocation( db: Data, - path: Path, + path: Path, id: Identity, ) -> HttpResponse { let allocation_id = path.allocation_id.clone(); @@ -110,7 +109,7 @@ async fn get_allocation( async fn amend_allocation( db: Data, - path: Path, + path: Path, body: Json, ) -> HttpResponse { response::not_implemented() // TODO @@ -118,7 +117,7 @@ async fn amend_allocation( async fn release_allocation( db: Data, - path: Path, + path: Path, id: Identity, ) -> HttpResponse { let allocation_id = path.allocation_id.clone(); @@ -133,7 +132,7 @@ async fn release_allocation( async fn get_demand_decorations( db: Data, - path: Query, + path: Query, id: Identity, ) -> HttpResponse { let allocation_ids = path.allocation_ids.clone(); diff --git a/core/payment/src/api/debit_notes.rs b/core/payment/src/api/debit_notes.rs index 7d717ba223..18e3e1a4b4 100644 --- a/core/payment/src/api/debit_notes.rs +++ b/core/payment/src/api/debit_notes.rs @@ -18,7 +18,6 @@ use ya_service_api_web::middleware::Identity; use ya_service_bus::{typed as bus, RpcEndpoint}; // Local uses -use crate::api::*; use crate::dao::*; use crate::error::{DbError, Error}; use crate::utils::provider::get_agreement_for_activity; @@ -57,7 +56,7 @@ pub fn register_endpoints(scope: Scope) -> Scope { async fn get_debit_notes( db: Data, - query: Query, + query: Query, id: Identity, ) -> HttpResponse { let node_id = id.identity; @@ -75,7 +74,7 @@ async fn get_debit_notes( async fn get_debit_note( db: Data, - path: Path, + path: Path, id: Identity, ) -> HttpResponse { let debit_note_id = path.debit_note_id.clone(); @@ -88,17 +87,20 @@ async fn get_debit_note( } } -async fn get_debit_note_payments(db: Data, path: Path) -> HttpResponse { +async fn get_debit_note_payments( + db: Data, + path: Path, +) -> HttpResponse { response::not_implemented() // TODO } async fn get_debit_note_events( db: Data, - query: Query, + query: Query, id: Identity, ) -> HttpResponse { let node_id = id.identity; - let timeout_secs = query.poll_timeout; + let timeout_secs = query.poll_timeout.unwrap_or(params::DEFAULT_EVENT_TIMEOUT); let after_timestamp = query.after_timestamp.map(|d| d.naive_utc()); let max_events = query.max_events; let app_session_id = &query.app_session_id; @@ -173,8 +175,8 @@ async fn issue_debit_note( async fn send_debit_note( db: Data, - path: Path, - query: Query, + path: Path, + query: Query, id: Identity, ) -> HttpResponse { let debit_note_id = path.debit_note_id.clone(); @@ -190,7 +192,8 @@ async fn send_debit_note( return response::ok(Null); // Debit note has been already sent } - with_timeout(query.timeout, async move { + let timeout = query.timeout.unwrap_or(params::DEFAULT_ACK_TIMEOUT); + with_timeout(timeout, async move { match async move { ya_net::from(node_id) .to(debit_note.recipient_id) @@ -215,8 +218,8 @@ async fn send_debit_note( async fn cancel_debit_note( db: Data, - path: Path, - query: Query, + path: Path, + query: Query, ) -> HttpResponse { response::not_implemented() // TODO } @@ -225,8 +228,8 @@ async fn cancel_debit_note( async fn accept_debit_note( db: Data, - path: Path, - query: Query, + path: Path, + query: Query, body: Json, id: Identity, ) -> HttpResponse { @@ -287,7 +290,8 @@ async fn accept_debit_note( return response::bad_request(&msg); } - with_timeout(query.timeout, async move { + let timeout = query.timeout.unwrap_or(params::DEFAULT_ACK_TIMEOUT); + with_timeout(timeout, async move { let issuer_id = debit_note.issuer_id; let accept_msg = AcceptDebitNote::new(debit_note_id.clone(), acceptance, issuer_id); let schedule_msg = @@ -320,8 +324,8 @@ async fn accept_debit_note( async fn reject_debit_note( db: Data, - path: Path, - query: Query, + path: Path, + query: Query, body: Json, ) -> HttpResponse { response::not_implemented() // TODO diff --git a/core/payment/src/api/invoices.rs b/core/payment/src/api/invoices.rs index 059f574df7..da6ce0cbf4 100644 --- a/core/payment/src/api/invoices.rs +++ b/core/payment/src/api/invoices.rs @@ -19,7 +19,6 @@ use ya_service_api_web::middleware::Identity; use ya_service_bus::{typed as bus, RpcEndpoint}; // Local uses -use crate::api::*; use crate::dao::*; use crate::error::{DbError, Error}; use crate::utils::provider::get_agreement_id; @@ -46,7 +45,7 @@ pub fn register_endpoints(scope: Scope) -> Scope { async fn get_invoices( db: Data, - query: Query, + query: Query, id: Identity, ) -> HttpResponse { let node_id = id.identity; @@ -62,7 +61,11 @@ async fn get_invoices( } } -async fn get_invoice(db: Data, path: Path, id: Identity) -> HttpResponse { +async fn get_invoice( + db: Data, + path: Path, + id: Identity, +) -> HttpResponse { let invoice_id = path.invoice_id.clone(); let node_id = id.identity; let dao: InvoiceDao = db.as_dao(); @@ -73,17 +76,17 @@ async fn get_invoice(db: Data, path: Path, id: Identity) } } -async fn get_invoice_payments(db: Data, path: Path) -> HttpResponse { +async fn get_invoice_payments(db: Data, path: Path) -> HttpResponse { response::not_implemented() // TODO } async fn get_invoice_events( db: Data, - query: Query, + query: Query, id: Identity, ) -> HttpResponse { let node_id = id.identity; - let timeout_secs = query.poll_timeout; + let timeout_secs = query.poll_timeout.unwrap_or(params::DEFAULT_EVENT_TIMEOUT); let after_timestamp = query.after_timestamp.map(|d| d.naive_utc()); let max_events = query.max_events; let app_session_id = &query.app_session_id; @@ -170,8 +173,8 @@ async fn issue_invoice(db: Data, body: Json, id: Identit async fn send_invoice( db: Data, - path: Path, - query: Query, + path: Path, + query: Query, id: Identity, ) -> HttpResponse { let invoice_id = path.invoice_id.clone(); @@ -186,8 +189,8 @@ async fn send_invoice( if invoice.status != DocumentStatus::Issued { return response::ok(Null); // Invoice has been already sent } - - with_timeout(query.timeout, async move { + let timeout = query.timeout.unwrap_or(params::DEFAULT_ACK_TIMEOUT); + with_timeout(timeout, async move { match async move { ya_net::from(node_id) .to(invoice.recipient_id) @@ -213,8 +216,8 @@ async fn send_invoice( async fn cancel_invoice( db: Data, - path: Path, - query: Query, + path: Path, + query: Query, id: Identity, ) -> HttpResponse { let invoice_id = path.invoice_id.clone(); @@ -236,7 +239,8 @@ async fn cancel_invoice( } } - with_timeout(query.timeout, async move { + let timeout = query.timeout.unwrap_or(params::DEFAULT_ACK_TIMEOUT); + with_timeout(timeout, async move { match async move { ya_net::from(node_id) .to(invoice.recipient_id) @@ -267,8 +271,8 @@ async fn cancel_invoice( async fn accept_invoice( db: Data, - path: Path, - query: Query, + path: Path, + query: Query, body: Json, id: Identity, ) -> HttpResponse { @@ -332,7 +336,8 @@ async fn accept_invoice( return response::bad_request(&msg); } - with_timeout(query.timeout, async move { + let timeout = query.timeout.unwrap_or(params::DEFAULT_ACK_TIMEOUT); + with_timeout(timeout, async move { let issuer_id = invoice.issuer_id; let accept_msg = AcceptInvoice::new(invoice_id.clone(), acceptance, issuer_id); let schedule_msg = SchedulePayment::from_invoice(invoice, allocation_id, amount_to_pay); @@ -362,8 +367,8 @@ async fn accept_invoice( async fn reject_invoice( db: Data, - path: Path, - query: Query, + path: Path, + query: Query, body: Json, ) -> HttpResponse { response::not_implemented() // TODO diff --git a/core/payment/src/api/payments.rs b/core/payment/src/api/payments.rs index a4b732c39a..be8d52a9f6 100644 --- a/core/payment/src/api/payments.rs +++ b/core/payment/src/api/payments.rs @@ -3,11 +3,11 @@ use actix_web::web::{get, Data, Path, Query}; use actix_web::{HttpResponse, Scope}; // Workspace uses +use ya_client_model::payment::*; use ya_persistence::executor::DbExecutor; use ya_service_api_web::middleware::Identity; // Local uses -use crate::api::*; use crate::dao::*; use crate::utils::*; @@ -19,11 +19,11 @@ pub fn register_endpoints(scope: Scope) -> Scope { async fn get_payments( db: Data, - query: Query, + query: Query, id: Identity, ) -> HttpResponse { let node_id = id.identity; - let timeout_secs = query.poll_timeout; + let timeout_secs = query.poll_timeout.unwrap_or(params::DEFAULT_EVENT_TIMEOUT); let after_timestamp = query.after_timestamp.map(|d| d.naive_utc()); let max_events = query.max_events; let app_session_id = &query.app_session_id; @@ -45,7 +45,11 @@ async fn get_payments( } } -async fn get_payment(db: Data, path: Path, id: Identity) -> HttpResponse { +async fn get_payment( + db: Data, + path: Path, + id: Identity, +) -> HttpResponse { let payment_id = path.payment_id.clone(); let node_id = id.identity; let dao: PaymentDao = db.as_dao(); From a0098243f1372886b68e6996ba7890bcde4a894f Mon Sep 17 00:00:00 2001 From: maaktweluit <10008353+maaktweluit@users.noreply.github.com> Date: Mon, 11 Jan 2021 14:27:23 +0100 Subject: [PATCH 2/3] Update branches and cargo.lock --- Cargo.lock | 16 ++++++++++++++-- Cargo.toml | 4 ++-- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index eba9e3f958..a15cec4f78 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5278,6 +5278,17 @@ dependencies = [ "serde", ] +[[package]] +name = "serde_qs" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e5cb0f0564a84554436c4ceff5c896308d4e09d0eb4bd0215b8f698f88084601" +dependencies = [ + "percent-encoding 2.1.0", + "serde", + "thiserror", +] + [[package]] name = "serde_urlencoded" version = "0.6.1" @@ -6910,7 +6921,7 @@ dependencies = [ [[package]] name = "ya-client" version = "0.4.0" -source = "git+https://github.com/golemfactory/ya-client.git?rev=87f106498a7a7b1519d82d0981fc21b260a89cf2#87f106498a7a7b1519d82d0981fc21b260a89cf2" +source = "git+https://github.com/golemfactory/ya-client.git?rev=e27bd52cc501b99b3cef8d16149779856d8c0e14#e27bd52cc501b99b3cef8d16149779856d8c0e14" dependencies = [ "awc 1.0.1", "bytes 0.5.6", @@ -6925,6 +6936,7 @@ dependencies = [ "rand 0.6.5", "serde", "serde_json", + "serde_qs", "structopt", "thiserror", "url 2.2.0", @@ -6947,7 +6959,7 @@ dependencies = [ [[package]] name = "ya-client-model" version = "0.2.0" -source = "git+https://github.com/golemfactory/ya-client.git?rev=87f106498a7a7b1519d82d0981fc21b260a89cf2#87f106498a7a7b1519d82d0981fc21b260a89cf2" +source = "git+https://github.com/golemfactory/ya-client.git?rev=e27bd52cc501b99b3cef8d16149779856d8c0e14#e27bd52cc501b99b3cef8d16149779856d8c0e14" dependencies = [ "bigdecimal 0.1.2", "chrono", diff --git a/Cargo.toml b/Cargo.toml index 1ef500abda..07b10bd98b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -172,8 +172,8 @@ ya-sb-router = { path = "service-bus/router" } ya-sb-util = { path = "service-bus/util" } ## CLIENT -ya-client = { git = "https://github.com/golemfactory/ya-client.git", rev = "87f106498a7a7b1519d82d0981fc21b260a89cf2"} -ya-client-model = { git = "https://github.com/golemfactory/ya-client.git", rev = "87f106498a7a7b1519d82d0981fc21b260a89cf2"} +ya-client = { git = "https://github.com/golemfactory/ya-client.git", rev = "e27bd52cc501b99b3cef8d16149779856d8c0e14"} +ya-client-model = { git = "https://github.com/golemfactory/ya-client.git", rev = "e27bd52cc501b99b3cef8d16149779856d8c0e14"} #ya-client = { path = "../ya-client" } #ya-client-model = { path = "../ya-client/model" } From bc397c212cacdf37215e6932fcadefbab4a6f3b1 Mon Sep 17 00:00:00 2001 From: maaktweluit <10008353+maaktweluit@users.noreply.github.com> Date: Wed, 13 Jan 2021 15:56:42 +0100 Subject: [PATCH 3/3] Update Cargo.toml after merge in ya-client --- Cargo.lock | 4 ++-- Cargo.toml | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a15cec4f78..67b906bf82 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6921,7 +6921,7 @@ dependencies = [ [[package]] name = "ya-client" version = "0.4.0" -source = "git+https://github.com/golemfactory/ya-client.git?rev=e27bd52cc501b99b3cef8d16149779856d8c0e14#e27bd52cc501b99b3cef8d16149779856d8c0e14" +source = "git+https://github.com/golemfactory/ya-client.git?rev=200d42efa6bd77de071b5674efe55aeb2f6296e1#200d42efa6bd77de071b5674efe55aeb2f6296e1" dependencies = [ "awc 1.0.1", "bytes 0.5.6", @@ -6959,7 +6959,7 @@ dependencies = [ [[package]] name = "ya-client-model" version = "0.2.0" -source = "git+https://github.com/golemfactory/ya-client.git?rev=e27bd52cc501b99b3cef8d16149779856d8c0e14#e27bd52cc501b99b3cef8d16149779856d8c0e14" +source = "git+https://github.com/golemfactory/ya-client.git?rev=200d42efa6bd77de071b5674efe55aeb2f6296e1#200d42efa6bd77de071b5674efe55aeb2f6296e1" dependencies = [ "bigdecimal 0.1.2", "chrono", diff --git a/Cargo.toml b/Cargo.toml index 07b10bd98b..eb063ae838 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -172,8 +172,8 @@ ya-sb-router = { path = "service-bus/router" } ya-sb-util = { path = "service-bus/util" } ## CLIENT -ya-client = { git = "https://github.com/golemfactory/ya-client.git", rev = "e27bd52cc501b99b3cef8d16149779856d8c0e14"} -ya-client-model = { git = "https://github.com/golemfactory/ya-client.git", rev = "e27bd52cc501b99b3cef8d16149779856d8c0e14"} +ya-client = { git = "https://github.com/golemfactory/ya-client.git", rev = "200d42efa6bd77de071b5674efe55aeb2f6296e1"} +ya-client-model = { git = "https://github.com/golemfactory/ya-client.git", rev = "200d42efa6bd77de071b5674efe55aeb2f6296e1"} #ya-client = { path = "../ya-client" } #ya-client-model = { path = "../ya-client/model" }