|
15 | 15 |
|
16 | 16 | use GuzzleHttp\ClientInterface;
|
17 | 17 | use GuzzleHttp\Exception\ClientException;
|
| 18 | +use GuzzleHttp\Pool; |
| 19 | +use GuzzleHttp\Psr7\Request; |
| 20 | +use Psr\Http\Message\ResponseInterface; |
18 | 21 | use Symfony\Component\Console\Exception\RuntimeException;
|
19 | 22 | use Tightenco\Collect\Support\Collection;
|
20 | 23 | use Ymir\Cli\Exception\ApiClientException;
|
@@ -712,13 +715,23 @@ public function getSecrets(int $projectId, string $environment): Collection
|
712 | 715 | */
|
713 | 716 | public function getSignedAssetRequests(int $deploymentId, array $assets): Collection
|
714 | 717 | {
|
715 |
| - $requests = $this->request('post', "/deployments/{$deploymentId}/signed-assets", ['assets' => $assets]); |
716 |
| - |
717 |
| - if (!empty($assets) && empty($requests)) { |
718 |
| - throw new RuntimeException('Unable to get authorized asset requests from the Ymir API'); |
719 |
| - } |
| 718 | + $requests = function (array $assets) use ($deploymentId) { |
| 719 | + foreach (array_chunk($assets, 500) as $chunkedAssets) { |
| 720 | + yield $this->createRequest('post', "/deployments/{$deploymentId}/signed-assets", ['assets' => $chunkedAssets]); |
| 721 | + } |
| 722 | + }; |
| 723 | + $signedAssetRequests = []; |
| 724 | + |
| 725 | + $pool = new Pool($this->client, $requests($assets), [ |
| 726 | + 'concurrency' => 10, |
| 727 | + 'fulfilled' => function (ResponseInterface $response) use (&$signedAssetRequests) { |
| 728 | + $signedAssetRequests[] = $this->decodeResponse($response); |
| 729 | + }, |
| 730 | + 'options' => ['verify' => false], |
| 731 | + ]); |
| 732 | + $pool->promise()->wait(); |
720 | 733 |
|
721 |
| - return $requests; |
| 734 | + return collect($signedAssetRequests)->collapse(); |
722 | 735 | }
|
723 | 736 |
|
724 | 737 | /**
|
@@ -879,35 +892,42 @@ public function validateProjectConfiguration(ProjectConfiguration $projectConfig
|
879 | 892 | }
|
880 | 893 |
|
881 | 894 | /**
|
882 |
| - * Send a request to the Ymir API. |
| 895 | + * Create a PSR request object. |
883 | 896 | */
|
884 |
| - private function request(string $method, string $uri, array $body = []): Collection |
| 897 | + private function createRequest(string $method, string $uri, array $body = []): Request |
885 | 898 | {
|
886 |
| - $method = strtolower($method); |
887 |
| - $options = [ |
888 |
| - 'base_uri' => $this->baseUrl, |
889 |
| - 'headers' => [ |
890 |
| - 'Accept' => 'application/json', |
891 |
| - 'Content-Type' => 'application/json', |
892 |
| - ], |
893 |
| - 'verify' => false, |
| 899 | + $headers = [ |
| 900 | + 'Accept' => 'application/json', |
| 901 | + 'Content-Type' => 'application/json', |
894 | 902 | ];
|
895 |
| - $uri = ltrim($uri, '/'); |
| 903 | + $method = strtolower($method); |
896 | 904 |
|
897 | 905 | if ($this->cliConfiguration->hasAccessToken()) {
|
898 |
| - $options['headers']['Authorization'] = 'Bearer '.$this->cliConfiguration->getAccessToken(); |
| 906 | + $headers['Authorization'] = 'Bearer '.$this->cliConfiguration->getAccessToken(); |
899 | 907 | }
|
900 | 908 |
|
901 |
| - if (in_array($method, ['delete', 'post', 'put'])) { |
902 |
| - $options['json'] = $body; |
903 |
| - } |
| 909 | + return new Request($method, $this->baseUrl.ltrim($uri, '/'), $headers, in_array($method, ['delete', 'post', 'put']) ? (string) json_encode($body) : null); |
| 910 | + } |
904 | 911 |
|
| 912 | + /** |
| 913 | + * Decode response returned by the Ymir API. |
| 914 | + */ |
| 915 | + private function decodeResponse(ResponseInterface $response): Collection |
| 916 | + { |
| 917 | + return collect(json_decode((string) $response->getBody(), true)); |
| 918 | + } |
| 919 | + |
| 920 | + /** |
| 921 | + * Send a request to the Ymir API. |
| 922 | + */ |
| 923 | + private function request(string $method, string $uri, array $body = []): Collection |
| 924 | + { |
905 | 925 | try {
|
906 |
| - $response = $this->client->request($method, $uri, $options); |
| 926 | + $response = $this->client->send($this->createRequest($method, $uri, $body), ['verify' => false]); |
907 | 927 | } catch (ClientException $exception) {
|
908 | 928 | throw new ApiClientException($exception);
|
909 | 929 | }
|
910 | 930 |
|
911 |
| - return collect(json_decode((string) $response->getBody(), true)); |
| 931 | + return $this->decodeResponse($response); |
912 | 932 | }
|
913 | 933 | }
|
0 commit comments