77use CodeOfDigital \LaravelUrlShortener \Exceptions \InvalidDataException ;
88use CodeOfDigital \LaravelUrlShortener \Exceptions \InvalidResponseException ;
99use CodeOfDigital \LaravelUrlShortener \Exceptions \ShortUrlException ;
10+ use CodeOfDigital \LaravelUrlShortener \Exceptions \TooManyRequestException ;
1011use GuzzleHttp \ClientInterface ;
1112use GuzzleHttp \Exception \RequestException ;
1213use GuzzleHttp \Psr7 \Request ;
@@ -39,14 +40,15 @@ public function shortenAsync($url, array $options = [])
3940 if (!Str::startsWith ($ url , ['http:// ' , 'https:// ' ]))
4041 throw new ShortUrlException ('The given URL must begin with http:// or https:// ' );
4142
42- $ options = array_merge_recursive (Arr::add ($ this ->object , 'query.short ' , urlencode ( $ url) ), ['query ' => $ options ]);
43+ $ options = array_merge_recursive (Arr::add ($ this ->object , 'query.short ' , $ url ), ['query ' => $ options ]);
4344 $ request = new Request ('GET ' , '/api/api.php ' );
4445
4546 return $ this ->client ->sendAsync ($ request , $ options )->then (
4647 function (ResponseInterface $ response ) {
47- $ statusCode = json_decode ($ response ->getBody ()->getContents ())->url ->status ;
48+ $ data = json_decode ($ response ->getBody ()->getContents ());
49+ $ statusCode = $ data ->url ->status ;
4850 if ($ statusCode != 7 ) $ this ->getErrorMessage ($ statusCode );
49- return str_replace ('http:// ' , 'https:// ' , json_decode ( $ response -> getBody ()-> getContents ()) ->url ->shortLink );
51+ return str_replace ('http:// ' , 'https:// ' , $ data ->url ->shortLink );
5052 },
5153 function (RequestException $ e ) {
5254 $ this ->getErrorMessage ($ e ->getCode (), $ e ->getMessage ());
@@ -65,7 +67,9 @@ protected function getErrorMessage($code, $message = null)
6567 throw new InvalidDataException ("The preferred link name is already taken. " );
6668 case 4 :
6769 case 401 :
68- throw new InvalidApiTokenException ("Your API Key is invalid and incorrect. " );
70+ throw new InvalidApiTokenException ($ message ?? "Your API Key is invalid and incorrect. " );
71+ case 429 :
72+ throw new TooManyRequestException ($ message );
6973 case 5 :
7074 throw new InvalidDataException ("The link has not passed validated. There is invalid characters. " );
7175 case 6 :
0 commit comments