@@ -44,23 +44,52 @@ public function __construct(
4444 LoggerInterface $ logger ,
4545 ExistingJobFactory $ existingJobFactory ,
4646 string $ internalQueueApiUrl ,
47- string $ internalQueueToken ,
47+ ?string $ internalQueueToken ,
48+ ?string $ storageApiToken ,
4849 array $ options = []
4950 ) {
50- $ validator = Validation::createValidator ();
51- $ errors = $ validator ->validate ($ internalQueueApiUrl , [new Url ()]);
52- $ errors ->addAll (
53- $ validator ->validate ($ internalQueueToken , [new NotBlank ()])
54- );
51+ $ this ->validateConfiguration ($ internalQueueApiUrl , $ internalQueueToken , $ storageApiToken , $ options );
5552 if (!empty ($ options ['backoffMaxTries ' ])) {
56- $ errors ->addAll ($ validator ->validate ($ options ['backoffMaxTries ' ], [new Range (['min ' => 0 , 'max ' => 100 ])]));
5753 $ options ['backoffMaxTries ' ] = intval ($ options ['backoffMaxTries ' ]);
5854 } else {
5955 $ options ['backoffMaxTries ' ] = self ::DEFAULT_BACKOFF_RETRIES ;
6056 }
6157 if (empty ($ options ['userAgent ' ])) {
6258 $ options ['userAgent ' ] = self ::DEFAULT_USER_AGENT ;
6359 }
60+
61+ $ this ->guzzle = $ this ->initClient ($ internalQueueApiUrl , $ internalQueueToken , $ storageApiToken , $ options );
62+ $ this ->existingJobFactory = $ existingJobFactory ;
63+ $ this ->logger = $ logger ;
64+ }
65+
66+ private function validateConfiguration (
67+ string $ internalQueueApiUrl ,
68+ ?string $ internalQueueToken ,
69+ ?string $ storageApiToken ,
70+ array $ options
71+ ): void {
72+ $ validator = Validation::createValidator ();
73+ $ errors = $ validator ->validate ($ internalQueueApiUrl , [new Url ()]);
74+ if ($ internalQueueToken === null && $ storageApiToken === null ) {
75+ throw new ClientException ('Both InternalApiToken and StorageAPIToken are empty. ' );
76+ }
77+ if ($ internalQueueToken !== null && $ storageApiToken !== null ) {
78+ throw new ClientException ('Both InternalApiToken and StorageAPIToken are non-empty. Use only one. ' );
79+ }
80+ if ($ internalQueueToken !== null ) {
81+ $ errors ->addAll (
82+ $ validator ->validate ($ internalQueueToken , [new NotBlank ()])
83+ );
84+ }
85+ if ($ storageApiToken !== null ) {
86+ $ errors ->addAll (
87+ $ validator ->validate ($ storageApiToken , [new NotBlank ()])
88+ );
89+ }
90+ if (!empty ($ options ['backoffMaxTries ' ])) {
91+ $ errors ->addAll ($ validator ->validate ($ options ['backoffMaxTries ' ], [new Range (['min ' => 0 , 'max ' => 100 ])]));
92+ }
6493 if ($ errors ->count () !== 0 ) {
6594 $ messages = '' ;
6695 /** @var ConstraintViolationInterface $error */
@@ -69,9 +98,6 @@ public function __construct(
6998 }
7099 throw new ClientException ('Invalid parameters when creating client: ' . $ messages );
71100 }
72- $ this ->guzzle = $ this ->initClient ($ internalQueueApiUrl , $ internalQueueToken , $ options );
73- $ this ->existingJobFactory = $ existingJobFactory ;
74- $ this ->logger = $ logger ;
75101 }
76102
77103 public function addJobUsage (string $ jobId , array $ usage ): void
@@ -316,8 +342,12 @@ private function createDefaultDecider(int $maxRetries): Closure
316342 };
317343 }
318344
319- private function initClient (string $ url , string $ token , array $ options = []): GuzzleClient
320- {
345+ private function initClient (
346+ string $ url ,
347+ ?string $ internalToken ,
348+ ?string $ storageToken ,
349+ array $ options = []
350+ ): GuzzleClient {
321351 // Initialize handlers (start with those supplied in constructor)
322352 if (isset ($ options ['handler ' ]) && is_callable ($ options ['handler ' ])) {
323353 $ handlerStack = HandlerStack::create ($ options ['handler ' ]);
@@ -328,11 +358,16 @@ private function initClient(string $url, string $token, array $options = []): Gu
328358 $ handlerStack ->push (Middleware::retry ($ this ->createDefaultDecider ($ options ['backoffMaxTries ' ])));
329359 // Set handler to set default headers
330360 $ handlerStack ->push (Middleware::mapRequest (
331- function (RequestInterface $ request ) use ($ token , $ options ) {
332- return $ request
333- ->withHeader ('User-Agent ' , $ options ['userAgent ' ])
334- ->withHeader ('X-JobQueue-InternalApi-Token ' , $ token )
335- ->withHeader ('Content-type ' , 'application/json ' );
361+ function (RequestInterface $ request ) use ($ internalToken , $ storageToken , $ options ) {
362+ $ request = $ request ->withHeader ('User-Agent ' , $ options ['userAgent ' ])
363+ ->withHeader ('Content-type ' , 'application/json ' );
364+ if ($ internalToken !== null ) {
365+ $ request = $ request ->withHeader ('X-JobQueue-InternalApi-Token ' , $ internalToken );
366+ }
367+ if ($ storageToken !== null ) {
368+ $ request = $ request ->withHeader ('X-StorageApi-Token ' , $ storageToken );
369+ }
370+ return $ request ;
336371 }
337372 ));
338373 // Set client logger
0 commit comments