Refactors ClientInterface and Guzzle adapter.
This patch achieves three interrelated changes 1. an update to the ClientInterface, making it more efficient and consistent with other HTTP libraries. PSR-FIG messages have also been added as separate classes, rather than lumped as one. Guzzle functionality has also been moved to its own namespace; 2. the refactoring of `GuzzleClient` to `GuzzleAdapter`, including relevant changes needed for the interface change (1). We now have ADAPTERS that bridge our interfaces with Guzzle's - making that difference much clearer, extensible, less tightly coupled and less brittle; 3. moving "bad request" error handling into its own dedicated space, based on how the new Adapter functionality (2). Previously the client tried to do all the exception logic - but this is not strictly its responsibility. This logic has been shipped out to a base RequestException which instantiates one of its children exception based on the HTTP status code. Although I have attempted to keep the scope of this patch to a minimum, as granular as possible, because the interface is a core internal API, various other files need to be modified to reflect the change. In terms of the other two changes, these are inextricably linked to the interface change, so cannot be teased out into their own patches. Change-Id: Ibc1b50cec125c11d32ee6e4f0dbb395fcaea864e
This commit is contained in:
@@ -19,8 +19,8 @@
|
||||
*/
|
||||
|
||||
namespace OpenStack\Identity\v2;
|
||||
|
||||
use OpenStack\Common\Transport\GuzzleClient;
|
||||
use OpenStack\Common\Transport\ClientInterface;
|
||||
use OpenStack\Common\Transport\Guzzle\GuzzleAdapter;
|
||||
|
||||
/**
|
||||
* IdentityService provides authentication and authorization.
|
||||
@@ -203,7 +203,7 @@ class IdentityService
|
||||
*
|
||||
* @param \OpenStack\Common\Transport\ClientInterface $client An optional HTTP client to use when making the requests.
|
||||
*/
|
||||
public function __construct($url, \OpenStack\Common\Transport\ClientInterface $client = null)
|
||||
public function __construct($url, ClientInterface $client = null)
|
||||
{
|
||||
$parts = parse_url($url);
|
||||
|
||||
@@ -215,7 +215,7 @@ class IdentityService
|
||||
|
||||
// Guzzle is the default client to use.
|
||||
if (is_null($client)) {
|
||||
$this->client = new GuzzleClient();
|
||||
$this->client = GuzzleAdapter::create();
|
||||
} else {
|
||||
$this->client = $client;
|
||||
}
|
||||
@@ -278,13 +278,13 @@ class IdentityService
|
||||
|
||||
$body = json_encode($envelope);
|
||||
|
||||
$headers = array(
|
||||
'Content-Type' => 'application/json',
|
||||
'Accept' => self::ACCEPT_TYPE,
|
||||
$headers = [
|
||||
'Content-Type' => 'application/json',
|
||||
'Accept' => self::ACCEPT_TYPE,
|
||||
'Content-Length' => strlen($body),
|
||||
);
|
||||
];
|
||||
|
||||
$response = $this->client->doRequest($url, 'POST', $headers, $body);
|
||||
$response = $this->client->post($url, $body, ['headers' => $headers]);
|
||||
|
||||
$this->handleResponse($response);
|
||||
|
||||
@@ -327,7 +327,7 @@ class IdentityService
|
||||
'passwordCredentials' => array(
|
||||
'username' => $username,
|
||||
'password' => $password,
|
||||
),
|
||||
)
|
||||
);
|
||||
|
||||
// If a tenant ID is provided, added it to the auth array.
|
||||
@@ -599,18 +599,14 @@ class IdentityService
|
||||
$token = $this->token();
|
||||
}
|
||||
|
||||
$headers = array(
|
||||
$headers = [
|
||||
'X-Auth-Token' => $token,
|
||||
'Accept' => 'application/json',
|
||||
//'Content-Type' => 'application/json',
|
||||
);
|
||||
'Accept' => 'application/json'
|
||||
];
|
||||
|
||||
$response = $this->client->doRequest($url, 'GET', $headers);
|
||||
|
||||
$json = $response->json();
|
||||
|
||||
return $json['tenants'];
|
||||
$response = $this->client->get($url, ['headers' => $headers]);
|
||||
|
||||
return $response->json()['tenants'];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -644,25 +640,24 @@ class IdentityService
|
||||
public function rescopeUsingTenantId($tenantId)
|
||||
{
|
||||
$url = $this->url() . '/tokens';
|
||||
$token = $this->token();
|
||||
$data = array(
|
||||
'auth' => array(
|
||||
|
||||
$body = json_encode([
|
||||
'auth' => [
|
||||
'tenantId' => $tenantId,
|
||||
'token' => array(
|
||||
'id' => $token,
|
||||
),
|
||||
),
|
||||
);
|
||||
$body = json_encode($data);
|
||||
'token' => [
|
||||
'id' => $this->token(),
|
||||
]
|
||||
]
|
||||
]);
|
||||
|
||||
$headers = array(
|
||||
'Accept' => self::ACCEPT_TYPE,
|
||||
'Content-Type' => 'application/json',
|
||||
'Content-Length' => strlen($body),
|
||||
//'X-Auth-Token' => $token,
|
||||
);
|
||||
$headers = [
|
||||
'Accept' => self::ACCEPT_TYPE,
|
||||
'Content-Type' => 'application/json',
|
||||
'Content-Length' => strlen($body)
|
||||
];
|
||||
|
||||
$response = $this->client->post($url, $body, ['headers' => $headers]);
|
||||
|
||||
$response = $this->client->doRequest($url, 'POST', $headers, $body);
|
||||
$this->handleResponse($response);
|
||||
|
||||
return $this->token();
|
||||
@@ -699,25 +694,24 @@ class IdentityService
|
||||
public function rescopeUsingTenantName($tenantName)
|
||||
{
|
||||
$url = $this->url() . '/tokens';
|
||||
$token = $this->token();
|
||||
$data = array(
|
||||
'auth' => array(
|
||||
|
||||
$body = json_encode([
|
||||
'auth' => [
|
||||
'tenantName' => $tenantName,
|
||||
'token' => array(
|
||||
'id' => $token,
|
||||
),
|
||||
),
|
||||
);
|
||||
$body = json_encode($data);
|
||||
'token' => [
|
||||
'id' => $this->token()
|
||||
]
|
||||
]
|
||||
]);
|
||||
|
||||
$headers = array(
|
||||
'Accept' => self::ACCEPT_TYPE,
|
||||
'Content-Type' => 'application/json',
|
||||
'Content-Length' => strlen($body),
|
||||
//'X-Auth-Token' => $token,
|
||||
);
|
||||
$headers = [
|
||||
'Accept' => self::ACCEPT_TYPE,
|
||||
'Content-Type' => 'application/json',
|
||||
'Content-Length' => strlen($body)
|
||||
];
|
||||
|
||||
$response = $this->client->post($url, $body, ['headers' => $headers]);
|
||||
|
||||
$response = $this->client->doRequest($url, 'POST', $headers, $body);
|
||||
$this->handleResponse($response);
|
||||
|
||||
return $this->token();
|
||||
|
||||
Reference in New Issue
Block a user