/** * Constructor * * @param string $accessToken */ public function __construct($accessToken) { $this->accessToken = $accessToken; $this->client = new Client(); $this->client->setDescription(ServiceDescription::factory(__DIR__ . '/Resources/config/service.json')); $this->client->getEventDispatcher()->addListener('client.create_request', [$this, 'onClientCreateRequest']); $this->client->getEventDispatcher()->addListener('request.error', [$this, 'onRequestError']); }
/** * Initializes initializer * * @param Client $client * @param array $parameters */ public function __construct(Client $client, array $parameters) { $this->client = $client; $this->parameters = $parameters; if (!empty($parameters['service_descriptions'])) { $this->client->setDescription(ServiceDescription::factory($parameters['service_descriptions'])); } }
public function __construct($request_options) { $description = ServiceDescription::factory(__DIR__ . "/../../clients/index.json"); $this->client = new Client($request_options['base_url']); $this->client->setDescription($description); $this->client->setConfig($request_options); $tokenPlugin = new \Gitlab\Auth\Token\Plugin($this, $request_options); $urlEncoder = new \Gitlab\Core\Plugin\UrlEncoder(); $this->client->addSubscriber($tokenPlugin); $this->client->addSubscriber($urlEncoder); }
protected function getClient() { $service = ServiceDescription::factory(__DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'TestData' . DIRECTORY_SEPARATOR . 'test_service.xml'); $client = new Client('http://www.google.com/'); $client->setDescription($service); return $client; }
/** * @return Client */ public function testClientConstruct() { // Define a service with a test() method $service = new ServiceDescription(array('name' => 'test-service', 'operations' => array('test' => array('uri' => '/test.json', 'httpMethod' => 'GET', 'responseClass' => 'TestModel', 'class' => '\\Loco\\Utils\\Swizzle\\Command\\StrictCommand'), 'testlist' => array('uri' => '/testlist.json', 'httpMethod' => 'GET', 'responseClass' => 'array', 'class' => '\\Loco\\Utils\\Swizzle\\Command\\StrictCommand', 'items' => array('$ref' => 'TestModel')), 'testwrap' => array('uri' => '/testwrap.json', 'httpMethod' => 'GET', 'responseClass' => 'TestModelList', 'class' => '\\Loco\\Utils\\Swizzle\\Command\\StrictCommand'), 'testwrapobj' => array('uri' => '/testwrapobj.json', 'httpMethod' => 'GET', 'responseClass' => 'TestModelListObject', 'class' => '\\Loco\\Utils\\Swizzle\\Command\\StrictCommand')), 'models' => array('TestModel' => array('type' => 'object', 'additionalProperties' => false, 'properties' => array('foo' => array('type' => 'integer', 'location' => 'json', 'required' => true), 'bar' => array('type' => 'integer', 'location' => 'json', 'required' => false))), 'TestModelList' => array('type' => 'array', 'items' => array('$ref' => 'TestModel')), 'TestModelListObject' => array('type' => 'object', 'additionalProperties' => false, 'properties' => array('list' => array('type' => 'array', 'location' => 'json', 'items' => array('$ref' => 'TestModel'))))))); $client = new Client(); $client->setDescription($service); // test models are defined ok $op = $service->getOperation('test'); $this->assertEquals('model', $op->getResponseType()); $this->assertEquals('TestModel', $op->getResponseClass()); // listing is just an array $op = $service->getOperation('testlist'); $this->assertEquals('primitive', $op->getResponseType()); $this->assertEquals('array', $op->getResponseClass()); // test listing is aware of models in itself // It's not. Operation has no 'items' property // list wrapper is a model $op = $service->getOperation('testwrap'); $this->assertEquals('model', $op->getResponseType()); $this->assertEquals('TestModelList', $op->getResponseClass()); // test model resolved from $ref $listModel = $service->getModel('TestModelList'); $this->assertInstanceOf('\\Guzzle\\Service\\Description\\Parameter', $listModel); // items should be single schema specifying one allowed model type $items = $listModel->getItems(); $this->assertInstanceOf('\\Guzzle\\Service\\Description\\Parameter', $items); $this->assertEquals('object', $items->getType()); $this->assertArrayHasKey('foo', $items->getProperties()); return $client; }
protected function getClient() { $description = new ServiceDescription(array('operations' => array('test' => array('httpMethod' => 'PUT', 'parameters' => array('ContentMD5' => array(), 'Body' => array('location' => 'body')))))); $client = new Client(); $client->setDescription($description); return $client; }
/** * Construct Swagger client for calling the petstore * @depends testServiceBuild * @return Client */ public function testClientConstruct(ServiceDescription $service) { $client = new Client(); $client->setDescription($service); $this->assertEquals('http://petstore.swagger.wordnik.com/api', $client->getBaseUrl()); // @todo add Accept: application/json to every request? return $client; }
public function testAllowsForJsonBasedArrayParamsFunctionalTest() { $description = new ServiceDescription(array('operations' => array('test' => new Operation(array('httpMethod' => 'PUT', 'parameters' => array('data' => array('required' => true, 'filters' => 'json_encode', 'location' => 'body'))))))); $client = new Client(); $client->setDescription($description); $command = $client->getCommand('test', array('data' => array('foo' => 'bar'))); $request = $command->prepare(); $this->assertEquals('{"foo":"bar"}', (string) $request->getBody()); }
public function testAllowsForJsonBasedArrayParamsFunctionalTest() { $service = array('test' => new ApiCommand(array('method' => 'PUT', 'params' => array('data' => array('required' => true, 'type' => 'type:array', 'filters' => 'json_encode', 'location' => 'body'))))); $description = new ServiceDescription($service); $client = new Client(); $client->setDescription($description); $command = $client->getCommand('test', array('data' => array('foo' => 'bar'))); $request = $command->prepare(); $this->assertEquals(json_encode(array('foo' => 'bar')), (string) $request->getBody()); }
public function createEchaleGasClient() { $client = new Client(); $client->setDescription(ServiceDescription::factory('config/services/echalegas.json')); $logger = new Logger('debug'); $logger->pushHandler(new StreamHandler('logs/debug.log')); $logPlugin = new LogPlugin(new MonologLogAdapter($logger), MessageFormatter::DEBUG_FORMAT); $client->addSubscriber($logPlugin); return $client; }
protected function getMockedClient(Response $response) { $operation = new Operation(array('httpMethod' => 'GET', 'name' => 'Mock')); $service = new ServiceDescription(); $service->addOperation($operation); $plugin = new MockPlugin(); $plugin->addResponse($response); $client = new Client(); $client->setDescription($service); $client->addSubscriber($plugin); return $client; }
public function testValidatesAdditionalParameters() { $description = ServiceDescription::factory(array('operations' => array('foo' => array('httpMethod' => 'PUT', 'parameters' => array('bar' => array('location' => 'header')), 'additionalParameters' => array('location' => 'json'))))); $client = new Client(); $client->setDescription($description); $command = $client->getCommand('foo'); $command['bar'] = 'test'; $command['hello'] = 'abc'; $request = $command->prepare(); $this->assertEquals('test', (string) $request->getHeader('bar')); $this->assertEquals('{"hello":"abc"}', (string) $request->getBody()); }
public function testVisitsLocationWithMultipleFiles() { $description = ServiceDescription::factory(array('operations' => array('DoPost' => array('httpMethod' => 'POST', 'parameters' => array('foo' => array('location' => 'postFile', 'type' => array('string', 'array'))))))); $this->getServer()->flush(); $this->getServer()->enqueue(array("HTTP/1.1 200 OK\r\nContent-Length:0\r\n\r\n")); $client = new Client($this->getServer()->getUrl()); $client->setDescription($description); $command = $client->getCommand('DoPost', array('foo' => array(__FILE__, __FILE__))); $command->execute(); $received = $this->getServer()->getReceivedRequests(); $this->assertContains('name="foo[0]";', $received[0]); $this->assertContains('name="foo[1]";', $received[0]); }
public function testUsesProcessedModelsWhenEnabled() { $d = ServiceDescription::factory(array('operations' => array('foobar' => array('name' => 'foobar', 'httpMethod' => 'PUT', 'responseClass' => 'foo', 'responseType' => 'model', 'class' => 'Aws\\Common\\Command\\JsonCommand', 'parameters' => array('test' => array('location' => 'query')))), 'models' => array('foo' => array('type' => 'object', 'properties' => array('test' => array('type' => 'string', 'location' => 'json', 'filters' => array('strtoupper'))))))); $response = new Response(200, array('Content-Type' => 'application/json'), '{"test":"bar"}'); $client = new Client('http://localhost:1245'); $client->setDescription($d); $this->setMockResponse($client, array($response)); $command = $client->getCommand('foobar'); $this->assertEquals(array('test' => 'bar'), $command->execute()->toArray()); $this->setMockResponse($client, array($response)); $command = $client->getCommand('foobar'); $command->set('command.model_processing', true); $this->assertEquals(array('test' => 'BAR'), $command->execute()->toArray()); }
public function testSupportsServiceDescriptionBaseUrls() { $description = new ServiceDescription(array('baseUrl' => 'http://foo.com')); $client = new Client(); $client->setDescription($description); $this->assertEquals('http://foo.com', $client->getBaseUrl()); }
protected function getClient() { $client = new Client('http://www.google.com/'); return $client->setDescription(ServiceDescription::factory(__DIR__ . '/../../TestData/test_service.json')); }
<?php require '../vendor/autoload.php'; use Guzzle\Service\Client; use Guzzle\Service\Description\ServiceDescription; use Guzzle\Log\MessageFormatter; use Guzzle\Log\MonologLogAdapter; use Guzzle\Plugin\Log\LogPlugin; use Monolog\Handler\StreamHandler; use Monolog\Logger; use Faker\Factory; $faker = Factory::create(); $client = new Client(); $client->setDescription(ServiceDescription::factory('../config/config.json')); $logger = new Logger('debug'); $logger->pushHandler(new StreamHandler('../logs/debug.log')); $logPlugin = new LogPlugin(new MonologLogAdapter($logger), MessageFormatter::DEBUG_FORMAT); $client->addSubscriber($logPlugin); $contact = $client->showContact(['contactId' => 1, 'accept' => 'application/json']); echo "\nShow contact information {$contact['name']} {$contact['last_name']}\n"; $newContact = ['name' => $faker->firstName, 'lastName' => $faker->lastName]; $contact = $client->newContact(array_merge($newContact, ['accept' => 'application/xml'])); echo <<<MESSAGE New contact information {$contact['name']} {$contact['last_name']} with ID {$contact['contact_id']} MESSAGE; $newName = $faker->firstName; $contact = $client->editContact(['contactId' => 2, 'name' => $newName, 'accept' => 'application/json']); echo <<<MESSAGE
/** * Loads API method definitions * * @param \Guzzle\Service\Client $client */ public static function loadDefinitions(Client $client) { $serviceDescriptions = ServiceDescription::factory(__DIR__ . '/Resources/config/meetup.json'); foreach ($serviceDescriptions->getOperations() as $operation) { /** @var $operation Operation */ $operation->setClass('DMS\\Service\\Meetup\\Command\\MeetupCommand'); } $client->setDescription($serviceDescriptions); }
/** * @covers Guzzle\Service\Command\DynamicCommand::build */ public function testAllowsPostFieldsAndFiles() { $service = new ServiceDescription(array('post_command' => new ApiCommand(array('method' => 'POST', 'uri' => '/key', 'params' => array('test' => array('location' => 'post_field'), 'test_2' => array('location' => 'post_field:foo'), 'test_3' => array('location' => 'post_file')))))); $client = new Client('http://www.test.com/api/v2'); $client->setDescription($service); $command = $client->getCommand('post_command', array('test' => 'Hi!', 'test_2' => 'There', 'test_3' => __FILE__)); $request = $command->prepare(); $this->assertEquals('Hi!', $request->getPostField('test')); $this->assertEquals('There', $request->getPostField('foo')); $this->assertInternalType('array', $request->getPostFile('test_3')); $command = $client->getCommand('post_command', array('test_3' => new PostFile('baz', __FILE__))); $request = $command->prepare(); $this->assertInternalType('array', $request->getPostFile('baz')); }
public function testCanAddListenerToParseDomainObjects() { $client = new Client(); $client->setDescription(ServiceDescription::factory(array('operations' => array('test' => array('responseClass' => 'FooBazBar'))))); $foo = new \stdClass(); $client->getEventDispatcher()->addListener('command.parse_response', function ($e) use($foo) { $e['result'] = $foo; }); $command = $client->getCommand('test'); $command->prepare()->setResponse(new Response(200), true); $result = $command->execute(); $this->assertSame($result, $foo); }
/** * @covers Guzzle\Service\Command\DynamicCommand::build */ public function testUsesRelativePaths() { $service = new ServiceDescription(array('test_path' => new ApiCommand(array('method' => 'GET', 'path' => 'test/abc')))); $client = new Client('http://www.test.com/api/v2'); $client->setDescription($service); $command = $client->getCommand('test_path'); $request = $command->prepare(); $this->assertEquals('/api/v2/test/abc', $request->getPath()); }
protected function setupClient() { $this->client = new GuzzleClient($this->getBaseUrl(), $this->guzzleOptions); $this->client->setDescription(ServiceDescription::factory(__DIR__ . '/config/service.json')); }
/** * Loads the service description document which stores the service information * * @param string $document */ private function loadServiceDescription($document) { $description = GuzzleServiceDescription::factory($document); $this->client->setDescription($description); }
/** * @covers Guzzle\Service\Command\DynamicCommand::addVisitor */ public function testAllowsCustomVisitor() { $service = new ServiceDescription(array('foo' => new ApiCommand(array('params' => array('test' => array('location' => 'query')))))); $client = new Client(); $client->setDescription($service); $command = $client->getCommand('foo', array('test' => 'hi')); // Flip query and header $command->addVisitor('query', new HeaderVisitor()); $request = $command->prepare(); $this->assertEquals('hi', (string) $request->getHeader('test')); }
/** * @expectedException \Guzzle\Service\Exception\ValidationException * @expectedExceptionMessage Validation errors: [abc] must be of type string */ public function testValidatesAdditionalParameters() { $description = ServiceDescription::factory(array('operations' => array('foo' => array('parameters' => array('baz' => array('type' => 'integer')), 'additionalParameters' => array('type' => 'string'))))); $client = new Client(); $client->setDescription($description); $command = $client->getCommand('foo', array('abc' => false, 'command.headers' => array('foo' => 'bar'))); $command->prepare(); }
<?php use Guzzle\Service\Client; use Guzzle\Service\Description\ServiceDescription; $app->container->singleton('client', function () { $client = new Client(); $client->setDescription(ServiceDescription::factory('config/service.json')); return $client; });