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
use async_trait::async_trait;
use thiserror::Error;
use crate::{coords::WorldTileCoords, io::source_type::SourceType};
pub type HTTPClientFactory<HC> = dyn Fn() -> HC;
#[cfg_attr(not(feature = "thread-safe-futures"), async_trait(?Send))]
#[cfg_attr(feature = "thread-safe-futures", async_trait)]
pub trait HttpClient: Clone + Sync + Send + 'static {
async fn fetch(&self, url: &str) -> Result<Vec<u8>, SourceFetchError>;
}
#[derive(Clone)]
pub struct HttpSourceClient<HC>
where
HC: HttpClient,
{
inner_client: HC,
}
#[derive(Error, Debug)]
#[error("failed to fetch from source")]
pub struct SourceFetchError(#[source] pub Box<dyn std::error::Error>);
#[derive(Clone)]
pub struct SourceClient<HC>
where
HC: HttpClient,
{
http: HttpSourceClient<HC>,
}
impl<HC> SourceClient<HC>
where
HC: HttpClient,
{
pub fn new(http: HttpSourceClient<HC>) -> Self {
Self { http }
}
pub async fn fetch(
&self,
coords: &WorldTileCoords,
source_type: &SourceType,
) -> Result<Vec<u8>, SourceFetchError> {
self.http.fetch(coords, source_type).await
}
}
impl<HC> HttpSourceClient<HC>
where
HC: HttpClient,
{
pub fn new(http_client: HC) -> Self {
Self {
inner_client: http_client,
}
}
pub async fn fetch(
&self,
coords: &WorldTileCoords,
source_type: &SourceType,
) -> Result<Vec<u8>, SourceFetchError> {
self.inner_client
.fetch(source_type.format(coords).as_str())
.await
}
}