@@ -44,9 +44,9 @@ abstract class AbstractApi
4444 private $ credentialProvider ;
4545
4646 /**
47- * @var Signer
47+ * @var Signer[]
4848 */
49- private $ signer ;
49+ private $ signers ;
5050
5151 /**
5252 * @param Configuration|array $configuration
@@ -78,10 +78,10 @@ final public function getConfiguration(): Configuration
7878 final public function presign (Input $ input , ?\DateTimeImmutable $ expires = null ): string
7979 {
8080 $ request = $ input ->request ();
81- $ request ->setEndpoint ($ this ->getEndpoint ($ request ->getUri (), $ request ->getQuery ()));
81+ $ request ->setEndpoint ($ this ->getEndpoint ($ request ->getUri (), $ request ->getQuery (), $ input -> getRegion () ));
8282
8383 if (null !== $ credentials = $ this ->credentialProvider ->getCredentials ($ this ->configuration )) {
84- $ this ->getSigner ()->presign ($ request , $ credentials , new RequestContext (['expirationDate ' => $ expires ]));
84+ $ this ->getSigner ($ input -> getRegion () )->presign ($ request , $ credentials , new RequestContext (['expirationDate ' => $ expires ]));
8585 }
8686
8787 return $ request ->getEndpoint ();
@@ -95,10 +95,10 @@ abstract protected function getSignatureScopeName(): string;
9595
9696 final protected function getResponse (Request $ request , ?RequestContext $ context = null ): Response
9797 {
98- $ request ->setEndpoint ($ this ->getEndpoint ($ request ->getUri (), $ request ->getQuery ()));
98+ $ request ->setEndpoint ($ this ->getEndpoint ($ request ->getUri (), $ request ->getQuery (), $ context ? $ context -> getRegion () : null ));
9999
100100 if (null !== $ credentials = $ this ->credentialProvider ->getCredentials ($ this ->configuration )) {
101- $ this ->getSigner ()->sign ($ request , $ credentials , $ context ?? new RequestContext ());
101+ $ this ->getSigner ($ context ? $ context -> getRegion () : null )->sign ($ request , $ credentials , $ context ?? new RequestContext ());
102102 }
103103
104104 $ length = $ request ->getBody ()->length ();
@@ -142,11 +142,11 @@ protected function getSignerFactories(): array
142142 * @param string $uri or path
143143 * @param array $query parameters that should go in the query string
144144 */
145- private function getEndpoint (string $ uri , array $ query ): string
145+ private function getEndpoint (string $ uri , array $ query, ? string $ region ): string
146146 {
147147 /** @psalm-suppress PossiblyNullArgument */
148148 $ endpoint = strtr ($ this ->configuration ->get ('endpoint ' ), [
149- '%region% ' => $ this ->configuration ->get ('region ' ),
149+ '%region% ' => $ region ?? $ this ->configuration ->get ('region ' ),
150150 '%service% ' => $ this ->getServiceCode (),
151151 ]);
152152 $ endpoint .= $ uri ;
@@ -157,19 +157,20 @@ private function getEndpoint(string $uri, array $query): string
157157 return $ endpoint . (false === \strpos ($ endpoint , '? ' ) ? '? ' : '& ' ) . http_build_query ($ query );
158158 }
159159
160- private function getSigner ()
160+ private function getSigner (? string $ region )
161161 {
162- if (null === $ this ->signer ) {
162+ if (! isset ( $ this ->signers [ $ region ]) ) {
163163 /** @var string $region */
164- $ region = $ this ->configuration ->get (Configuration::OPTION_REGION );
164+ $ region = $ region ?? $ this ->configuration ->get (Configuration::OPTION_REGION );
165165 $ factories = $ this ->getSignerFactories ();
166166 if (!isset ($ factories [$ signatureVersion = $ this ->getSignatureVersion ()])) {
167167 throw new InvalidArgument (sprintf ('The signature "%s" is not implemented. ' , $ signatureVersion ));
168168 }
169169
170- $ this ->signer = $ factories [$ signatureVersion ]($ this ->getSignatureScopeName (), $ region );
170+ $ this ->signers [ $ region ] = $ factories [$ signatureVersion ]($ this ->getSignatureScopeName (), $ region );
171171 }
172172
173- return $ this ->signer ;
173+ /** @psalm-suppress PossiblyNullArrayOffset */
174+ return $ this ->signers [$ region ];
174175 }
175176}
0 commit comments