Skip to content

Commit 8e34738

Browse files
author
Stephan Dilly
committed
support url parameters
1 parent d692502 commit 8e34738

File tree

3 files changed

+33
-14
lines changed

3 files changed

+33
-14
lines changed

rest-api/readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ Support as many flavours and special cases out there in different Rest APIs with
1515
## todo
1616

1717
- [x] proof of concept
18+
- [x] support url parameters
1819
- [ ] string results
1920
- [ ] define base url in attribute
20-
- [ ] support url parameters
2121
- [ ] support query parameters
2222
- [ ] support post using json
2323
- [ ] allow serializable types in api

rest-api/src/lib.rs

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,23 +65,44 @@ fn get_impl_item(trait_item: syn::TraitItem) -> syn::ImplItemMethod {
6565
fn get_impl_method(trait_item: syn::TraitItemMethod) -> syn::ImplItemMethod {
6666
let name = trait_item.clone().sig.ident;
6767
// find the endpoint attribute defining the url postfix to use
68-
let endpoint_name = match get_endpoint_attr(trait_item) {
68+
let endpoint_name = match get_endpoint_attr(trait_item.clone()) {
6969
Some(s) => s,
7070
None => name.to_string().clone(),
7171
};
7272

73+
let args: Vec<syn::Ident> = trait_item
74+
.clone()
75+
.sig
76+
.inputs
77+
.into_iter()
78+
.filter_map(|a| match a {
79+
syn::FnArg::Typed(arg) => Some(*arg.pat),
80+
_ => None,
81+
})
82+
.filter_map(|pat| match pat {
83+
syn::Pat::Ident(ident) => Some(ident.ident),
84+
_ => None,
85+
})
86+
.collect();
87+
88+
let path: syn::Expr = syn::parse2(quote! {&format!(#endpoint_name, #(#args),*)}).unwrap();
89+
7390
// build the method
74-
syn::parse2(quote! {
91+
let mut method_impl: syn::ImplItemMethod = syn::parse2(quote! {
7592
fn #name(&self) -> Result<serde_json::Value, Box<dyn std::error::Error>> {
76-
let url = self.base_url.clone() + #endpoint_name;
93+
let url = self.base_url.clone() + #path;
7794
let mut builder = reqwest::Client::new().get(url.as_str());
7895
builder = self.add_headers(builder);
7996
let json: serde_json::Value = builder.send()?.json()?;
8097

8198
Ok(json)
8299
}
83100
})
84-
.unwrap()
101+
.unwrap();
102+
103+
method_impl.sig = trait_item.clone().sig;
104+
105+
method_impl
85106
}
86107

87108
fn get_endpoint_attr(trait_item: syn::TraitItemMethod) -> Option<String> {

src/main.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@ trait AlpacaApi {
99
fn get_account(&self) -> Result<serde_json::Value, Box<dyn std::error::Error>>;
1010

1111
/// see https://docs.alpaca.markets/api-documentation/api-v2/account-activities/
12-
#[endpoint("account/activities/FILL")]
12+
#[endpoint("account/activities/{}")]
1313
fn account_activities(
14-
&self, //activity_type: String,
14+
&self,
15+
activity_type: &str,
1516
) -> Result<serde_json::Value, Box<dyn std::error::Error>>;
1617

1718
/// see https://docs.alpaca.markets/api-documentation/api-v2/clock/
@@ -36,14 +37,11 @@ fn main() {
3637
};
3738

3839
let acc = api.get_account().unwrap();
40+
println!("account:\n{:#?}\n", acc);
3941

40-
println!("{:#?}", acc);
41-
42-
let act = api.account_activities().unwrap();
43-
44-
println!("{:#?}", act);
42+
let act = api.account_activities("FILL").unwrap();
43+
println!("activities:\n{:#?}\n", act);
4544

4645
let clc = api.clock().unwrap();
47-
48-
println!("{:#?}", clc);
46+
println!("clock:\n{:#?}\n", clc);
4947
}

0 commit comments

Comments
 (0)