/** * {@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; }
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; }
/** * 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; }
/** * {@inheritDoc} */ public function populateFromResponse(ResponseInterface $response) { $this->populateFromArray(Utils::jsonDecode($response)['access']['token']); return $this; }
/** * {@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; } } }
/** * 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 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; }
/** * @expectedException \InvalidArgumentException */ public function test_decoding_malformed_json_throws_error() { $response = new Response(200, [], \GuzzleHttp\Psr7\stream_for('{')); Utils::jsonDecode($response); }