Skip to content

Commit 4b15f54

Browse files
authored
Merge pull request #20 from interledger/15-remove-client-field-from-grant-request-type
fix: remove client field from GrantRequest type
2 parents a8a9006 + 939ddf2 commit 4b15f54

File tree

16 files changed

+68
-49
lines changed

16 files changed

+68
-49
lines changed

src/client/config.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use std::path::PathBuf;
1818
/// key_id: "my-key-2024".to_string(),
1919
/// private_key_path: PathBuf::from("keys/private.pem"),
2020
/// jwks_path: Some(PathBuf::from("keys/jwks.json")),
21+
/// wallet_address_url: "https://rafiki.money/alice".into(),
2122
/// };
2223
/// ```
2324
#[derive(Debug, Clone, Serialize, Deserialize)]
@@ -43,6 +44,11 @@ pub struct ClientConfig {
4344
///
4445
/// Example: `Some(PathBuf::from("keys/jwks.json"))`
4546
pub jwks_path: Option<PathBuf>,
47+
48+
/// URL of the wallet address to use for the client.
49+
///
50+
/// This is the URL of the wallet address that will be used to send and receive payments.
51+
pub wallet_address_url: String,
4652
}
4753

4854
impl Default for ClientConfig {
@@ -68,7 +74,8 @@ impl Default for ClientConfig {
6874
Self {
6975
key_id: "".into(),
7076
private_key_path: PathBuf::from("private.key"),
71-
jwks_path: Some(PathBuf::from("jwks.json")),
77+
jwks_path: None,
78+
wallet_address_url: "".into(),
7279
}
7380
}
7481
}

src/client/core.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ pub trait BaseClient {
3131
/// private_key_path: "path/to/private-key.pem".into(),
3232
/// key_id: "my-key-id".to_string(),
3333
/// jwks_path: Some("path/to/jwks.json".into()),
34+
/// wallet_address_url: "https://rafiki.money/alice".into(),
3435
/// };
3536
///
3637
/// // This would fail in a real scenario if the files don't exist

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/client/mod.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
//! private_key_path: "path/to/private-key.pem".into(),
3838
//! key_id: "my-key-id".to_string(),
3939
//! jwks_path: Some("path/to/jwks.json".into()),
40+
//! wallet_address_url: "https://rafiki.money/alice".into(),
4041
//! };
4142
//!
4243
//! // This would fail in a real scenario if the files don't exist
@@ -47,16 +48,15 @@
4748
//! let wallet_address = client.wallet_address().get("https://rafiki.money/alice").await?;
4849
//!
4950
//! // Example of how to request a grant
50-
//! let grant_request = GrantRequest {
51-
//! access_token: AccessTokenRequest {
51+
//! let grant_request = GrantRequest::new(
52+
//! AccessTokenRequest {
5253
//! access: vec![AccessItem::IncomingPayment {
5354
//! actions: vec![IncomingPaymentAction::Create, IncomingPaymentAction::Read],
5455
//! identifier: None,
5556
//! }],
5657
//! },
57-
//! client: "https://rafiki.money/alice".to_string(),
58-
//! interact: None,
59-
//! };
58+
//! None,
59+
//! );
6060
//!
6161
//! let access_token = client.grant().request(&wallet_address.auth_server, &grant_request).await?;
6262
//!

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
//! private_key_path: "path/to/private-key.pem".into(),
2323
//! key_id: "my-key-id".to_string(),
2424
//! jwks_path: Some("path/to/jwks.json".into()),
25+
//! wallet_address_url: "https://rafiki.money/alice".into(),
2526
//! };
2627
//!
2728
//! // This would fail in a real scenario if the files don't exist
@@ -63,6 +64,7 @@
6364
//! private_key_path: "path/to/private-key.pem".into(),
6465
//! key_id: "my-key-id".to_string(),
6566
//! jwks_path: Some("path/to/jwks.json".into()),
67+
//! wallet_address_url: "https://rafiki.money/alice".into(),
6668
//! };
6769
//!
6870
//! // This would fail in a real scenario if the files don't exist

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>,

0 commit comments

Comments
 (0)