本文整理汇总了PHP中Guzzle\Service\Description\ServiceDescription类的典型用法代码示例。如果您正苦于以下问题:PHP ServiceDescription类的具体用法?PHP ServiceDescription怎么用?PHP ServiceDescription使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了ServiceDescription类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: testConstructor
/**
* @covers Guzzle\Service\Description\ServiceDescription
* @covers Guzzle\Service\Description\ServiceDescription::__construct
* @covers Guzzle\Service\Description\ServiceDescription::getCommands
* @covers Guzzle\Service\Description\ServiceDescription::getCommand
*/
public function testConstructor()
{
$service = new ServiceDescription($this->serviceData);
$this->assertEquals(1, count($service->getCommands()));
$this->assertFalse($service->hasCommand('foobar'));
$this->assertTrue($service->hasCommand('test_command'));
}
示例2: testAllowsRawResponses
public function testAllowsRawResponses()
{
$description = new ServiceDescription(array('operations' => array('foo' => array('responseClass' => 'bar', 'responseType' => 'model')), 'models' => array('bar' => array())));
$op = new OperationCommand(array(OperationCommand::RESPONSE_PROCESSING => OperationCommand::TYPE_RAW), $description->getOperation('foo'));
$op->setClient(new Client());
$request = $op->prepare();
$response = new Response(200, array('Content-Type' => 'application/xml'), '<Foo><Baz>Bar</Baz></Foo>');
$request->setResponse($response, true);
$this->assertSame($response, $op->execute());
}
示例3: getMockedClient
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;
}
示例4: factory
public static function factory($config = array())
{
if (isset($config['developer_mode']) && is_bool($config['developer_mode'])) {
$developerMode = $config['developer_mode'];
} else {
$developerMode = false;
}
$baseUrl = array('https://api.auspost.com.au', 'https://devcentre.auspost.com.au/myapi');
// Ignore unnecessary user-specified configuration values
if ($developerMode) {
unset($config['email_address']);
unset($config['password']);
}
unset($config['base_url']);
$default = array('developer_mode' => $developerMode, 'base_url' => $baseUrl[$developerMode], 'email_address' => 'anonymous@auspost.com.au', 'password' => 'password');
$required = array('developer_mode', 'base_url', 'email_address', "password");
$config = Collection::fromConfig($config, $default, $required);
$client = new self($config->get('base_url'), $config);
$client->getConfig()->setPath('request.options/headers/Authorization', 'Basic ' . base64_encode($config->get('email_address') . ':' . $config->get('password')));
$client->setDescription(ServiceDescription::factory(__DIR__ . '/service.json'));
$client->setSslVerification(false);
$client->getEventDispatcher()->addListener('request.before_send', function (Event $event) {
$request = $event['request'];
$request->addCookie('OBBasicAuth', 'fromDialog');
});
return $client;
}
示例5: getClient
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;
}
示例6: factory
/**
* Factory method to create a new TogglClient
*
* The following array keys and values are available options:
* - base_url: Base URL of web service
* - username: username or API key
* - password: password (if empty, then username is a API key)
*
* See https://www.toggl.com/public/api#api_token for more information on the api token
*
* @param array|Collection $config Configuration data
*
* @return self
*/
public static function factory($config = array())
{
$default = array('base_url' => 'https://www.toggl.com/api/{apiVersion}', 'debug' => false, 'apiVersion' => 'v8', 'api_key' => '', 'username' => '', 'password' => '');
$required = array('api_key', 'username', 'password', 'base_url', 'apiVersion');
$config = Collection::fromConfig($config, $default, $required);
$client = new self($config->get('base_url'), $config);
// Attach a service description to the client
if ($config->get('apiVersion') == 'v8') {
$description = ServiceDescription::factory(__DIR__ . '/services_v8.json');
} else {
die('Only v8 is supported at this time');
}
$client->setDescription($description);
$client->setDefaultHeaders(array("Content-type" => "application/json"));
if (!empty($config->get('api_key'))) {
$config->set('username', $config->get('api_key'));
$config->set('password', 'api_token');
}
if (empty($config->get('password'))) {
$config->set('password', 'api_token');
}
$authPlugin = new CurlAuthPlugin($config->get('username'), $config->get('password'));
$client->addSubscriber($authPlugin);
if ($config->get('debug')) {
$client->addSubscriber(LogPlugin::getDebugPlugin());
}
return $client;
}
示例7: factory
/**
* @param array $config
* @return \Guzzle\Service\Client|ImageRelayClient
* @throws \Guzzle\Common\Exception\InvalidArgumentException
*/
public static function factory($config = array())
{
$default = array('base_url' => 'https://{imagerelay_url}/api/v2/', 'imagerelay_url' => 'subdomain.imagerelay.com');
$config = Collection::fromConfig($config, $default);
$client = new self($config->get('base_url'), $config);
if ($config['auth'] === 'http') {
if (!isset($config['username'], $config['password'])) {
throw new InvalidArgumentException("Username and password required when using http auth.");
}
$authorization = 'Basic ' . base64_encode($config['username'] . ':' . $config['password']);
}
if ($config['auth'] === 'oauth') {
if (!isset($config['token'])) {
throw new InvalidArgumentException("Access token required when using oauth.");
}
$authorization = sprintf('Bearer %s', $config['token']);
}
if (!isset($authorization)) {
throw new InvalidArgumentException("Must use either http or oauth authentication method.");
}
// Attach a service description to the client
$description = ServiceDescription::factory(__DIR__ . '/Resources/api.php');
$client->setDescription($description);
// Set required User-Agent
$client->setUserAgent(sprintf('%s (%s)', $config['app_name'], $config['app_contact']));
$client->getEventDispatcher()->addListener('request.before_send', function (Event $event) use($authorization) {
$event['request']->addHeader('Authorization', $authorization);
});
return $client;
}
示例8: factory
/**
* {@inheritdoc}
*/
public static function factory($config = array())
{
$default = array('url' => false, 'munchkin_id' => false, 'version' => 1, 'bulk' => false);
$required = array('client_id', 'client_secret', 'version');
$config = Collection::fromConfig($config, $default, $required);
$url = $config->get('url');
if (!$url) {
$munchkin = $config->get('munchkin_id');
if (!$munchkin) {
throw new \Exception('Must provide either a URL or Munchkin code.');
}
$url = sprintf('https://%s.mktorest.com', $munchkin);
}
$grantType = new Credentials($url, $config->get('client_id'), $config->get('client_secret'));
$auth = new Oauth2Plugin($grantType);
if ($config->get('bulk') === true) {
$restUrl = sprintf('%s/bulk/v%d', rtrim($url, '/'), $config->get('version'));
} else {
$restUrl = sprintf('%s/rest/v%d', rtrim($url, '/'), $config->get('version'));
}
$client = new self($restUrl, $config);
$client->addSubscriber($auth);
$client->setDescription(ServiceDescription::factory(__DIR__ . '/service.json'));
$client->setDefaultOption('headers/Content-Type', 'application/json');
return $client;
}
示例9: factory
/**
* @param array $config
* @return \Guzzle\Service\Client|BasecampClient
* @throws \Guzzle\Common\Exception\InvalidArgumentException
*/
public static function factory($config = array())
{
$default = array('base_url' => 'https://basecamp.com/', 'version' => 'v1', 'token' => null, 'user_agent' => null, 'auth_method' => 'oauth');
$required = [];
$config = Collection::fromConfig($config, $default, $required);
$client = new self($config->get('base_url'), $config);
if (empty($config['token'])) {
throw new InvalidArgumentException("Config must contain token when using oath");
}
$authorization = sprintf('Bearer %s', $config['token']);
if (!isset($authorization)) {
throw new InvalidArgumentException("Config must contain valid authentication method");
}
// Attach a service description to the client
$description = ServiceDescription::factory(__DIR__ . '/Resources/service.php');
$client->setDescription($description);
// Set required User-Agent
$client->setUserAgent($config['user_agent']);
$client->getEventDispatcher()->addListener('request.before_send', function (Event $event) use($authorization) {
$event['request']->addHeader('Authorization', $authorization);
});
// Add cache plugin
$cachePlugin = new CachePlugin(['storage' => new DefaultCacheStorage(new DoctrineCacheAdapter(new ApcCache()))]);
$client->addSubscriber($cachePlugin);
return $client;
}
示例10: factory
public static function factory($config = array(), $required = array())
{
if (!defined('static::ENDPOINT')) {
throw new Exception\ServiceEndpointException('A client must have an endpoint');
}
$default = array('base_url' => '{scheme}://{domain}/' . static::ENDPOINT);
$required = array_merge(array('scheme', 'domain', 'base_url'), $required);
$config = Collection::fromConfig($config, $default, $required);
$client = new static($config->get('base_url'), $config);
$refClass = new \ReflectionClass(get_called_class());
$serviceDefinitionPath = dirname($refClass->getFileName());
$classNamePieces = explode('\\', get_called_class());
$serviceDefinitionFile = array_pop($classNamePieces) . '.json';
switch (true) {
case is_readable(dirname($serviceDefinitionPath) . DIRECTORY_SEPARATOR . 'Resources' . DIRECTORY_SEPARATOR . $serviceDefinitionFile):
$serviceDefinition = $serviceDefinitionPath . DIRECTORY_SEPARATOR . 'Resources' . DIRECTORY_SEPARATOR . $serviceDefinitionFile;
break;
case is_readable($serviceDefinitionPath . DIRECTORY_SEPARATOR . $serviceDefinitionFile):
$serviceDefinition = $serviceDefinitionPath . DIRECTORY_SEPARATOR . $serviceDefinitionFile;
break;
default:
throw new Exception\ClientConfigurationException('A client must have a service definition. Could not read the file "' . $serviceDefinition . '"');
}
$description = ServiceDescription::factory($serviceDefinition);
$client->setDescription($description);
return $client;
}
示例11: getServiceDescriptionFromFile
/**
* Loads the service description from the service description file
*
* @param string $description_file The service description file
* @return ServiceDescription
* @throws InvalidArgumentException If the description file doesn't exist or cannot be read
*/
public function getServiceDescriptionFromFile($description_file)
{
if (!file_exists($description_file) || !is_readable($description_file)) {
throw new InvalidArgumentException('Unable to read API definition schema');
}
return ServiceDescription::factory($description_file);
}
示例12: setUp
public function setUp()
{
$mockError = 'Guzzle\\Tests\\Mock\\ErrorResponseMock';
$description = ServiceDescription::factory(array('operations' => array('works' => array('httpMethod' => 'GET', 'errorResponses' => array(array('code' => 500, 'class' => $mockError), array('code' => 503, 'reason' => 'foo', 'class' => $mockError), array('code' => 200, 'reason' => 'Error!', 'class' => $mockError))), 'bad_class' => array('httpMethod' => 'GET', 'errorResponses' => array(array('code' => 500, 'class' => 'Does\\Not\\Exist'))), 'does_not_implement' => array('httpMethod' => 'GET', 'errorResponses' => array(array('code' => 500, 'class' => __CLASS__))), 'no_errors' => array('httpMethod' => 'GET'), 'no_class' => array('httpMethod' => 'GET', 'errorResponses' => array(array('code' => 500))))));
$this->client = new Client($this->getServer()->getUrl());
$this->client->setDescription($description);
}
示例13: factory
/**
* @param array $config
* @return \Guzzle\Service\Client|BasecampClient
* @throws \Guzzle\Common\Exception\InvalidArgumentException
*/
public static function factory($config = array())
{
$default = array('base_url' => 'https://basecamp.com/{user_id}/api/{version}/', 'version' => 'v1', 'auth' => 'http', 'token' => null, 'username' => null, 'password' => null);
$required = array('user_id', 'app_name', 'app_contact');
$config = Collection::fromConfig($config, $default, $required);
$client = new self($config->get('base_url'), $config);
if ($config['auth'] === 'http') {
if (!isset($config['username'], $config['password'])) {
throw new InvalidArgumentException("Config must contain username and password when using http auth");
}
$authorization = 'Basic ' . base64_encode($config['username'] . ':' . $config['password']);
}
if ($config['auth'] === 'oauth') {
if (!isset($config['token'])) {
throw new InvalidArgumentException("Config must contain token when using oauth");
}
$authorization = sprintf('Bearer %s', $config['token']);
}
if (!isset($authorization)) {
throw new InvalidArgumentException("Config must contain valid authentication method");
}
// Attach a service description to the client
$description = ServiceDescription::factory(__DIR__ . '/Resources/service.php');
$client->setDescription($description);
// Set required User-Agent
$client->setUserAgent(sprintf('%s (%s)', $config['app_name'], $config['app_contact']));
$client->getEventDispatcher()->addListener('request.before_send', function (Event $event) use($authorization) {
$event['request']->addHeader('Authorization', $authorization);
});
return $client;
}
示例14: build
/**
* {@inheritdoc}
*/
public function build($config, array $options = null)
{
if (!$this->loader) {
$this->loader = new JsonLoader();
}
return ServiceDescription::factory($this->loader->parseJsonFile($config));
}
示例15: __construct
/**
* Class constructor
*
* Call parent constructor and attach an event listener that in turn will attach listeners to
* the request based on the command being called.
*
* @param string $baseUrl The base URL to Imbo
* @param array|Collection $config Client configuration
*/
public function __construct($baseUrl, $config)
{
parent::__construct($baseUrl, $config);
if (empty($config['serverUrls'])) {
$config['serverUrls'] = array($baseUrl);
}
$this->setServerUrls($config['serverUrls']);
$this->setDescription(ServiceDescription::factory(__DIR__ . '/service.php'));
$this->setUserAgent('ImboClient/' . Version::VERSION, true);
// Attach event listeners that handles the signing of write operations and the appending of
// access tokens to requests that require this
$dispatcher = $this->getEventDispatcher();
$dispatcher->addSubscriber(new EventSubscriber\AccessToken());
$dispatcher->addSubscriber(new EventSubscriber\Authenticate());
$dispatcher->addSubscriber(new EventSubscriber\PublicKey());
$client = $this;
$dispatcher->addListener('command.before_send', function ($event) use($client) {
$client->currentCommand = $event['command']->getName();
});
$dispatcher->addListener('request.error', function ($event) use($client) {
if ($client->currentCommand === 'GetServerStatus') {
// Stop propagation of the event when there is an error with the server status
$event->stopPropagation();
$client->currentCommand = null;
}
});
}