public function __construct(HttpClient $httpClient, MessageFactory $messageFactory, UriFactory $uriFactory) { $httpClient = new PluginClient($httpClient, [new AddHostPlugin($uriFactory->createUri(self::BASE_URL))]); $this->httpClient = $httpClient; $this->messageFactory = $messageFactory; $this->uriFactory = $uriFactory; $this->httpMethodsClient = new HttpMethodsClient($httpClient, $messageFactory); $this->registerResources(); }
/** * Configure the base uri from using the API Schema if no baseUri is provided * Or from the baseUri config if provided * * @return UriInterface */ private function getBaseUri() { // Create a base uri from the API Schema if ($this->config['baseUri'] === null) { $scheme = null; $schemes = $this->schema->getSchemes(); if ($schemes === null) { throw new \LogicException('You need to provide at least on scheme in your API Schema'); } foreach ($this->schema->getSchemes() as $candidate) { // Always prefer https if ($candidate === 'https') { $scheme = 'https'; } if ($scheme === null && $candidate === 'http') { $scheme = 'http'; } } if ($scheme === null) { throw new \RuntimeException('Cannot choose a proper scheme from the API Schema. Supported: https, http'); } $host = $this->schema->getHost(); if ($host === null) { throw new \LogicException('The host in the API Schema should not be null'); } return $this->uriFactory->createUri($scheme . '://' . $host); } else { return $this->uriFactory->createUri($this->config['baseUri']); } }
/** * Filter a URL. * * Prefix the URL with "http://" if it has no scheme, then check the URL * for validity. You can specify what parts of the URL are allowed. * * @param string $uriString * @param string[] $allowedParts Array of allowed URL parts (optional) * * @return UriInterface Filtered URI (with default scheme if there was no scheme) * * @throws InvalidUrlException If URL is invalid, the scheme is not http or * contains parts that are not expected */ private function filterUri($uriString, array $allowedParts = []) { // Creating a PSR-7 URI without scheme (with parse_url) results in the // original hostname to be seen as path. So first add a scheme if none // is given. if (false === strpos($uriString, '://')) { $uriString = sprintf('%s://%s', 'http', $uriString); } try { $uri = $this->uriFactory->createUri($uriString); } catch (\InvalidArgumentException $e) { throw InvalidUrlException::invalidUrl($uriString); } if (!$uri->getScheme()) { throw InvalidUrlException::invalidUrl($uriString, 'empty scheme'); } if (count($allowedParts) > 0) { $parts = parse_url((string) $uri); $diff = array_diff(array_keys($parts), $allowedParts); if (count($diff) > 0) { throw InvalidUrlException::invalidUrlParts($uriString, $allowedParts); } } return $uri; }
/** * @param Token $token * @return UriInterface */ private function createUriFromToken(Token $token) { return $this->uriFactory->createUri($this->url . $token->getValue()); }