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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#[macro_use]
extern crate error_chain;
extern crate reqwest;
#[macro_use]
extern crate serde_derive;
extern crate serde_json;
mod errors;
use errors::*;
use std::io::prelude::*;
use std::fs;
use std::fs::{File, OpenOptions};
use std::path::Path;
pub mod stash;
const BASE_URL: &'static str = "http://api.pathofexile.com";
const CACHE_DIR: &'static str = "cache";
pub trait Endpoint {
fn get_url(&self) -> &str;
}
pub fn cache_body<T: Endpoint>(endpoint: &T) -> Result<String> {
let mut cached_file = CACHE_DIR.to_string();
cached_file.push_str(endpoint.get_url());
cached_file = cached_file.replace("?", "");
let cached_file = Path::new(cached_file.as_str());
if cached_file.exists() {
let mut file = File::open(cached_file)?;
let mut body = String::new();
file.read_to_string(&mut body)?;
Ok(body)
} else {
let body = get_body(endpoint)?;
if let Some(parent) = cached_file.parent() {
if !parent.exists() {
fs::create_dir_all(cached_file.parent().unwrap())
.chain_err(|| "could not create cache directory")?;
}
}
let mut file = OpenOptions::new()
.create_new(true)
.write(true)
.open(cached_file)
.chain_err(|| "could not create cache file")?;
file.write_all(body.as_bytes())?;
Ok(body)
}
}
pub fn get_body<T: Endpoint>(endpoint: &T) -> Result<String> {
let mut result = String::new();
let mut url = BASE_URL.to_string();
url.push_str(endpoint.get_url());
let mut response = reqwest::get(url.as_str()).chain_err(|| "could not get response")?;
response.read_to_string(&mut result).chain_err(|| "could not read body")?;
Ok(result)
}