Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support configured headers as request options #4

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@ public function getHttpClient(): HttpClientInterface
return $this->httpClient;
}

public function getConfig(): Config
{
return $this->config;
}

/**
* Get S3 client.
*/
Expand Down
4 changes: 4 additions & 0 deletions src/Resources/AbstractResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use LBausch\CephRadosgwAdmin\ApiRequest;
use LBausch\CephRadosgwAdmin\Client;
use LBausch\CephRadosgwAdmin\Config;
use LBausch\CephRadosgwAdmin\Contracts\ResourceContract;

abstract class AbstractResource implements ResourceContract
Expand All @@ -18,9 +19,12 @@ abstract class AbstractResource implements ResourceContract
*/
protected ApiRequest $api;

protected Config $config;

final public function __construct(Client $client)
{
$this->api = ApiRequest::make($client->getHttpClient());
$this->config = $client->getConfig();
}

/**
Expand Down
8 changes: 8 additions & 0 deletions src/Resources/Bucket.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public function info(array $data): ApiResponse
{
return $this->api->get($this->endpoint, [
RequestOptions::QUERY => $data,
RequestOptions::HEADERS => $this->config->get('httpClientHeaders'),
]);
}

Expand All @@ -43,6 +44,7 @@ public function remove(string $bucket, array $data = []): ApiResponse
{
return $this->api->delete($this->endpoint, [
RequestOptions::QUERY => array_merge(['bucket' => $bucket], $data),
RequestOptions::HEADERS => $this->config->get('httpClientHeaders'),
]);
}

Expand All @@ -58,6 +60,7 @@ public function check(string $bucket, array $data = []): ApiResponse
'index' => '',
'bucket' => $bucket,
], $data),
RequestOptions::HEADERS => $this->config->get('httpClientHeaders'),
]);
}

Expand All @@ -73,6 +76,7 @@ public function link(string $bucket, string $uid, array $data = []): ApiResponse
'bucket' => $bucket,
'uid' => $uid,
], $data),
RequestOptions::HEADERS => $this->config->get('httpClientHeaders'),
]);
}

Expand All @@ -88,6 +92,7 @@ public function unlink(string $bucket, string $uid): ApiResponse
'bucket' => $bucket,
'uid' => $uid,
],
RequestOptions::HEADERS => $this->config->get('httpClientHeaders'),
]);
}

Expand All @@ -103,6 +108,7 @@ public function policy(string $bucket, array $data = []): ApiResponse
'policy' => '',
'bucket' => $bucket,
], $data),
RequestOptions::HEADERS => $this->config->get('httpClientHeaders'),
]);
}

Expand All @@ -118,6 +124,7 @@ public function removeObject(string $bucket, string $object): ApiResponse
'bucket' => $bucket,
'object' => $object,
],
RequestOptions::HEADERS => $this->config->get('httpClientHeaders'),
]);
}

Expand All @@ -136,6 +143,7 @@ public function setQuota(string $uid, string $bucket, array $quota): ApiResponse
],
RequestOptions::BODY => json_encode($quota),
AbstractSignature::SIGNATURE_OPTION => SignatureV2::class,
RequestOptions::HEADERS => $this->config->get('httpClientHeaders'),
]);
}
}
2 changes: 2 additions & 0 deletions src/Resources/Usage.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public function info(array $data = []): ApiResponse
{
return $this->api->get($this->endpoint, [
RequestOptions::QUERY => $data,
RequestOptions::HEADERS => $this->config->get('httpClientHeaders'),
]);
}

Expand All @@ -33,6 +34,7 @@ public function trim(array $data = []): ApiResponse
{
return $this->api->delete($this->endpoint, [
RequestOptions::QUERY => $data,
RequestOptions::HEADERS => $this->config->get('httpClientHeaders'),
]);
}
}
15 changes: 15 additions & 0 deletions src/Resources/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public function info(string $uid): ApiResponse
RequestOptions::QUERY => [
'uid' => $uid,
],
RequestOptions::HEADERS => $this->config->get('httpClientHeaders'),
]);
}

Expand All @@ -48,6 +49,7 @@ public function create(string $uid, string $displayName, array $data = []): ApiR
'uid' => $uid,
'display-name' => $displayName,
], $data),
RequestOptions::HEADERS => $this->config->get('httpClientHeaders'),
AbstractSignature::SIGNATURE_OPTION => SignatureV2::class,
]);
}
Expand All @@ -63,6 +65,7 @@ public function modify(string $uid, array $data = []): ApiResponse
RequestOptions::QUERY => array_merge([
'uid' => $uid,
], $data),
RequestOptions::HEADERS => $this->config->get('httpClientHeaders'),
]);
}

Expand All @@ -78,6 +81,7 @@ public function remove(string $uid, bool $purgeData = false): ApiResponse
'uid' => $uid,
'purge-data' => $purgeData,
],
RequestOptions::HEADERS => $this->config->get('httpClientHeaders'),
]);
}

Expand All @@ -93,6 +97,7 @@ public function createKey(string $uid, array $data = []): ApiResponse
'key' => '',
'uid' => $uid,
], $data),
RequestOptions::HEADERS => $this->config->get('httpClientHeaders'),
]);
}

Expand All @@ -108,6 +113,7 @@ public function removeKey(string $accessKey, array $data = []): ApiResponse
'key' => '',
'access-key' => $accessKey,
], $data),
RequestOptions::HEADERS => $this->config->get('httpClientHeaders'),
]);
}

Expand All @@ -123,6 +129,7 @@ public function createSubuser(string $uid, string $subuser, array $data = []): A
'uid' => $uid,
'subuser' => $subuser,
], $data),
RequestOptions::HEADERS => $this->config->get('httpClientHeaders'),
]);
}

Expand All @@ -138,6 +145,7 @@ public function modifySubuser(string $uid, string $subuser, array $data = []): A
'uid' => $uid,
'subuser' => $subuser,
], $data),
RequestOptions::HEADERS => $this->config->get('httpClientHeaders'),
]);
}

Expand All @@ -153,6 +161,7 @@ public function removeSubuser(string $uid, string $subuser, array $data = []): A
'uid' => $uid,
'subuser' => $subuser,
], $data),
RequestOptions::HEADERS => $this->config->get('httpClientHeaders'),
]);
}

Expand All @@ -169,6 +178,7 @@ public function addCapability(string $uid, string $userCaps): ApiResponse
'uid' => $uid,
'user-caps' => $userCaps,
],
RequestOptions::HEADERS => $this->config->get('httpClientHeaders'),
]);
}

Expand All @@ -185,6 +195,7 @@ public function removeCapability(string $uid, string $userCaps): ApiResponse
'uid' => $uid,
'user-caps' => $userCaps,
],
RequestOptions::HEADERS => $this->config->get('httpClientHeaders'),
]);
}

Expand All @@ -201,6 +212,7 @@ public function getQuota(string $uid): ApiResponse
'uid' => $uid,
'quota-type' => 'user',
],
RequestOptions::HEADERS => $this->config->get('httpClientHeaders'),
]);
}

Expand All @@ -219,6 +231,7 @@ public function setQuota(string $uid, array $quota): ApiResponse
],
RequestOptions::BODY => json_encode($quota),
AbstractSignature::SIGNATURE_OPTION => SignatureV2::class,
RequestOptions::HEADERS => $this->config->get('httpClientHeaders'),
]);
}

Expand All @@ -235,6 +248,7 @@ public function getBucketQuota(string $uid): ApiResponse
'uid' => $uid,
'quota-type' => 'bucket',
],
RequestOptions::HEADERS => $this->config->get('httpClientHeaders'),
]);
}

Expand All @@ -253,6 +267,7 @@ public function setBucketQuota(string $uid, array $quota): ApiResponse
],
RequestOptions::BODY => json_encode($quota),
AbstractSignature::SIGNATURE_OPTION => SignatureV2::class,
RequestOptions::HEADERS => $this->config->get('httpClientHeaders'),
]);
}
}
50 changes: 50 additions & 0 deletions tests/ApiRequestResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Tests;

use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;
use LBausch\CephRadosgwAdmin\ApiException;
use LBausch\CephRadosgwAdmin\Client;
Expand Down Expand Up @@ -49,6 +50,55 @@ public function testRequestAndResponse(): void
$this->assertSame('baz', $response->get('foobar', 'baz'));
}

/**
* @covers \LBausch\CephRadosgwAdmin\ApiRequest::__construct
* @covers \LBausch\CephRadosgwAdmin\ApiRequest::get
* @covers \LBausch\CephRadosgwAdmin\ApiRequest::make
* @covers \LBausch\CephRadosgwAdmin\ApiRequest::request
* @covers \LBausch\CephRadosgwAdmin\ApiResponse::__construct
* @covers \LBausch\CephRadosgwAdmin\ApiResponse::failed
* @covers \LBausch\CephRadosgwAdmin\ApiResponse::fromResponse
* @covers \LBausch\CephRadosgwAdmin\ApiResponse::get
* @covers \LBausch\CephRadosgwAdmin\ApiResponse::getResponse
* @covers \LBausch\CephRadosgwAdmin\ApiResponse::has
* @covers \LBausch\CephRadosgwAdmin\ApiResponse::shouldThrowException
* @covers \LBausch\CephRadosgwAdmin\ApiResponse::succeeded
* @covers \LBausch\CephRadosgwAdmin\Client
* @covers \LBausch\CephRadosgwAdmin\Config
* @covers \LBausch\CephRadosgwAdmin\Middlewares\SignatureMiddleware
* @covers \LBausch\CephRadosgwAdmin\Resources\AbstractResource
* @covers \LBausch\CephRadosgwAdmin\Resources\User
* @covers \LBausch\CephRadosgwAdmin\Signature\SignatureV4
*/
public function testRequestAndResponseWithHostHeaders(): void
{
$transactions = [];

$config = $this->getConfigWithMockedHandlers($transactions, [
new Response(200, [], <<<'EOT'
{
"tenant": "baz",
"user_id": "foo",
"display_name": "bazfoo"
}
EOT),
]);
$config->set('httpClientHeaders', ['Host' => 'Bar']);

$client = Client::make('http://gateway:8000', 'accesskey', 'secretkey', $config);

$response = $client->user()->info('baz$foo');

$this->assertCount(1, $transactions);

/** @var Request $request */
$request = $transactions[0]['request'];

$headers = $request->getHeaders();

$this->assertSame('Bar', $headers['Host'][0]);
}

/**
* @covers \LBausch\CephRadosgwAdmin\ApiRequest::__construct
* @covers \LBausch\CephRadosgwAdmin\ApiRequest::delete
Expand Down