-
Notifications
You must be signed in to change notification settings - Fork 2.9k
RPC Consistency Proposal #2473
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
RPC Consistency Proposal #2473
Changes from 24 commits
a8f5f41
778dd7c
d1b1c33
b77506c
b764cd4
d9091e6
90c411f
625bf8f
085d173
a98ef9b
c95f09a
6099651
3b81537
11e6876
88b9c52
c5f82ff
844e4e6
71f8087
cbea694
da7f057
f5e63f6
5f4500b
37dd63e
9913620
f849eb5
1fddd80
e1f71d4
be7f927
0b1e31f
8e1b0a5
abe13c2
eae1e22
0a3da1f
e7e1db7
6b76769
b6f206d
2f52aaa
cd3be50
dbc5cdc
c5db12f
24b8d69
4a8d839
51ec4a9
37b03f1
396a953
af0c6d6
b1e89d6
6e50f3b
dcec472
8c6af1f
818a717
0717b2d
6397726
f6c9e70
a87f998
27cebde
3bd9b96
e41b8ab
ee36ce1
d479306
14fcc5f
3429123
bf3f85d
2972279
1219775
cf656bf
e630285
2435cfe
385eda2
703bc24
511431f
48d508d
461fbe7
af2484a
44cffec
5cb1639
eb25459
0f6f319
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -77,6 +77,12 @@ use pagination::{ | |
| PaginatedResult, | ||
| PaginationRequest, | ||
| }; | ||
| use reqwest::header::{ | ||
| AsHeaderName, | ||
| HeaderMap, | ||
| HeaderValue, | ||
| IntoHeaderName, | ||
| }; | ||
| use schema::{ | ||
| balance::BalanceArgs, | ||
| blob::BlobByIdArgs, | ||
|
|
@@ -149,12 +155,24 @@ pub mod types; | |
|
|
||
| type RegisterId = u32; | ||
|
|
||
| #[derive(Debug, derive_more::Display, derive_more::From)] | ||
| #[non_exhaustive] | ||
| /// Error occurring during interaction with the FuelClient | ||
| // anyhow::Error is wrapped inside a custom Error type, | ||
| // so that we can specific error variants in the future. | ||
| pub enum Error { | ||
| /// Unknown or not expected(by architecture) error. | ||
| #[from] | ||
| Other(anyhow::Error), | ||
| } | ||
|
|
||
| #[derive(Debug, Clone)] | ||
| pub struct FuelClient { | ||
| client: reqwest::Client, | ||
| #[cfg(feature = "subscriptions")] | ||
| cookie: std::sync::Arc<reqwest::cookie::Jar>, | ||
| url: reqwest::Url, | ||
| headers: HeaderMap, | ||
| } | ||
|
|
||
| impl FromStr for FuelClient { | ||
|
|
@@ -182,13 +200,18 @@ impl FromStr for FuelClient { | |
| client, | ||
| cookie, | ||
| url, | ||
| headers: HeaderMap::new(), | ||
| }) | ||
| } | ||
|
|
||
| #[cfg(not(feature = "subscriptions"))] | ||
| { | ||
| let client = reqwest::Client::new(); | ||
| Ok(Self { client, url }) | ||
| Ok(Self { | ||
| client, | ||
| url, | ||
| headers: HeaderMap::new(), | ||
| }) | ||
| } | ||
| } | ||
| } | ||
|
|
@@ -221,6 +244,23 @@ impl FuelClient { | |
| Self::from_str(url.as_ref()) | ||
| } | ||
|
|
||
| pub fn set_header( | ||
| &mut self, | ||
| key: impl IntoHeaderName, | ||
| value: impl TryInto<HeaderValue>, | ||
| ) -> Result<&mut Self, Error> { | ||
| let header_value: HeaderValue = value | ||
| .try_into() | ||
| .map_err(|_err| anyhow::anyhow!("Cannot parse value for header"))?; | ||
|
||
| self.headers.insert(key, header_value); | ||
| Ok(self) | ||
| } | ||
|
|
||
| pub fn remove_header(&mut self, key: impl AsHeaderName) -> &mut Self { | ||
| self.headers.remove(key); | ||
| self | ||
| } | ||
|
|
||
| /// Send the GraphQL query to the client. | ||
| pub async fn query<ResponseData, Vars>( | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done in d479306 |
||
| &self, | ||
|
|
@@ -230,9 +270,12 @@ impl FuelClient { | |
| Vars: serde::Serialize, | ||
| ResponseData: serde::de::DeserializeOwned + 'static, | ||
| { | ||
| let response = self | ||
| .client | ||
| .post(self.url.clone()) | ||
| let mut request_builder = self.client.post(self.url.clone()); | ||
| for (header_name, header_value) in self.headers.iter() { | ||
| request_builder = request_builder.header(header_name, header_value); | ||
| } | ||
|
||
|
|
||
| let response = request_builder | ||
| .run_graphql(q) | ||
| .await | ||
| .map_err(|e| io::Error::new(io::ErrorKind::Other, e))?; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we don't use it and can remove
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done in d479306