nano_get/lib.rs
1//! This crate provides a basic implementation of the HTTP(s) GET Method.
2//!
3//! This uses only the standard Rust Library and has no 3rd party dependencies by default.
4//!
5//! ## Quick Example
6//!
7//! An example usage is shown below:
8//! ```rust
9//! let response = nano_get::get_http("http://example.com");
10//! println!("{}", response);
11//! ```
12//!
13//! ## HTTPS
14//!
15//! A HTTPS version is provided since v0.2.x that depends on OpenSSL & the [Rust OpenSSL wrapper](https://crates.io/crates/openssl) crate.
16//! This can be enabled by the "https" feature flag (which is NOT activated by default).
17//!
18//! This provides you with the `nano_get::get_https` method which has the same signature as
19//! the standard `nano_get::get_http` method.
20//!
21//! ## Unified HTTP GET
22//!
23//! From version 0.2.x, we profide the new unified `nano_get::get` method which activates the
24//! specific version of http/https GET, based on the protocol of the URL.
25//!
26//! However, please note that the https part still depends on the "https" feature flag
27//! (which is NOT activated by default). The `nano_get::get` falls back on the regular http version,
28//! incase the "https" feature flag is not enabled.
29//!
30//! An example usage of the unified get is shown below:
31//! ```rust
32//! let response = nano_get::get("http://dummy.restapiexample.com/api/v1/employees");
33//! println!("{}", response);
34//! ```
35//!
36//! or, with the "https" feature flag enabled and the OpenSSL library present,
37//!
38//! ```rust
39//! let response = nano_get::get("https://www.google.com");
40//! println!("{}", response);
41//! ```
42//!
43//! ## Executing HTTP(s) Requests:
44//!
45//! There are two ways to execute the HTTP(s) requests.
46//!
47//! ### Basic Get
48//!
49//! The basic version, demonstrated by the use of the `nano_get::get` function, which takes a url
50//! and returns the body of the response.
51//!
52//! #### Example
53//! ```rust
54//! let response = nano_get::get("https://www.google.com");
55//! println!("{}", response);
56//! ```
57//!
58//! ### Request-Response based
59//!
60//! Another more fine-grained method exists by using the `nano_get::Request` object.
61//! This gives you access to request headers, optional request body and the execution returns a
62//! `nano_get::Response` object. This allows inspection of HTTP Response codes, response body, etc.
63//!
64//! #### Example
65//! ```rust
66//! use nano_get::Response;
67//! let mut request = nano_get::Request::default_get_request("http://example.com/").unwrap();
68//! let response: Response = request.execute().unwrap();
69//! ```
70//!
71//! For details, check the `Request` and `Response` structure documentation.
72pub use http::get_http;
73#[cfg(feature = "https")]
74pub use https::get_https;
75pub use request::{Header, Request};
76pub use response::{Response, ResponseStatus, StatusCode};
77pub use url::{ToUrl, Url};
78
79mod url;
80mod http;
81mod request;
82mod response;
83mod errors;
84
85#[cfg(feature = "https")]
86mod https;
87
88/// This is a unified function for the HTTP GET method.
89///
90/// This calls the http version of GET provided in this crate by default.
91///
92/// If the "https" feature flag is enabled, then this calls the get_https method, if the protocol is
93/// https, else it calls the get_http method. The "https" feature flag is NOT enabled by default.
94///
95/// This function is a wrapper around the http/https get methods provided in this crate.
96///
97/// If you require manual control of the method that is called, you should use the specific method.
98///
99/// This can be called on anything that implements the ToUrl Trait.
100#[allow(unused_variables)]
101pub fn get<U: ToUrl>(url: U) -> String {
102 let url = url.to_url().unwrap();
103 let protocol = &url.protocol[..];
104
105 #[cfg(feature = "https")] {
106 if protocol.eq("https") {
107 return get_https(&url);
108 }
109 }
110
111 get_http(&url)
112}
113
114#[cfg(test)]
115mod tests {
116 use url;
117
118 use super::*;
119
120 #[test]
121 fn test_proto_parse_http() {
122 let url_str = "http://example.com/?a=1&b=2&c=3".to_string();
123 let (a, b) = url::parse_proto(url_str, None);
124 println!("{}, {}", a, b);
125 assert_eq!(a, "http".to_string());
126 }
127
128 #[test]
129 fn test_proto_parse_https() {
130 let url_str = "https://example.com/?a=1&b=2&c=3".to_string();
131 let (a, b) = url::parse_proto(url_str, None);
132 println!("{}, {}", a, b);
133 assert_eq!(a, "https".to_string());
134 }
135
136 #[test]
137 fn test_proto_parse_ftp() {
138 let url_str = "ftp://example.com/?a=1&b=2&c=3".to_string();
139 let (a, b) = url::parse_proto(url_str, None);
140 println!("{}, {}", a, b);
141 assert_eq!(a, "ftp".to_string());
142 }
143
144 #[test]
145 fn test_proto_parse_none() {
146 let url_str = "example.com/?a=1&b=2&c=3".to_string();
147 let (a, b) = url::parse_proto(url_str, None);
148 println!("{}, {}", a, b);
149 assert_eq!(a, "http".to_string());
150 }
151}