5
5
6
6
namespace Torchlight ;
7
7
8
+ use Illuminate \Http \Client \Pool ;
8
9
use Illuminate \Support \Arr ;
9
10
use Illuminate \Support \Collection ;
10
- use Illuminate \Support \Facades \Cache ;
11
11
use Illuminate \Support \Facades \Http ;
12
12
use Throwable ;
13
13
use Torchlight \Exceptions \ConfigurationException ;
@@ -42,26 +42,35 @@ public function highlight($blocks)
42
42
43
43
protected function request (Collection $ blocks )
44
44
{
45
- $ blocks = $ this ->collectionOfBlocks ($ blocks );
46
-
47
- $ host = Torchlight::config ('host ' , 'https://api.torchlight.dev ' );
45
+ $ error = false ;
48
46
49
47
try {
50
- $ response = Http::timeout (5 )
51
- ->withToken ($ this ->getToken ())
52
- ->post ($ host . '/highlight ' , [
53
- 'blocks ' => $ this ->blocksAsRequestParam ($ blocks )->values ()->toArray (),
54
- ])
55
- ->json ();
48
+ $ response = $ this ->collectionOfBlocks ($ blocks )
49
+ ->chunk (Torchlight::config ('request_chunk_size ' , 15 ))
50
+ ->pipe (function ($ chunks ) {
51
+ return collect ($ this ->requestChunks ($ chunks ));
52
+ })
53
+ ->map (function ($ response ) use (&$ error ) {
54
+ if ($ response instanceof Throwable) {
55
+ $ error = $ response ;
56
+
57
+ return [];
58
+ }
59
+
60
+ if ($ response ->failed ()) {
61
+ $ error = $ response ->toException ();
62
+
63
+ return [];
64
+ }
65
+
66
+ return Arr::get ($ response ->json (), 'blocks ' , []);
67
+ })
68
+ ->flatten (1 );
56
69
} catch (Throwable $ e ) {
57
- $ response = [
58
- 'error ' => $ e ->getMessage ()
59
- ];
70
+ $ this ->throwUnlessProduction ($ e );
60
71
}
61
72
62
- $ this ->potentiallyThrowRequestException ($ response );
63
-
64
- $ response = collect (Arr::get ($ response , 'blocks ' , []))->keyBy ('id ' );
73
+ $ response = collect ($ response )->keyBy ('id ' );
65
74
66
75
$ blocks ->each (function (Block $ block ) use ($ response ) {
67
76
$ blockFromResponse = Arr::get ($ response , "{$ block ->id ()}" , []);
@@ -84,9 +93,28 @@ protected function request(Collection $blocks)
84
93
// Only store the ones we got back from the API.
85
94
$ this ->setCacheFromBlocks ($ blocks , $ response ->keys ());
86
95
96
+ $ this ->potentiallyThrowRequestException ($ error );
97
+
87
98
return $ blocks ;
88
99
}
89
100
101
+ protected function requestChunks ($ chunks )
102
+ {
103
+ return Http::pool (function (Pool $ pool ) use ($ chunks ) {
104
+ $ host = Torchlight::config ('host ' , 'https://api.torchlight.dev ' );
105
+ $ timeout = Torchlight::config ('request_timeout ' , 5 );
106
+
107
+ $ chunks ->each (function ($ blocks ) use ($ pool , $ host , $ timeout ) {
108
+ $ pool ->timeout ($ timeout )
109
+ ->baseUrl ($ host )
110
+ ->withToken ($ this ->getToken ())
111
+ ->post ('highlight ' , [
112
+ 'blocks ' => $ this ->blocksAsRequestParam ($ blocks )->values ()->toArray (),
113
+ ]);
114
+ });
115
+ });
116
+ }
117
+
90
118
protected function collectionOfBlocks ($ blocks )
91
119
{
92
120
return collect ($ blocks )->each (function ($ block ) {
@@ -109,10 +137,10 @@ protected function getToken()
109
137
return $ token ;
110
138
}
111
139
112
- protected function potentiallyThrowRequestException ($ response )
140
+ protected function potentiallyThrowRequestException ($ exception )
113
141
{
114
- if ($ error = Arr:: get ( $ response , ' error ' ) ) {
115
- $ this ->throwUnlessProduction (new RequestException ($ error ));
142
+ if ($ exception ) {
143
+ $ this ->throwUnlessProduction (new RequestException ($ exception -> getMessage () ));
116
144
}
117
145
}
118
146
0 commit comments