Skip to content

Commit 02175fb

Browse files
committed
fix: remove client field from GrantRequest type, add client field in when building request
1 parent 96051e0 commit 02175fb

File tree

12 files changed

+55
-39
lines changed

12 files changed

+55
-39
lines changed

src/client/config.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ pub struct ClientConfig {
4343
///
4444
/// Example: `Some(PathBuf::from("keys/jwks.json"))`
4545
pub jwks_path: Option<PathBuf>,
46+
47+
/// URL of the wallet address to use for the client.
48+
///
49+
/// This is the URL of the wallet address that will be used to send and receive payments.
50+
pub wallet_address_url: String,
4651
}
4752

4853
impl Default for ClientConfig {
@@ -68,7 +73,8 @@ impl Default for ClientConfig {
6873
Self {
6974
key_id: "".into(),
7075
private_key_path: PathBuf::from("private.key"),
71-
jwks_path: Some(PathBuf::from("jwks.json")),
76+
jwks_path: None,
77+
wallet_address_url: "".into(),
7278
}
7379
}
7480
}

src/client/grant.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@ pub(crate) async fn request_grant(
1010
auth_url: &str,
1111
grant: &GrantRequest,
1212
) -> Result<GrantResponse> {
13-
let body = serde_json::to_string(grant).map_err(OpClientError::Serde)?;
13+
let grant_with_client = GrantRequest {
14+
client: client.config.wallet_address_url.clone(),
15+
..grant.clone()
16+
};
17+
let body = serde_json::to_string(&grant_with_client).map_err(OpClientError::Serde)?;
1418

1519
AuthenticatedRequest::new(client, Method::POST, auth_url.to_string())
1620
.with_body(body)

src/snippets/grant/incoming_payment.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ async fn main() -> open_payments::client::Result<()> {
1515
let wallet_address_url = get_env_var("WALLET_ADDRESS_URL")?;
1616
let wallet_address = client.wallet_address().get(&wallet_address_url).await?;
1717

18-
let grant_request = GrantRequest {
19-
access_token: AccessTokenRequest {
18+
let grant_request = GrantRequest::new(
19+
AccessTokenRequest {
2020
access: vec![AccessItem::IncomingPayment {
2121
actions: vec![
2222
IncomingPaymentAction::Create,
@@ -28,9 +28,8 @@ async fn main() -> open_payments::client::Result<()> {
2828
identifier: None,
2929
}],
3030
},
31-
client: wallet_address.id,
32-
interact: None,
33-
};
31+
None,
32+
);
3433

3534
println!(
3635
"Grant request JSON: {}",

src/snippets/grant/outgoing_payment.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ async fn main() -> open_payments::client::Result<()> {
2626
let quote = client.quotes().get(&quote_url, Some(&gnap_token)).await?;
2727

2828
let wallet_id = &wallet_address.id;
29-
let grant_request = GrantRequest {
30-
access_token: AccessTokenRequest {
29+
let grant_request = GrantRequest::new(
30+
AccessTokenRequest {
3131
access: vec![AccessItem::OutgoingPayment {
3232
actions: vec![
3333
OutgoingPaymentAction::Read,
@@ -44,16 +44,15 @@ async fn main() -> open_payments::client::Result<()> {
4444
}),
4545
}],
4646
},
47-
client: wallet_id.to_string(),
48-
interact: Some(InteractRequest {
47+
Some(InteractRequest {
4948
start: vec!["redirect".to_string()],
5049
finish: Some(InteractFinish {
5150
method: "redirect".to_string(),
5251
uri: "http://localhost".to_string(),
5352
nonce: Uuid::new_v4().to_string(),
5453
}),
5554
}),
56-
};
55+
);
5756

5857
println!(
5958
"Grant request JSON: {}",

src/snippets/grant/quote.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,14 @@ async fn main() -> open_payments::client::Result<()> {
1616
let wallet_address_url = get_env_var("WALLET_ADDRESS_URL")?;
1717
let wallet_address = client.wallet_address().get(&wallet_address_url).await?;
1818

19-
let grant_request = GrantRequest {
20-
access_token: AccessTokenRequest {
19+
let grant_request = GrantRequest::new(
20+
AccessTokenRequest {
2121
access: vec![AccessItem::Quote {
2222
actions: vec![QuoteAction::Create, QuoteAction::Read, QuoteAction::ReadAll],
2323
}],
2424
},
25-
client: wallet_address.id,
26-
interact: None,
27-
};
25+
None,
26+
);
2827

2928
println!(
3029
"Grant request JSON: {}",

src/snippets/utils/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,17 @@ pub fn get_env_var(key: &str) -> Result<String> {
1515
}
1616

1717
pub fn create_authenticated_client() -> Result<AuthenticatedClient> {
18+
let wallet_address_url = get_env_var("WALLET_ADDRESS_URL")?;
1819
let private_key_path = PathBuf::from(get_env_var("PRIVATE_KEY_PATH")?);
1920
let key_id = get_env_var("KEY_ID")?;
2021
let key_id_clone = key_id.clone();
2122
let jwks_path = get_env_var("JWKS_PATH").map(PathBuf::from).ok();
2223

2324
let config = ClientConfig {
24-
private_key_path,
2525
key_id,
26+
private_key_path,
2627
jwks_path,
28+
wallet_address_url,
2729
};
2830

2931
let client = AuthenticatedClient::new(config)

src/types/auth.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,21 @@ pub struct AccessTokenResponse {
8181
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
8282
pub struct GrantRequest {
8383
pub access_token: AccessTokenRequest,
84-
pub client: String,
84+
pub(crate) client: String,
8585
#[serde(skip_serializing_if = "Option::is_none")]
8686
pub interact: Option<InteractRequest>,
8787
}
8888

89+
impl GrantRequest {
90+
pub fn new(access_token: AccessTokenRequest, interact: Option<InteractRequest>) -> Self {
91+
Self {
92+
access_token,
93+
client: String::new(), // Will be set by the client internally
94+
interact,
95+
}
96+
}
97+
}
98+
8999
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
90100
pub struct AccessTokenRequest {
91101
pub access: Vec<AccessItem>,

tests/integration/common.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ impl TestSetup {
3232
let config = ClientConfig {
3333
key_id,
3434
private_key_path: private_key_path.into(),
35+
wallet_address_url: wallet_address.clone(),
3536
..Default::default()
3637
};
3738

tests/integration/grant.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ async fn test_grant_flows() {
1515
.await
1616
.expect("Failed to get wallet address");
1717

18-
let grant_request = GrantRequest {
19-
access_token: AccessTokenRequest {
18+
let grant_request = GrantRequest::new(
19+
AccessTokenRequest {
2020
access: vec![AccessItem::IncomingPayment {
2121
actions: vec![
2222
IncomingPaymentAction::Create,
@@ -28,9 +28,8 @@ async fn test_grant_flows() {
2828
identifier: None,
2929
}],
3030
},
31-
client: wallet_address.id,
32-
interact: None,
33-
};
31+
None,
32+
);
3433

3534
let response = test_setup
3635
.auth_client

tests/integration/incoming_payment.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ async fn get_access_token(test_setup: &mut TestSetup) -> String {
1313
.await
1414
.expect("Failed to get wallet address");
1515

16-
let grant_request = GrantRequest {
17-
access_token: AccessTokenRequest {
16+
let grant_request = GrantRequest::new(
17+
AccessTokenRequest {
1818
access: vec![AccessItem::IncomingPayment {
1919
actions: vec![
2020
IncomingPaymentAction::Create,
@@ -26,9 +26,8 @@ async fn get_access_token(test_setup: &mut TestSetup) -> String {
2626
identifier: None,
2727
}],
2828
},
29-
client: wallet_address.id,
30-
interact: None,
31-
};
29+
None,
30+
);
3231

3332
let response = test_setup
3433
.auth_client

0 commit comments

Comments
 (0)