public function build()

in src/ClientBuilder.php [330:407]


    public function build(): Client
    {
        // Transport builder
        $builder = TransportBuilder::create();

        // Set the default hosts if empty
        if (empty($this->hosts)) {
            $this->hosts = [self::DEFAULT_HOST];
        }
        $builder->setHosts($this->hosts);

        // Logger
        if (!empty($this->logger)) {    
            $builder->setLogger($this->logger);
        }

        // Http client
        if (!empty($this->httpClient)) {
            $builder->setClient($this->httpClient);
        }
        // Set HTTP client options
        $builder->setClient(
            $this->setOptions($builder->getClient(), $this->getConfig(), $this->httpClientOptions)
        );

        // Cloud id
        if (!empty($this->cloudId)) {
            $builder->setCloudId($this->cloudId);
        }

        // Node Pool
        if (!empty($this->nodePool)) {
            $builder->setNodePool($this->nodePool);
        }

        $transport = $builder->build();
        
        // The default retries is equal to the number of hosts
        if (empty($this->retries)) {
            $this->retries = count($this->hosts);
        }
        $transport->setRetries($this->retries);

        // Async client
        if (!empty($this->asyncHttpClient)) {
            $transport->setAsyncClient($this->asyncHttpClient);
        }
        
        // Basic authentication
        if (!empty($this->username) && !empty($this->password)) {
            $transport->setUserInfo($this->username, $this->password);
        }

        // API key
        if (!empty($this->apiKey)) {
            if (!empty($this->username)) {
                throw new AuthenticationException('You cannot use APIKey and Basic Authenication together');
            }
            $transport->setHeader('Authorization', sprintf("ApiKey %s", $this->apiKey));
        }

        /**
         * Elastic cloud or serverless optimized with gzip
         * @see https://github.com/elastic/elasticsearch-php/issues/1241 omit for Symfony HTTP Client    
         */
        if ((!empty($this->cloudId) || $this->isCloud($this->hosts) || $this->isServerless($this->hosts)) && !$this->isSymfonyHttpClient($transport)) {
            $transport->setHeader('Accept-Encoding', 'gzip');
        }

        $client = new Client($transport, $transport->getLogger());
        // Enable or disable the x-elastic-client-meta header
        $client->setElasticMetaHeader($this->elasticMetaHeader);

        if ($this->isServerless($this->hosts)) {
            $client->setServerless(true);
        }
        return $client;
    }