summaryrefslogtreecommitdiff
path: root/game_server/src/backend_connection.rs
blob: 9307c4a1eb5573d3c96e5e51579b05daa3ff831f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
use reqwest::{Response, Client, Url, UrlError, Error as ReqError};

pub struct BackendConnection {
    host: String,
    client: Client,
    last_response: Option<Result<Response, ReqError>>
}

impl BackendConnection {
    pub fn new(host: &str) -> Self {
        BackendConnection {
            host: host.to_string(),
            client: Client::new(),
            last_response: None
        }
    }

    pub fn request(&mut self, location: &str) -> Result<(), UrlError> {
        Ok(self.last_response =
                Some(self.client.get(Url::parse(&format!("{}{}", self.host, location))?)
                           .send()))
    }

    pub fn get_response(&self) -> &Option<Result<Response, ReqError>> {
        &self.last_response
    }
    
    pub fn host_name<'a>(&'a self) -> &'a str {
        &self.host
    }
}