January 2019
Intermediate to advanced
520 pages
14h 32m
English
Add the following struct to your utils.rs source file:
pub struct WebApi { client: Client, url: String, jar: CookieJar,}
It has three fields—an HTTP Client; a base url, which is used to construct full URLs with added paths; and a CookieJar instance to keep cookie values between requests.
The constructor of this struct takes a URL and builds a Client instance with redirects disabled:
impl WebApi { fn new(url: &str) -> Self { let client = Client::builder() .redirect(RedirectPolicy::none()) .build() .unwrap(); Self { client, url: url.into(), jar: CookieJar::new(), } }}
We can add shortcuts to create instances of WebApi for the specific microservice of our application:
pub fn users() -> Self { WebApi::new(USERS) ...Read now
Unlock full access