mirror of
https://github.com/guzzle/guzzle.git
synced 2025-02-12 11:30:49 +01:00
299 lines
8.9 KiB
Markdown
299 lines
8.9 KiB
Markdown
|
Guzzle Upgrade Guide
|
||
|
====================
|
||
|
|
||
|
2.8 to 3.x
|
||
|
----------
|
||
|
|
||
|
### Guzzle\Service\Inspector
|
||
|
|
||
|
Change `\Guzzle\Service\Inspector::fromConfig` to `\Guzzle\Common\Collection::fromConfig`
|
||
|
|
||
|
**Before**
|
||
|
|
||
|
```php
|
||
|
use Guzzle\Service\Inspector;
|
||
|
|
||
|
class YourClient extends \Guzzle\Service\Client
|
||
|
{
|
||
|
public static function factory($config = array())
|
||
|
{
|
||
|
$default = array();
|
||
|
$required = array('base_url', 'username', 'api_key');
|
||
|
$config = Inspector::fromConfig($config, $default, $required);
|
||
|
|
||
|
$client = new self(
|
||
|
$config->get('base_url'),
|
||
|
$config->get('username'),
|
||
|
$config->get('api_key')
|
||
|
);
|
||
|
$client->setConfig($config);
|
||
|
|
||
|
$client->setDescription(ServiceDescription::factory(__DIR__ . DIRECTORY_SEPARATOR . 'client.json'));
|
||
|
|
||
|
return $client;
|
||
|
}
|
||
|
```
|
||
|
|
||
|
**After**
|
||
|
|
||
|
```php
|
||
|
use Guzzle\Common\Collection;
|
||
|
|
||
|
class YourClient extends \Guzzle\Service\Client
|
||
|
{
|
||
|
public static function factory($config = array())
|
||
|
{
|
||
|
$default = array();
|
||
|
$required = array('base_url', 'username', 'api_key');
|
||
|
$config = Collection::fromConfig($config, $default, $required);
|
||
|
|
||
|
$client = new self(
|
||
|
$config->get('base_url'),
|
||
|
$config->get('username'),
|
||
|
$config->get('api_key')
|
||
|
);
|
||
|
$client->setConfig($config);
|
||
|
|
||
|
$client->setDescription(ServiceDescription::factory(__DIR__ . DIRECTORY_SEPARATOR . 'client.json'));
|
||
|
|
||
|
return $client;
|
||
|
}
|
||
|
```
|
||
|
|
||
|
### Convert XML Service Descriptions to JSON
|
||
|
|
||
|
**Before**
|
||
|
|
||
|
```xml
|
||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||
|
<client>
|
||
|
<commands>
|
||
|
<!-- Groups -->
|
||
|
<command name="list_groups" method="GET" uri="groups.json">
|
||
|
<doc>Get a list of groups</doc>
|
||
|
</command>
|
||
|
<command name="search_groups" method="GET" uri='search.json?query="{{query}} type:group"'>
|
||
|
<doc>Uses a search query to get a list of groups</doc>
|
||
|
<param name="query" type="string" required="true" />
|
||
|
</command>
|
||
|
<command name="create_group" method="POST" uri="groups.json">
|
||
|
<doc>Create a group</doc>
|
||
|
<param name="data" type="array" location="body" filters="json_encode" doc="Group JSON"/>
|
||
|
<param name="Content-Type" location="header" static="application/json"/>
|
||
|
</command>
|
||
|
<command name="delete_group" method="DELETE" uri="groups/{{id}}.json">
|
||
|
<doc>Delete a group by ID</doc>
|
||
|
<param name="id" type="integer" required="true"/>
|
||
|
</command>
|
||
|
<command name="get_group" method="GET" uri="groups/{{id}}.json">
|
||
|
<param name="id" type="integer" required="true"/>
|
||
|
</command>
|
||
|
<command name="update_group" method="PUT" uri="groups/{{id}}.json">
|
||
|
<doc>Update a group</doc>
|
||
|
<param name="id" type="integer" required="true"/>
|
||
|
<param name="data" type="array" location="body" filters="json_encode" doc="Group JSON"/>
|
||
|
<param name="Content-Type" location="header" static="application/json"/>
|
||
|
</command>
|
||
|
</commands>
|
||
|
</client>
|
||
|
```
|
||
|
|
||
|
**After**
|
||
|
|
||
|
```json
|
||
|
{
|
||
|
"name": "Zendesk REST API v2",
|
||
|
"apiVersion": "2012-12-31",
|
||
|
"description":"Provides access to Zendesk views, groups, tickets, ticket fields, and users",
|
||
|
"operations": {
|
||
|
"list_groups": {
|
||
|
"httpMethod":"GET",
|
||
|
"uri": "groups.json",
|
||
|
"summary": "Get a list of groups"
|
||
|
},
|
||
|
"search_groups":{
|
||
|
"httpMethod":"GET",
|
||
|
"uri": "search.json?query=\"{query} type:group\"",
|
||
|
"summary": "Uses a search query to get a list of groups",
|
||
|
"parameters":{
|
||
|
"query":{
|
||
|
"location": "uri",
|
||
|
"description":"Zendesk Search Query",
|
||
|
"type": "string",
|
||
|
"required": true
|
||
|
}
|
||
|
}
|
||
|
},
|
||
|
"create_group": {
|
||
|
"httpMethod":"POST",
|
||
|
"uri": "groups.json",
|
||
|
"summary": "Create a group",
|
||
|
"parameters":{
|
||
|
"data": {
|
||
|
"type": "array",
|
||
|
"location": "body",
|
||
|
"description":"Group JSON",
|
||
|
"filters": "json_encode",
|
||
|
"required": true
|
||
|
},
|
||
|
"Content-Type":{
|
||
|
"type": "string",
|
||
|
"location":"header",
|
||
|
"static": "application/json"
|
||
|
}
|
||
|
}
|
||
|
},
|
||
|
"delete_group": {
|
||
|
"httpMethod":"DELETE",
|
||
|
"uri": "groups/{id}.json",
|
||
|
"summary": "Delete a group",
|
||
|
"parameters":{
|
||
|
"id":{
|
||
|
"location": "uri",
|
||
|
"description":"Group to delete by ID",
|
||
|
"type": "integer",
|
||
|
"required": true
|
||
|
}
|
||
|
}
|
||
|
},
|
||
|
"get_group": {
|
||
|
"httpMethod":"GET",
|
||
|
"uri": "groups/{id}.json",
|
||
|
"summary": "Get a ticket",
|
||
|
"parameters":{
|
||
|
"id":{
|
||
|
"location": "uri",
|
||
|
"description":"Group to get by ID",
|
||
|
"type": "integer",
|
||
|
"required": true
|
||
|
}
|
||
|
}
|
||
|
},
|
||
|
"update_group": {
|
||
|
"httpMethod":"PUT",
|
||
|
"uri": "groups/{id}.json",
|
||
|
"summary": "Update a group",
|
||
|
"parameters":{
|
||
|
"id": {
|
||
|
"location": "uri",
|
||
|
"description":"Group to update by ID",
|
||
|
"type": "integer",
|
||
|
"required": true
|
||
|
},
|
||
|
"data": {
|
||
|
"type": "array",
|
||
|
"location": "body",
|
||
|
"description":"Group JSON",
|
||
|
"filters": "json_encode",
|
||
|
"required": true
|
||
|
},
|
||
|
"Content-Type":{
|
||
|
"type": "string",
|
||
|
"location":"header",
|
||
|
"static": "application/json"
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
```
|
||
|
|
||
|
### Guzzle\Service\Description\ServiceDescription
|
||
|
|
||
|
Commands are now called Operations
|
||
|
|
||
|
**Before**
|
||
|
|
||
|
```php
|
||
|
use Guzzle\Service\Description\ServiceDescription;
|
||
|
|
||
|
$sd = new ServiceDescription();
|
||
|
$sd->getCommands(); // @returns ApiCommandInterface[]
|
||
|
$sd->hasCommand($name);
|
||
|
$sd->getCommand($name); // @returns ApiCommandInterface|null
|
||
|
$sd->addCommand($command); // @param ApiCommandInterface $command
|
||
|
```
|
||
|
|
||
|
**After**
|
||
|
|
||
|
```php
|
||
|
use Guzzle\Service\Description\ServiceDescription;
|
||
|
|
||
|
$sd = new ServiceDescription();
|
||
|
$sd->getOperations(); // @returns OperationInterface[]
|
||
|
$sd->hasOperation($name);
|
||
|
$sd->getOperation($name); // @returns OperationInterface|null
|
||
|
$sd->addOperation($operation); // @param OperationInterface $operation
|
||
|
```
|
||
|
|
||
|
### Guzzle\Common\Inflection\Inflector
|
||
|
|
||
|
Namespace is now `Guzzle\Inflection\Inflector`
|
||
|
|
||
|
### Guzzle\Http\Plugin
|
||
|
|
||
|
Namespace is now `Guzzle\Plugin`. Many other changes occur within this namespace and are detailed in their own sections below.
|
||
|
|
||
|
### Guzzle\Http\Plugin\LogPlugin and Guzzle\Common\Log
|
||
|
|
||
|
Now `Guzzle\Plugin\Log\LogPlugin` and `Guzzle\Log` respectively.
|
||
|
|
||
|
**Before**
|
||
|
|
||
|
```php
|
||
|
use Guzzle\Common\Log\ClosureLogAdapter;
|
||
|
use Guzzle\Http\Plugin\LogPlugin;
|
||
|
|
||
|
/** @var \Guzzle\Http\Client */
|
||
|
$client;
|
||
|
|
||
|
// $verbosity is an integer indicating desired message verbosity level
|
||
|
$client->addSubscriber(new LogPlugin(new ClosureLogAdapter(function($m) { echo $m; }, $verbosity = LogPlugin::LOG_VERBOSE);
|
||
|
```
|
||
|
|
||
|
**After**
|
||
|
|
||
|
```php
|
||
|
use Guzzle\Log\ClosureLogAdapter;
|
||
|
use Guzzle\Log\MessageFormatter;
|
||
|
use Guzzle\Plugin\Log\LogPlugin;
|
||
|
|
||
|
/** @var \Guzzle\Http\Client */
|
||
|
$client;
|
||
|
|
||
|
// $format is a string indicating desired message format -- @see MessageFormatter
|
||
|
$client->addSubscriber(new LogPlugin(new ClosureLogAdapter(function($m) { echo $m; }, $format = MessageFormatter::DEBUG_FORMAT);
|
||
|
```
|
||
|
|
||
|
### Guzzle\Http\Plugin\CurlAuthPlugin
|
||
|
|
||
|
Now `Guzzle\Plugin\CurlAuth\CurlAuthPlugin`.
|
||
|
|
||
|
### Guzzle\Http\Plugin\ExponentialBackoffPlugin
|
||
|
|
||
|
Now `Guzzle\Plugin\Backoff\BackoffPlugin`, and other changes.
|
||
|
|
||
|
**Before**
|
||
|
|
||
|
```php
|
||
|
use Guzzle\Http\Plugin\ExponentialBackoffPlugin;
|
||
|
|
||
|
$backoffPlugin = new ExponentialBackoffPlugin($maxRetries, array_merge(
|
||
|
ExponentialBackoffPlugin::getDefaultFailureCodes(), array(429)
|
||
|
));
|
||
|
|
||
|
$client->addSubscriber($backoffPlugin);
|
||
|
```
|
||
|
|
||
|
**After**
|
||
|
|
||
|
```php
|
||
|
use Guzzle\Plugin\Backoff\BackoffPlugin;
|
||
|
use Guzzle\Plugin\Backoff\HttpBackoffStrategy;
|
||
|
|
||
|
// Use convenient factory method instead -- see implementation for ideas of what
|
||
|
// you can do with chaining backoff strategies
|
||
|
$backoffPlugin = BackoffPlugin::getExponentialBackoff($maxRetries, array_merge(
|
||
|
HttpBackoffStrategy::getDefaultFailureCodes(), array(429)
|
||
|
));
|
||
|
$client->addSubscriber($backoffPlugin);
|
||
|
```
|