66namespace Torchlight ;
77
88use GuzzleHttp \Promise \Promise ;
9+ use Illuminate \Http \Client \ConnectionException ;
910use Illuminate \Support \Arr ;
1011use Illuminate \Support \Collection ;
1112use Illuminate \Support \Facades \Http ;
@@ -42,35 +43,33 @@ public function highlight($blocks)
4243
4344 protected function request (Collection $ blocks )
4445 {
45- $ error = false ;
46- $ response = [];
47-
4846 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+
7064 } catch (Throwable $ e ) {
71- $ this ->throwUnlessProduction ($ e );
65+ $ e instanceof ConnectionException
66+ ? $ this ->potentiallyThrowRequestException ($ e )
67+ : $ this ->throwUnlessProduction ($ e );
68+
69+ $ response = [];
7270 }
7371
72+ $ response = Arr::get ($ response , 'blocks ' , []);
7473 $ response = collect ($ response )->keyBy ('id ' );
7574
7675 $ blocks ->each (function (Block $ block ) use ($ response ) {
@@ -94,8 +93,6 @@ protected function request(Collection $blocks)
9493 // Only store the ones we got back from the API.
9594 $ this ->setCacheFromBlocks ($ blocks , $ response ->keys ());
9695
97- $ this ->potentiallyThrowRequestException ($ error );
98-
9996 return $ blocks ;
10097 }
10198
@@ -148,7 +145,9 @@ protected function getToken()
148145 protected function potentiallyThrowRequestException ($ exception )
149146 {
150147 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 );
152151 }
153152 }
154153
0 commit comments