Laravel AT Protocol Client (alpha & unstable)

Auto-throw AtpResponseException on failed API responses

Changed files
+14
src
Client
Http
+5
src/Client/Public/PublicClient.php
··· 3 3 namespace SocialDept\AtpClient\Client\Public; 4 4 5 5 use Illuminate\Support\Facades\Http; 6 + use SocialDept\AtpClient\Exceptions\AtpResponseException; 6 7 use SocialDept\AtpClient\Http\Response; 7 8 8 9 class PublicClient ··· 17 18 $params = array_filter($params, fn ($v) => !is_null($v)); 18 19 19 20 $response = Http::get($url, $params); 21 + 22 + if ($response->failed() || isset($response->json()['error'])) { 23 + throw AtpResponseException::fromResponse($response, $endpoint); 24 + } 20 25 21 26 return new Response($response); 22 27 }
+9
src/Http/HasHttp.php
··· 7 7 use InvalidArgumentException; 8 8 use SocialDept\AtpClient\Auth\ScopeChecker; 9 9 use SocialDept\AtpClient\Enums\Scope; 10 + use SocialDept\AtpClient\Exceptions\AtpResponseException; 10 11 use SocialDept\AtpClient\Exceptions\ValidationException; 11 12 use SocialDept\AtpClient\Session\Session; 12 13 use SocialDept\AtpClient\Session\SessionManager; ··· 44 45 'DELETE' => $request->delete($url, $params), 45 46 default => throw new InvalidArgumentException("Unsupported method: {$method}"), 46 47 }; 48 + 49 + if ($response->failed() || isset($response->json()['error'])) { 50 + throw AtpResponseException::fromResponse($response, $endpoint); 51 + } 47 52 48 53 if (config('atp-client.schema_validation') && Schema::exists($endpoint)) { 49 54 $this->validateResponse($endpoint, $response); ··· 129 134 $response = $this->buildAuthenticatedRequest($session, $url, 'POST') 130 135 ->withBody($data, $mimeType) 131 136 ->post($url); 137 + 138 + if ($response->failed() || isset($response->json()['error'])) { 139 + throw AtpResponseException::fromResponse($response, $endpoint); 140 + } 132 141 133 142 return new Response($response); 134 143 }