Configure HTTP Client

@return ClientInterface
553282
protected function initHttpClient()
    {

        // Middleware
        $obj_stack = HandlerStack::create();
        $obj_stack->push(ApplicationDefaultCredentials::getMiddleware(['https://www.googleapis.com/auth/datastore']));

        $str_base_url = self::DEFAULT_BASE_URL;

        if (getenv("DATASTORE_EMULATOR_HOST") !== FALSE) {
            $str_base_url = getenv("DATASTORE_EMULATOR_HOST");
        }

        // Create the HTTP client
        return new Client([
            'handler' => $obj_stack,
            'base_url' => $str_base_url,
            'auth' => 'google_auth'  // authorize all requests
        ]);
    }
743117
protected function getClient() {
		if(!$this->client) {
			$this->client = new Client([
				RequestOptions::VERIFY  => self::$VERIFY_SSL,
				RequestOptions::TIMEOUT => $this->timeout,
			]);
		}
		return $this->client;
	}
1301391
private function httpClient()
    {
        if (!self::$httpClient) {
            self::$httpClient = HttpClient\CurlClient::instance();
        }
        return self::$httpClient;
    }
655201
public function getHttpClient()
    {
        if (!is_object($this->_httpClient)) {
            $this->_httpClient = new Client([
                'baseUrl' => $this->baseUrl,
                'requestConfig' => [
                    'options' => $this->requestOptions
                ],
                'responseConfig' => [
                    'format' => Client::FORMAT_JSON
                ],
            ]);
        }
        return $this->_httpClient;
    }
236991
public function getHttpClient()
    {
        if (!is_object($this->_httpClient)) {
            $this->_httpClient = new Client([
                'requestConfig' => [
                    'options' => [
                        'timeout' => 30,
                    ]
                ],
                'responseConfig' => [
                    'format' => Client::FORMAT_JSON
                ],
            ]);
        }
        return $this->_httpClient;
    }
1286323
public function getHttpClient()
    {
        if (!isset($this->_httpClient)) {
            $this->_httpClient = Yii::createObject($this->guzzleClass, [$this->guzzleConfig]);
        }
        return $this->_httpClient;
    }
916192
protected function http() {
        // 若$this->http不是\GuzzleHttp\Client实例，则创建一个
        if (!$this->http instanceof Client) {
            $this->http = new Client(
                array_merge(
                    [ 'base_uri' => $this->url ],
                    $this->options
                )
            );
        }

        return $this->http;
    }
735033
protected function getClient()
    {
        $retries   = 5;
        $httpCodes = null;
        $curlCodes = null;

        $plugin = BackoffPlugin::getExponentialBackoff($retries, $httpCodes, $curlCodes);
        $client = new Client($this->baseUrl);
        $client->addSubscriber($plugin);

        return $client;
    }
1151055
protected function configureRequest(RequestInterface $Request)
    {
        if ($Request->getStatus() >= Curl::STATUS_SENT){
            $Request->reset();
        }
        if (isset($this->properties[self::PROPERTY_HTTP_METHOD]) &&
            $this->properties[self::PROPERTY_HTTP_METHOD] !== ''){
            $Request->setMethod($this->properties[self::PROPERTY_HTTP_METHOD]);
        }
        $url = $this->configureURL($this->getOptions());
        if ($this->verifyUrl($url)) {
            $url = rtrim($this->getBaseUrl(),"/")."/".$url;
            $Request->setURL($url);
        }
        $data = ($this->getData()?$this->getData():NULL);
        $data = $this->configureData($data);
        $Request->setBody($data);
        if ($this->authRequired()){
            if (isset($this->Auth)){
                $this->Auth->configureRequest($Request);
            }
        }
        return $Request;
    }
282893
public function initHttpClients()
    {
        $clientConfig = $this->config->getHttpClientConfig();
        $authClient = new HttpClient($clientConfig);
        $this->setAuthHttpClient($authClient);

        $clientConfig['handler'] = $this->initHandlerStack();
        $client = new HttpClient($clientConfig);
        $this->setHttpClient($client);
    }
950691
private static function setClient()
    {
        if (self::$client === null) {
            self::$client = new Client([
                'base_uri'  => self::$environment,
                'timeout'   => self::TIMEOUT,
                'verify'    => self::VERIFY,
            ]);
        }
    }
1241851
protected function getClient()
	{
		$this->client->timeout       = $this->timeout;
		$this->client->sslVerifyPeer = $this->sslVerifyPeer;

		return $this->client;
	}
1200522
public function generateHttpClient()
    {
        $config = $this->config;
        if (!array_key_exists('handler', $config)
            || $config['handler'] === null) {
            $config['handler'] = HandlerStack::create();
        }

        // Add a logging middleware required by this library.
        $config['handler']->before(
            'http_errors',
            GuzzleLogMessageHandler::log($this->logger, $this->messageFormatter)
        );

        return new Client($config);
    }
38543
protected function httpClient()
    {
        if (null === $this->obj_http_client) {
            $this->obj_http_client = $this->initHttpClient();
        }
        return $this->obj_http_client;
    }
1056116
private function httpHandler()
    {
        if (config()->loadFile('view') === true) {
            // Instantiate Http UserAgent Service
            $this->services->load(Framework\Http\UserAgent::class, 'userAgent');

            // Instantiate Http View Service
            $this->services->load(Framework\Http\Parser::class);

            // Instantiate Http View Service
            $this->services->load(Framework\Http\View::class);

            // Instantiate Http Presenter Service
            $this->services->load(Framework\Http\Presenter::class);
        }

        // Instantiate Http Router Service
        $this->services->load(Framework\Http\Router::class);

        if (profiler() !== false) {
            profiler()->watch('Parse Router Request');
        }
        router()->parseRequest();

        if (config()->loadFile('session') === true) {

            // Instantiate Session Service
            $session = new Session(config('session', true));
            $session->setLogger($this->services->get('logger'));

            if ( ! $session->isStarted()) {
                $session->start();
            }

            $this->services->add($session, 'session');

            if ($session->has('language') and $this->services->has('language')) {
                language()->setDefault($session->get('language'));
            } else {
                $session->set('language', language()->getDefault());
            }

            if (config('security')->protection[ 'csrf' ] === true) {
                $csrfProtection = new Security\Protections\Csrf();
                $this->services->add($csrfProtection, 'csrfProtection');
            }

            if (config('security')->protection[ 'xss' ] === true) {
                $xssProtection = new Security\Protections\Xss();
                $this->services->add($xssProtection, 'xssProtection');
            }
        }

        // Instantiate Http Middleware Service
        $this->services->load(Framework\Http\Middleware::class);

        if (profiler() !== false) {
            profiler()->watch('Running Middleware Service: Pre Controller');
        }
        middleware()->run();

        if ($this->services->has('controller')) {
            $controller = $this->services->get('controller');

            $controllerParameter = dash($controller->getParameter());
            $controllerRequestMethod = dash($controller->getRequestMethod());

            $modules = $this->modules->getArrayCopy();
            
            // Run Module Autoloader
            foreach ($modules as $module) {
                if (in_array($module->getType(), ['KERNEL', 'FRAMEWORK'])) {
                    continue;
                }

                // Autoload Module Language
                if ($this->services->has('language')) {
                    language()->loadFile($module->getParameter());
                }

                // Autoload Module Model
                $module->loadModel();

                // Add View Resource Directory
                if($this->services->has('view')) {
                    view()->addFilePath($module->getResourcesDir());
                    presenter()->assets->pushFilePath($module->getResourcesDir());
                }
            }
            
            if ($this->services->has('view')) {
                presenter()->initialize();
            }

            // Autoload Language
            if ($this->services->has('language')) {
                language()->loadFile($controller->getParameter());
                language()->loadFile($controller->getRequestMethod());
                language()->loadFile($controller->getParameter() . '/' . $controller->getRequestMethod());
            }

            // Autoload Model
            $modelClassName = str_replace(['Controllers', 'Presenters'], 'Models', $controller->getName());

            if (class_exists($modelClassName)) {
                $this->models->load($modelClassName, 'controller');
            }

            if ($this->services->has('view')) {
                // Autoload Presenter
                $presenterClassName = str_replace('Controllers', 'Presenters', $controller->getName());

                if (class_exists($presenterClassName)) {
                    $presenterClassObject = new $presenterClassName();
                    if ($presenterClassObject instanceof Framework\Http\Presenter) {
                        $this->services->add($presenterClassObject, 'presenter');
                    }
                }
            }

            // Initialize Controller
            if (profiler() !== false) {
                profiler()->watch('Calling Hooks Service: Pre Controller');
            }

            hooks()->callEvent(Framework\Services\Hooks::PRE_CONTROLLER);

            if (profiler() !== false) {
                profiler()->watch('Instantiating Requested Controller: ' . $controller->getClass());
            }
            $requestController = $controller->getInstance();

            if (method_exists($requestController, '__reconstruct')) {
                $requestController->__reconstruct();
            }

            if (profiler() !== false) {
                profiler()->watch('Calling Hooks Service: Post Controller');
            }
            hooks()->callEvent(Framework\Services\Hooks::POST_CONTROLLER);

            if (profiler() !== false) {
                profiler()->watch('Calling Middleware Service: Post Controller');
            }
            middleware()->run();

            $requestMethod = $controller->getRequestMethod();
            $requestMethodArgs = $controller->getRequestMethodArgs();

            // Call the requested controller method
            if (profiler() !== false) {
                profiler()->watch('Execute Requested Controller Method');
            }

            ob_start();
            $requestController->__call($requestMethod, $requestMethodArgs);
            $requestControllerOutput = ob_get_contents();
            ob_end_clean();

            if (is_numeric($requestControllerOutput)) {
                output()->sendError($requestControllerOutput);
            } elseif (is_bool($requestControllerOutput)) {
                if ($requestControllerOutput === true) {
                    output()->sendError(200);
                } elseif ($requestControllerOutput === false) {
                    output()->sendError(204);
                }
            } elseif (is_array($requestControllerOutput) or is_object($requestControllerOutput)) {
                output()->sendPayload($requestControllerOutput);
            } elseif ($requestController instanceof Framework\Http\Controllers\Restful) {
                if (empty($requestControllerOutput)) {
                    $requestController->sendError(204);
                } elseif (is_string($requestControllerOutput)) {
                    if (is_json($requestControllerOutput)) {
                        output()->setContentType('application/json');
                    } else {
                        output()->setContentType('text/plain');
                    }

                    echo $requestControllerOutput;
                }
            } elseif (is_string($requestControllerOutput)) {
                if (is_json($requestControllerOutput)) {
                    output()->setContentType('application/json');
                    echo $requestControllerOutput;
                } elseif ($this->services->has('view')) {
                    if (empty($requestControllerOutput)) {
                        $filenames = [
                            $controllerRequestMethod,
                            $controllerParameter . DIRECTORY_SEPARATOR . $controllerRequestMethod,
                        ];

                        if ($controllerRequestMethod === 'index') {
                            array_unshift($filenames, $controllerParameter);
                        }

                        foreach ($filenames as $filename) {
                            if (false !== ($filePath = view()->getFilePath($filename))) {
                                view()->load($filePath);
                                break;
                            }
                        }
                    } else {
                        presenter()->partials->offsetSet('content', $requestControllerOutput);
                    }

                    if (presenter()->partials->offsetExists('content')) {
                        if(is_ajax()) {
                            echo presenter()->partials->content;
                        } else {
                            $htmlOutput = view()->render();

                            if (empty($htmlOutput)) {
                                output()->sendError(204);
                            } else {
                                output()->setContentType('text/html');
                                output()->send($htmlOutput);
                            }
                        }
                    } else {
                        output()->sendError(204);
                    }
                } elseif (empty($requestControllerOutput) or $requestControllerOutput === '') {
                    output()->sendError(204);
                } else {
                    output()->setContentType('text/plain');
                    output()->send($requestControllerOutput);
                }
            }
        } else {
            // Show Error (404) Page Not Found
            output()->sendError(404);
        }
    }
90801
private function getHttpClient()
    {
        if ($httpClient = $this->app->config->get('swap.http_client')) {
            return $this->app[$httpClient];
        }

        return null;
    }
955027
protected function constructHttpClient()
    {
        $hasHttpClient = (null !== $this->httpClient);

        $this->httpClient = new HttpClient($this->getOptions());

        if (!$hasHttpClient) {
            $this->httpClient->registerDefaults();
        }
    }