6
6
namespace Torchlight ;
7
7
8
8
use GuzzleHttp \Promise \Promise ;
9
+ use Illuminate \Http \Client \ConnectionException ;
9
10
use Illuminate \Support \Arr ;
10
11
use Illuminate \Support \Collection ;
11
12
use Illuminate \Support \Facades \Http ;
@@ -42,35 +43,33 @@ public function highlight($blocks)
42
43
43
44
protected function request (Collection $ blocks )
44
45
{
45
- $ error = false ;
46
- $ response = [];
47
-
48
46
try {
49
- $ response = $ this ->collectionOfBlocks ($ blocks )
50
- ->chunk (Torchlight::config ('request_chunk_size ' , 15 ))
51
- ->pipe (function ($ chunks ) {
52
- return collect ($ this ->requestChunks ($ chunks ));
53
- })
54
- ->map (function ($ response ) use (&$ error ) {
55
- if ($ response instanceof Throwable) {
56
- $ error = $ response ;
57
-
58
- return [];
59
- }
60
-
61
- if ($ response ->failed ()) {
62
- $ error = $ response ->toException ();
63
-
64
- return [];
65
- }
66
-
67
- return Arr::get ($ response ->json (), 'blocks ' , []);
68
- })
69
- ->flatten (1 );
47
+ $ host = Torchlight::config ('host ' , 'https://api.torchlight.dev ' );
48
+ $ timeout = Torchlight::config ('request_timeout ' , 5 );
49
+
50
+ $ response = Http::baseUrl ($ host )
51
+ ->timeout ($ timeout )
52
+ ->withToken ($ this ->getToken ())
53
+ ->post ('highlight ' , [
54
+ 'blocks ' => $ this ->blocksAsRequestParam ($ blocks )->values ()->toArray (),
55
+ ]);
56
+
57
+ if ($ response ->failed ()) {
58
+ $ this ->potentiallyThrowRequestException ($ response ->toException ());
59
+ $ response = [];
60
+ } else {
61
+ $ response = $ response ->json ();
62
+ }
63
+
70
64
} catch (Throwable $ e ) {
71
- $ this ->throwUnlessProduction ($ e );
65
+ $ e instanceof ConnectionException
66
+ ? $ this ->potentiallyThrowRequestException ($ e )
67
+ : $ this ->throwUnlessProduction ($ e );
68
+
69
+ $ response = [];
72
70
}
73
71
72
+ $ response = Arr::get ($ response , 'blocks ' , []);
74
73
$ response = collect ($ response )->keyBy ('id ' );
75
74
76
75
$ blocks ->each (function (Block $ block ) use ($ response ) {
@@ -94,8 +93,6 @@ protected function request(Collection $blocks)
94
93
// Only store the ones we got back from the API.
95
94
$ this ->setCacheFromBlocks ($ blocks , $ response ->keys ());
96
95
97
- $ this ->potentiallyThrowRequestException ($ error );
98
-
99
96
return $ blocks ;
100
97
}
101
98
@@ -148,7 +145,9 @@ protected function getToken()
148
145
protected function potentiallyThrowRequestException ($ exception )
149
146
{
150
147
if ($ exception ) {
151
- $ this ->throwUnlessProduction (new RequestException ($ exception ->getMessage ()));
148
+ $ wrapped = new RequestException ('A Torchlight request exception has occurred. ' , 0 , $ exception );
149
+
150
+ $ this ->throwUnlessProduction ($ wrapped );
152
151
}
153
152
}
154
153
0 commit comments