@@ -3,10 +3,12 @@ use std::collections::HashMap;
3
3
use chrono:: TimeZone ;
4
4
use error_chain:: bail;
5
5
6
- use crate :: aoc_api :: { AocApi , ResponseStatus } ;
6
+ use crate :: domain :: ports :: { AocClient , GetLeaderboard , InputCache } ;
7
7
use crate :: domain:: { errors:: * , DurationString , Submission , SubmissionStatus } ;
8
- use crate :: infrastructure:: { CliDisplay , Configuration } ;
9
- use crate :: input_cache:: InputCache ;
8
+ use crate :: infrastructure:: aoc_api:: aoc_client_impl:: ResponseStatus ;
9
+ use crate :: infrastructure:: aoc_api:: AocApi ;
10
+ use crate :: infrastructure:: { CliDisplay , FileInputCache } ;
11
+ use crate :: infrastructure:: { Configuration , HttpDescription } ;
10
12
use crate :: submission_history:: SubmissionHistory ;
11
13
12
14
#[ derive( Debug , Default ) ]
@@ -31,7 +33,7 @@ impl Driver {
31
33
bail ! ( "The input is not released yet" ) ;
32
34
}
33
35
34
- match InputCache :: load ( year, day) {
36
+ match FileInputCache :: load ( year, day) {
35
37
Ok ( input) => return Ok ( input) ,
36
38
Err ( e) => match e {
37
39
Error ( ErrorKind :: NoCacheFound ( message) , _) => {
@@ -46,10 +48,11 @@ impl Driver {
46
48
} ,
47
49
} ;
48
50
49
- let aoc_api = AocApi :: new ( & self . configuration ) ;
51
+ let http_client = AocApi :: prepare_http_client ( & self . configuration ) ;
52
+ let aoc_api = AocApi :: new ( http_client, self . configuration . clone ( ) ) ;
50
53
let input = aoc_api. get_input ( & year, & day) ;
51
54
if input. status == ResponseStatus :: Ok {
52
- if InputCache :: cache ( & input. body , year, day) . is_err ( ) {
55
+ if FileInputCache :: save ( & input. body , year, day) . is_err ( ) {
53
56
eprintln ! ( "Failed saving the input to the cache" ) ;
54
57
}
55
58
} else {
@@ -65,7 +68,8 @@ impl Driver {
65
68
part : crate :: domain:: RiddlePart ,
66
69
answer : String ,
67
70
) -> Result < ( ) > {
68
- let aoc_api = AocApi :: new ( & self . configuration ) ;
71
+ let http_client = AocApi :: prepare_http_client ( & self . configuration ) ;
72
+ let aoc_api = AocApi :: new ( http_client, self . configuration . clone ( ) ) ;
69
73
70
74
let mut cache: Option < SubmissionHistory > = match SubmissionHistory :: from_cache ( & year, & day)
71
75
{
@@ -127,16 +131,17 @@ impl Driver {
127
131
}
128
132
129
133
pub fn clear_cache ( & self ) -> Result < ( ) > {
130
- InputCache :: clear ( ) . chain_err ( || "Failed to clear the input cache" ) ?;
134
+ FileInputCache :: clear ( ) . chain_err ( || "Failed to clear the input cache" ) ?;
131
135
SubmissionHistory :: clear ( ) . chain_err ( || "Failed to clear the submission history cache" ) ?;
132
136
Ok ( ( ) )
133
137
}
134
138
135
139
/// Returns the description of the riddles
136
140
pub fn get_description ( & self , year : u16 , day : u8 ) -> Result < String > {
137
- let aoc_api = AocApi :: new ( & self . configuration ) ;
141
+ let http_client = AocApi :: prepare_http_client ( & self . configuration ) ;
142
+ let aoc_api = AocApi :: new ( http_client, self . configuration . clone ( ) ) ;
138
143
Ok ( aoc_api
139
- . get_description ( & year, & day) ?
144
+ . get_description :: < HttpDescription > ( & year, & day) ?
140
145
. cli_fmt ( & self . configuration ) )
141
146
}
142
147
@@ -180,6 +185,14 @@ impl Driver {
180
185
}
181
186
Ok ( directories)
182
187
}
188
+
189
+ pub fn get_leaderboard ( & self , year : u16 ) -> Result < String > {
190
+ let http_client = AocApi :: prepare_http_client ( & self . configuration ) ;
191
+ let aoc_client = AocApi :: new ( http_client, self . configuration . clone ( ) ) ;
192
+ let leaderboard = aoc_client. get_leaderboard ( year) ?;
193
+
194
+ Ok ( leaderboard. cli_fmt ( & self . configuration ) )
195
+ }
183
196
}
184
197
185
198
#[ cfg( test) ]
0 commit comments