Skip to content

Commit

Permalink
Merge pull request #13 from ElGigi/savings
Browse files Browse the repository at this point in the history
Savings HTTP exchanges with HEAD request
  • Loading branch information
RoyVoetman authored Dec 16, 2020
2 parents d55bce7 + b8621c3 commit 4172ba1
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 18 deletions.
42 changes: 32 additions & 10 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,39 @@ public function readRaw(string $path): string
/**
* @param $path
*
* @return mixed|string
* @return array
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function read($path)
{
$path = urlencode($path);

$response = $this->request('GET', "files/$path");

return $this->responseContents($response);

$response = $this->request('HEAD', "files/$path");

$headers = $response->getHeaders();
$headers = array_filter(
$headers,
fn($key) => substr($key, 0, 9) == 'X-Gitlab-',
ARRAY_FILTER_USE_KEY
);

$keys = array_keys($headers);
$values = array_values($headers);

array_walk(
$keys,
function(&$key) {
$key = substr($key, 9);
$key = strtolower($key);
$key = preg_replace_callback(
'/[-_]+(.)?/i',
fn($matches) => strtoupper($matches[ 1 ]),
$key
);
}
);

return array_combine($keys, $values);
}

/**
Expand Down Expand Up @@ -254,12 +277,11 @@ public function setBranch(string $branch)
*/
private function request(string $method, string $uri, array $params = []): Response
{
$uri = ($method === 'GET') ? $this->buildUri($uri, $params) : $this->buildUri($uri);

$params = ($method !== 'GET') ? ['form_params' => array_merge(['branch' => $this->branch], $params)] : [];

$uri = !in_array($method, ['POST', 'PUT', 'DELETE']) ? $this->buildUri($uri, $params) : $this->buildUri($uri);
$params = in_array($method, ['POST', 'PUT', 'DELETE']) ? ['form_params' => array_merge(['branch' => $this->branch], $params)] : [];

$client = new HttpClient(['headers' => ['PRIVATE-TOKEN' => $this->personalAccessToken]]);

return $client->request($method, $uri, $params);
}

Expand Down
8 changes: 3 additions & 5 deletions src/GitlabAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,7 @@ public function writeStream(string $path, $contents, Config $config): void
public function read(string $path): string
{
try {
$response = $this->client->read($this->prefixer->prefixPath($path));

return base64_decode($response['content']);
return $this->client->readRaw($this->prefixer->prefixPath($path));
} catch (Throwable $e) {
throw UnableToReadFile::fromLocation($path, $e->getMessage(), $e);
}
Expand Down Expand Up @@ -281,9 +279,9 @@ public function lastModified(string $path): FileAttributes
public function fileSize(string $path): FileAttributes
{
try {
$response = $this->client->read($this->prefixer->prefixPath($path));
$meta = $this->client->read($this->prefixer->prefixPath($path));

return new FileAttributes($path, $response['size']);
return new FileAttributes($path, $meta['size'][0] ?? 0);
} catch (Throwable $e) {
throw UnableToRetrieveMetadata::fileSize($path, $e->getMessage(), $e);
}
Expand Down
7 changes: 4 additions & 3 deletions tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,11 @@ public function it_can_be_instantiated()
*/
public function it_can_read_a_file()
{
$file = $this->client->read('README.md');
$meta = $this->client->read('README.md');

$this->assertStringStartsWith(base64_encode('# Testing repo for `flysystem-gitlab`'),
$file[ 'content' ]);
$this->assertArrayHasKey('ref', $meta);
$this->assertArrayHasKey('size', $meta);
$this->assertArrayHasKey('lastCommitId', $meta);
}

/**
Expand Down

0 comments on commit 4172ba1

Please sign in to comment.