@@ -27,11 +27,13 @@ final class Client implements ClientInterface
2727 * @param HttpClientInterface $httpClient
2828 */
2929 public function __construct (
30- HttpClientInterface $ httpClient ,
3130 string $ chatworkToken ,
32- string $ apiVersion
31+ string $ apiVersion ,
32+ ?HttpClientInterface $ httpClient = null
3333 ) {
34- $ httpClient ->getConfig ('handler ' )->push (Middleware::mapRequest (fn (RequestInterface $ request ) => $ request ->withHeader ('X-ChatWorkToken ' , $ chatworkToken )));
34+ if ($ httpClient === null ) {
35+ $ httpClient = ClientFactory::createHttpClient ($ chatworkToken );
36+ }
3537 $ this ->apiVersion = $ apiVersion ;
3638 $ this ->httpClient = $ httpClient ;
3739 }
@@ -80,9 +82,11 @@ private function request(string $method, string $path, array $options = []): arr
8082 {
8183 $ path = sprintf ('/%s/%s ' , $ this ->apiVersion , $ path );
8284 try {
83- return json_decode ($ this ->httpClient ->request ($ method , $ path , $ options )->getBody ()->getContents (), true );
85+ return json_decode ($ this ->httpClient ->request ($ method , $ path , $ options )->getBody ()->getContents (), true , 512 , JSON_THROW_ON_ERROR );
8486 } catch (GuzzleException $ e ) {
8587 throw new ClientException (sprintf ('request error. method = %s, path = %s ' , $ method , $ path ), $ e ->getCode (), $ e );
88+ } catch (\JsonException $ e ) {
89+ throw new ClientException (sprintf ('json parse error. method = %s, path = %s ' , $ method , $ path ), $ e ->getCode (), $ e );
8690 }
8791 }
8892}
0 commit comments