1717use Psr \Http \Message \RequestInterface ;
1818use Psr \Http \Message \ResponseInterface ;
1919use Psr \Log \LoggerInterface ;
20- use Symfony \Component \Validator \Constraints \NotBlank ;
2120use Symfony \Component \Validator \Constraints \Range ;
2221use Symfony \Component \Validator \Constraints \Url ;
2322use Symfony \Component \Validator \ConstraintViolationInterface ;
@@ -32,15 +31,12 @@ abstract class Client
3231 protected GuzzleClient $ guzzle ;
3332
3433 public function __construct (
35- string $ notificationApiUrl ,
36- string $ token ,
34+ string $ baseUrl ,
35+ ? string $ token ,
3736 array $ options = []
3837 ) {
3938 $ validator = Validation::createValidator ();
40- $ errors = $ validator ->validate ($ notificationApiUrl , [new Url ()]);
41- $ errors ->addAll (
42- $ validator ->validate ($ token , [new NotBlank ()])
43- );
39+ $ errors = $ validator ->validate ($ baseUrl , [new Url ()]);
4440 if (!empty ($ options ['backoffMaxTries ' ])) {
4541 $ errors ->addAll ($ validator ->validate ($ options ['backoffMaxTries ' ], [new Range (['min ' => 0 , 'max ' => 100 ])]));
4642 $ options ['backoffMaxTries ' ] = intval ($ options ['backoffMaxTries ' ]);
@@ -58,7 +54,7 @@ public function __construct(
5854 }
5955 throw new NotificationClientException ('Invalid parameters when creating client: ' . $ messages );
6056 }
61- $ this ->guzzle = $ this ->initClient ($ notificationApiUrl , $ token , $ options );
57+ $ this ->guzzle = $ this ->initClient ($ baseUrl , $ token , $ options );
6258 }
6359
6460 private function createDefaultDecider (int $ maxRetries ): Closure
@@ -79,9 +75,9 @@ private function createDefaultDecider(int $maxRetries): Closure
7975 };
8076 }
8177
82- abstract protected function getTokenHeaderName (): string ;
78+ abstract protected function getTokenHeaderName (): ? string ;
8379
84- private function initClient (string $ url , string $ token , array $ options = []): GuzzleClient
80+ private function initClient (string $ url , ? string $ token , array $ options = []): GuzzleClient
8581 {
8682 // Initialize handlers (start with those supplied in constructor)
8783 $ handlerStack = HandlerStack::create ($ options ['handler ' ] ?? null );
@@ -90,10 +86,13 @@ private function initClient(string $url, string $token, array $options = []): Gu
9086 // Set handler to set default headers
9187 $ handlerStack ->push (Middleware::mapRequest (
9288 function (RequestInterface $ request ) use ($ token , $ options ) {
93- return $ request
89+ $ request = $ request
9490 ->withHeader ('User-Agent ' , $ options ['userAgent ' ])
95- ->withHeader ($ this ->getTokenHeaderName (), $ token )
9691 ->withHeader ('Content-type ' , 'application/json ' );
92+ if ($ this ->getTokenHeaderName ()) {
93+ $ request = $ request ->withHeader ((string ) $ this ->getTokenHeaderName (), (string ) $ token );
94+ }
95+ return $ request ;
9796 }
9897 ));
9998 // Set client logger
0 commit comments