/** * {@inheritDoc} */ public function populateFromResponse(ResponseInterface $response) { $entries = Utils::jsonDecode($response)['access']['serviceCatalog']; foreach ($entries as $entry) { $this->entries[] = $this->model(Entry::class, $entry); } }
private function getSchema() { if (null === $this->jsonSchema) { $response = $this->execute($this->api->getImageSchema()); $this->jsonSchema = new Schema(Utils::jsonDecode($response, false)); } return $this->jsonSchema; }
/** * Populates the current resource from a response object. * * @param ResponseInterface $response * * @return AbstractResource */ public function populateFromResponse(ResponseInterface $response) : self { if (strpos($response->getHeaderLine('Content-Type'), 'application/json') === 0) { $json = Utils::jsonDecode($response); if (!empty($json)) { $this->populateFromArray(Utils::flattenJson($json, $this->resourceKey)); } } return $this; }
/** * @param array $options * * @return Service */ private function getDefaultIdentityService(array $options) : Service { if (!isset($options['authUrl'])) { throw new \InvalidArgumentException("'authUrl' is a required option"); } $clientOptions = ['base_uri' => Utils::normalizeUrl($options['authUrl']), 'handler' => HandlerStack::create()]; if (isset($options['requestOptions'])) { $clientOptions = array_merge($options['requestOptions'], $clientOptions); } return Service::factory(new Client($clientOptions)); }
/** * Creates multiple networks in a single request. * * @param array $data {@see \OpenStack\Networking\v2\Api::postNetworks} * @return Network[] */ public function bulkCreate(array $data) { $response = $this->execute($this->api->postNetworks(), ['networks' => $data]); $networksData = Utils::jsonDecode($response)['networks']; $networks = []; foreach ($networksData as $resourceData) { $resource = $this->newInstance(); $resource->populateFromArray($resourceData); $networks[] = $resource; } return $networks; }
private function fetchResources() { if ($this->shouldNotSendAnotherRequest()) { return false; } $response = call_user_func($this->requestFn, $this->currentMarker); $json = Utils::flattenJson(Utils::jsonDecode($response), $this->resourcesKey); if ($response->getStatusCode() === 204 || empty($json)) { return false; } return $json; }
/** * Retrieves the public URI for this resource. * * @return \GuzzleHttp\Psr7\Uri */ public function getPublicUri() { //return $this->getHttpBaseUrl() . '/' . $this->containerName . '/' . $this->name; return Utils::addPaths($this->getHttpBaseUrl(), $this->containerName, $this->name); }
public function parseMetadata(ResponseInterface $response) { $json = Utils::jsonDecode($response); return isset($json['metadata']) ? $json['metadata'] : []; }
public function parseMetadata(ResponseInterface $response) : array { return Utils::jsonDecode($response)['metadata']; }
public function extractMultipleInstances(ResponseInterface $response, $key = null) { $key = $key ?: $this->getResourcesKey(); $resourcesData = Utils::jsonDecode($response)[$key]; $resources = []; foreach ($resourcesData as $resourceData) { $resource = $this->newInstance(); $resource->populateFromArray($resourceData); $resources[] = $resource; } return $resources; }
/** * {@inheritDoc} */ public function populateFromArray(array $array) : self { return parent::populateFromArray(Utils::flattenJson($array, $this->resourceKey)); }
/** * {@inheritDoc} */ public function enumerate(array $def, array $userVals = [], callable $mapFn = null) { $operation = $this->getOperation($def); $markerKey = $this->markerKey ?: self::DEFAULT_MARKER_KEY; $supportsPagination = $operation->hasParam('marker'); $limit = isset($userVals['limit']) ? $userVals : false; $count = 0; $totalReached = function ($count) use($limit) { return $limit && $count >= $limit; }; while (true) { $response = $this->sendRequest($operation, $userVals); $json = Utils::jsonDecode($response); if (!$json) { break; } $json = Utils::flattenJson($json, $this->resourcesKey); if ($response->getStatusCode() === 204 || empty($json)) { break; } foreach ($json as $resourceData) { if ($totalReached($count)) { break; } $count++; $resource = $this->newInstance(); $resource->populateFromArray($resourceData); if ($mapFn) { call_user_func_array($mapFn, [$resource]); } if ($supportsPagination) { $userVals['marker'] = $resource->{$markerKey}; } (yield $resource); } if ($totalReached($count) || !$supportsPagination) { break; } } }
<?php require 'vendor/autoload.php'; use GuzzleHttp\Client; use GuzzleHttp\HandlerStack; use OpenStack\Identity\v2\Service; use OpenStack\Common\Transport\Utils as TransportUtils; use OpenStack\OpenStack; $authUrl = 'https://example.com:5000/v2.0'; $httpClient = new Client(['base_uri' => TransportUtils::normalizeUrl($authUrl), 'handler' => HandlerStack::create()]); $options = ['authUrl' => $authUrl, 'region' => 'RegionOne', 'username' => 'foo', 'password' => 'bar', 'tenantName' => 'baz', 'identityService' => Service::factory($httpClient)]; $openstack = new OpenStack($options);
private function httpClient(string $baseUrl, HandlerStack $stack) : ClientInterface { $clientOptions = ['base_uri' => Utils::normalizeUrl($baseUrl), 'handler' => $stack]; if (isset($this->globalOptions['requestOptions'])) { $clientOptions = array_merge($this->globalOptions['requestOptions'], $clientOptions); } return new Client($clientOptions); }
private function httpClient($baseUrl, HandlerStack $stack) { return new Client(['base_uri' => Utils::normalizeUrl($baseUrl), 'handler' => $stack]); }
public function test_it_adds_paths() { $uri = Utils::addPaths(uri_for('http://openstack.org/foo'), 'bar', 'baz', '1', '2'); $this->assertInstanceOf(Uri::class, $uri); $this->assertEquals(uri_for('http://openstack.org/foo/bar/baz/1/2'), $uri); }
/** * Retrieve the value for a specific metadata key. * * @param string $key {@see \OpenStack\Compute\v2\Api::getServerMetadataKey} * * @return mixed */ public function getMetadataItem($key) { $response = $this->execute($this->api->getServerMetadataKey(), ['id' => $this->id, 'key' => $key]); return Utils::jsonDecode($response)['metadata'][$key]; }
public static function getAuthOptsV2() { $httpClient = new Client(['base_uri' => CommonUtils::normalizeUrl(getenv('OS_AUTH_URL')), 'handler' => HandlerStack::create()]); return ['authUrl' => getenv('OS_AUTH_URL'), 'region' => getenv('OS_REGION_NAME'), 'username' => getenv('OS_USERNAME'), 'password' => getenv('OS_PASSWORD'), 'tenantName' => getenv('OS_TENANT_NAME'), 'identityService' => new Service($httpClient, new Api())]; }
/** * {@inheritDoc} */ public function populateFromResponse(ResponseInterface $response) { $this->populateFromArray(Utils::jsonDecode($response)['access']['token']); return $this; }
/** * Retrieves the public URI for this resource. * * @return \GuzzleHttp\Psr7\Uri */ public function getPublicUri() : Uri { return Utils::addPaths($this->getHttpBaseUrl(), $this->containerName, $this->name); }