diff options
author | Kornel <kornel@geekhood.net> | 2020-04-11 19:27:17 +0100 |
---|---|---|
committer | Kornel <kornel@geekhood.net> | 2020-04-11 19:27:17 +0100 |
commit | 78f6d187519ff8740ba438e6e09ae2cd544955cd (patch) | |
tree | 9e7a0602e2ae0a1d95f892935d073cd81824d789 /github_v3 | |
parent | b89e65cc4e2bb9063371e91c4098e033ea57f385 (diff) |
Diffstat (limited to 'github_v3')
-rw-r--r-- | github_v3/Cargo.toml | 2 | ||||
-rw-r--r-- | github_v3/src/lib.rs | 31 |
2 files changed, 29 insertions, 4 deletions
diff --git a/github_v3/Cargo.toml b/github_v3/Cargo.toml index 5f5247c..5506603 100644 --- a/github_v3/Cargo.toml +++ b/github_v3/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "github_v3" description = "Async GitHub API v3 client" -version = "0.3.1" +version = "0.3.2" authors = ["Kornel <kornel@geekhood.net>"] keywords = ["github", "restful", "api", "async"] categories = ["web-programming", "web-programming::http-client"] diff --git a/github_v3/src/lib.rs b/github_v3/src/lib.rs index a5fc31a..79c5a06 100644 --- a/github_v3/src/lib.rs +++ b/github_v3/src/lib.rs @@ -61,29 +61,53 @@ impl Response { pub struct Builder { client: Arc<ClientInner>, url: String, + query_string_started: bool, } impl Builder { /// Add a constant path to the request, e.g. `.path("users")` /// + /// Inner slashes are OK, but the string must not start or end with a slash. + /// + /// Panics if query string has been added. + /// /// It's appended raw, so must be URL-safe. pub fn path(mut self, url_part: &'static str) -> Self { debug_assert_eq!(url_part, url_part.trim_matches('/')); + assert!(!self.query_string_started); self.url.push('/'); self.url.push_str(url_part); self } - /// Add a user-supplied argument to the request path, e.g. `.path("users").arg(username)` + /// Add a user-supplied argument to the request path, e.g. `.path("users").arg(username)`, + /// or after a call to query(), starts adding fragments to the query string with no delimiters. /// /// The arg is URL-escaped, so it's safe to use any user-supplied data. pub fn arg(mut self, arg: &str) -> Self { - self.url.push('/'); + if !self.query_string_started { + self.url.push('/'); + } self.url.push_str(&urlencoding::encode(arg)); self } + /// Add a raw unescaped query string. The string must *not* start with `?` + /// + /// ```rust + /// # Client::new(None) + /// .get().path("search/users").query("q=").arg(somestring) + /// ``` + pub fn query(mut self, query_string: &str) -> Self { + debug_assert!(!query_string.starts_with('?')); + debug_assert!(!query_string.starts_with('&')); + self.url.push(if self.query_string_started {'&'} else {'?'}); + self.url.push_str(query_string); + self.query_string_started = true; + self + } + /// Make the request pub async fn send(self) -> Result<Response, GHError> { let res = self.client.raw_get(&self.url).await?; @@ -135,11 +159,12 @@ impl Client { /// Make a new request to the API. pub fn get(&self) -> Builder { - let mut url = String::with_capacity(60); + let mut url = String::with_capacity(100); url.push_str("https://api.github.com"); Builder { client: self.inner.clone(), url, + query_string_started: false, } } } |