public static function serializeOptions(Operation $operation, array $userValues = []) { $options = ['headers' => []]; $jsonSerializer = new JsonSerializer(); foreach ($userValues as $paramName => $paramValue) { if (null === ($schema = $operation->getParam($paramName))) { continue; } switch ($schema->getLocation()) { case 'query': $options['query'][$schema->getName()] = $paramValue; break; case 'header': $options['headers'] += self::parseHeader($schema, $paramName, $paramValue); break; case 'json': $json = isset($options['json']) ? $options['json'] : []; $options['json'] = $jsonSerializer->stockJson($schema, $paramValue, $json); break; case 'raw': $options['body'] = $paramValue; break; } } if (!empty($options['json']) && ($key = $operation->getJsonKey())) { $options['json'] = [$key => $options['json']]; } return $options; }
protected function sendRequest(Operation $operation, array $userValues = [], $async = false) { $uri = uri_template($operation->getPath(), $userValues); $options = RequestSerializer::serializeOptions($operation, $userValues); $method = $async ? 'requestAsync' : 'request'; return $this->client->{$method}($operation->getMethod(), $uri, $options); }
public function test_json_is_set_on_request() { $client = $this->prophesize(ClientInterface::class); $client->createRequest('POST', 'path', ['json' => ['X-Foo' => 'bar'], 'exceptions' => false])->shouldBeCalled(); $def = ['method' => 'POST', 'path' => 'path', 'params' => ['Foo' => ['type' => 'string', 'location' => 'json', 'sentAs' => 'X-Foo']]]; $userVals = ['Foo' => 'bar']; $operation = new Operation($client->reveal(), $def, $userVals); $operation->createRequest(); }
public function testHeadersOfRequestAreStocked() { $definition = (include 'fixtures/headers.php'); $userValues = ['name' => 'john_doe', 'age' => 30, 'metadata' => ['hair_color' => 'brown'], 'other' => 'blah']; $expected = ['X-Foo-Name' => $userValues['name'], 'age' => $userValues['age'], 'X-Meta-hair_color' => $userValues['metadata']['hair_color']]; $actual = $this->serializer->serialize($userValues, Operation::toParamArray($definition['params'])); $this->assertEquals($expected, $actual); }
public function test_it_nests_object_keys_according_to_path() { $api = new IdentityV3Api(); $params = Operation::toParamArray($api->postTokens()['params']); $user = ['name' => 'foo', 'password' => 'bar', 'domain' => ['name' => 'default']]; $scope = ['project' => ['id' => 'baz']]; $userValues = ['user' => $user, 'scope' => $scope, 'methods' => ['password']]; $expected = ['auth' => ['identity' => ['methods' => ['password'], 'password' => ['user' => $user]], 'scope' => $scope]]; $this->assertEquals($expected, $this->serializer->serialize($userValues, $params)); }
public function serializeOptions(Operation $operation, array $userValues = []) : array { $options = ['headers' => []]; foreach ($userValues as $paramName => $paramValue) { if (null === ($schema = $operation->getParam($paramName))) { continue; } $this->callStockingMethod($schema, $paramValue, $options); } if (!empty($options['json'])) { if ($key = $operation->getJsonKey()) { $options['json'] = [$key => $options['json']]; } if (strpos(json_encode($options['json']), '\\/') !== false) { $options['body'] = json_encode($options['json'], JSON_UNESCAPED_SLASHES); $options['headers']['Content-Type'] = 'application/json'; unset($options['json']); } } return $options; }
/** * This method iterates over a collection of resources. It sends the operation's request to the API, * parses the response, converts each element into {@see self} and - if pagination is supported - continues * to send requests until an empty collection is received back. * * For paginated collections, it sends subsequent requests according to a marker URL query. The value * of the marker will depend on the last element returned in the previous response. If a limit is * provided, the loop will continue up until that point. * * @param Operation $operation The operation responsible for retrieving a new collection * @param callable $mapFn An optional callback that will be executed on every resource iteration. */ public function enumerate(Operation $operation, callable $mapFn = null) { $limit = $operation->getValue('limit') ?: false; $supportsPagination = $operation->hasParam('marker'); $markerKey = $this->markerKey ?: self::DEFAULT_MARKER_KEY; $count = 0; $moreRequestsRequired = true; $totalReached = function ($count) use($limit) { return $limit && $count >= $limit; }; while ($moreRequestsRequired && $count < 20) { $response = $operation->send(); $body = $response->json(); $json = $this->flatten($body, $this->resourcesKey); 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) { $operation->setValue('marker', $resource->{$markerKey}); } (yield $resource); } if ($totalReached($count) || !$supportsPagination || empty($json)) { $moreRequestsRequired = false; } } }