-
Notifications
You must be signed in to change notification settings - Fork 59
Added the http.get and http.post functions with query params, headers, form, bodies as optional params (depending on GET vs POST)
#636
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
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
3796114
added the get function with query params and headers as optional params
Cictrone cb890b4
only strings for query params
Cictrone 34c35b6
and now post as well
Cictrone 91032c4
typo
Cictrone 7a1bb45
logic fix
Cictrone 420b1a2
fixed sugestions
Cictrone File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,212 @@ | ||
| use anyhow::Result; | ||
| use reqwest::header::{HeaderMap, HeaderName, HeaderValue}; | ||
| use starlark::collections::SmallMap; | ||
| use std::collections::HashMap; | ||
|
|
||
| pub fn get( | ||
| uri: String, | ||
| query_params: Option<SmallMap<String, String>>, | ||
| headers: Option<SmallMap<String, String>>, | ||
| ) -> Result<String> { | ||
| let mut query_map = HashMap::new(); | ||
| let mut headers_map = HeaderMap::new(); | ||
| let runtime = tokio::runtime::Builder::new_current_thread() | ||
| .enable_all() | ||
| .build()?; | ||
|
|
||
| if let Some(q) = query_params { | ||
| for (k, v) in q { | ||
| query_map.insert(k, v); | ||
| } | ||
| } | ||
|
|
||
| if let Some(h) = headers { | ||
| for (k, v) in h { | ||
| let name = HeaderName::from_bytes(k.as_bytes())?; | ||
| let value = HeaderValue::from_bytes(v.as_bytes())?; | ||
| headers_map.append(name, value); | ||
| } | ||
| } | ||
|
|
||
| runtime.block_on(handle_get(uri, query_map, headers_map)) | ||
| } | ||
|
|
||
| async fn handle_get( | ||
| uri: String, | ||
| query_params: HashMap<String, String>, | ||
| headers: HeaderMap, | ||
| ) -> Result<String> { | ||
| #[cfg(debug_assertions)] | ||
| log::info!( | ||
| "eldritch sending HTTP GET request to '{}' with headers '{:#?}'", | ||
| uri, | ||
| headers | ||
| ); | ||
|
|
||
| let client = reqwest::Client::new() | ||
| .get(uri) | ||
| .headers(headers) | ||
| .query(&query_params); | ||
| let resp = client.send().await?.text().await?; | ||
| Ok(resp) | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
|
|
||
| use super::*; | ||
| use httptest::{matchers::*, responders::*, Expectation, Server}; | ||
| use starlark::collections::SmallMap; | ||
|
|
||
| #[test] | ||
| fn test_get_no_params_or_headers() -> anyhow::Result<()> { | ||
| // running test http server | ||
| let server = Server::run(); | ||
| server.expect( | ||
| Expectation::matching(request::method_path("GET", "/foo")) | ||
| .respond_with(status_code(200).body("test body")), | ||
| ); | ||
|
|
||
| // reference test server uri | ||
| let url = server.url("/foo").to_string(); | ||
|
|
||
| // run our code | ||
| let contents = get(url, None, None)?; | ||
|
|
||
| // check request returned correctly | ||
| assert_eq!(contents, "test body"); | ||
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_get_empty_params_and_headers() -> anyhow::Result<()> { | ||
| // running test http server | ||
| let server = Server::run(); | ||
| server.expect( | ||
| Expectation::matching(request::method_path("GET", "/foo")) | ||
| .respond_with(status_code(200).body("test body")), | ||
| ); | ||
|
|
||
| // reference test server uri | ||
| let url = server.url("/foo").to_string(); | ||
|
|
||
| // run our code | ||
| let contents = get(url, Some(SmallMap::new()), Some(SmallMap::new()))?; | ||
|
|
||
| // check request returned correctly | ||
| assert_eq!(contents, "test body"); | ||
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_get_with_params() -> anyhow::Result<()> { | ||
| // running test http server | ||
| let server = Server::run(); | ||
| let m = all_of![ | ||
| request::method_path("GET", "/foo"), | ||
| request::query(url_decoded(contains(("a", "true")))), | ||
| request::query(url_decoded(contains(("b", "bar")))), | ||
| request::query(url_decoded(contains(("c", "3")))), | ||
| ]; | ||
| server.expect(Expectation::matching(m).respond_with(status_code(200).body("test body"))); | ||
|
|
||
| // reference test server uri | ||
| let url = server.url("/foo").to_string(); | ||
|
|
||
| // run our code | ||
| let mut params = SmallMap::new(); | ||
| params.insert("a".to_string(), "true".to_string()); | ||
| params.insert("b".to_string(), "bar".to_string()); | ||
| params.insert("c".to_string(), "3".to_string()); | ||
| let contents = get(url, Some(params), None)?; | ||
|
|
||
| // check request returned correctly | ||
| assert_eq!(contents, "test body"); | ||
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_get_with_hybrid_params() -> anyhow::Result<()> { | ||
| // running test http server | ||
| let server = Server::run(); | ||
| let m = all_of![ | ||
| request::method_path("GET", "/foo"), | ||
| request::query(url_decoded(contains(("a", "true")))), | ||
| request::query(url_decoded(contains(("b", "bar")))), | ||
| request::query(url_decoded(contains(("c", "3")))), | ||
| ]; | ||
| server.expect(Expectation::matching(m).respond_with(status_code(200).body("test body"))); | ||
|
|
||
| // reference test server uri | ||
| let url = server.url("/foo?a=true").to_string(); | ||
|
|
||
| // run our code | ||
| let mut params = SmallMap::new(); | ||
| params.insert("b".to_string(), "bar".to_string()); | ||
| params.insert("c".to_string(), "3".to_string()); | ||
| let contents = get(url, Some(params), None)?; | ||
|
|
||
| // check request returned correctly | ||
| assert_eq!(contents, "test body"); | ||
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_get_with_headers() -> anyhow::Result<()> { | ||
| // running test http server | ||
| let server = Server::run(); | ||
| let m = all_of![ | ||
| request::method_path("GET", "/foo"), | ||
| request::headers(contains(("a", "TRUE"))), | ||
| request::headers(contains(("b", "bar"))), | ||
| ]; | ||
| server.expect(Expectation::matching(m).respond_with(status_code(200).body("test body"))); | ||
|
|
||
| // reference test server uri | ||
| let url = server.url("/foo").to_string(); | ||
|
|
||
| // run our code | ||
| let mut headers = SmallMap::new(); | ||
| headers.insert("A".to_string(), "TRUE".to_string()); | ||
| headers.insert("b".to_string(), "bar".to_string()); | ||
| let contents = get(url, None, Some(headers))?; | ||
|
|
||
| // check request returned correctly | ||
| assert_eq!(contents, "test body"); | ||
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_get_with_params_and_headers() -> anyhow::Result<()> { | ||
| // running test http server | ||
| let server = Server::run(); | ||
| let m = all_of![ | ||
| request::method_path("GET", "/foo"), | ||
| request::headers(contains(("a", "TRUE"))), | ||
| request::headers(contains(("b", "bar"))), | ||
| request::query(url_decoded(contains(("c", "3")))), | ||
| ]; | ||
| server.expect(Expectation::matching(m).respond_with(status_code(200).body("test body"))); | ||
|
|
||
| // reference test server uri | ||
| let url = server.url("/foo").to_string(); | ||
|
|
||
| // run our code | ||
| let mut headers = SmallMap::new(); | ||
| headers.insert("A".to_string(), "TRUE".to_string()); | ||
| headers.insert("b".to_string(), "bar".to_string()); | ||
| let mut params = SmallMap::new(); | ||
| params.insert("c".to_string(), "3".to_string()); | ||
| let contents = get(url, Some(params), Some(headers))?; | ||
|
|
||
| // check request returned correctly | ||
| assert_eq!(contents, "test body"); | ||
|
|
||
| Ok(()) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.