From b4728cd6e980d36287b6a66bb12a8d17b1878880 Mon Sep 17 00:00:00 2001 From: Ronan Giron Date: Wed, 16 Dec 2020 12:38:51 +0100 Subject: [PATCH 1/3] Savings HTTP exchanges with HEAD request --- src/Client.php | 70 +++++++++++++++++++++++++++++++------ src/GitlabAdapter.php | 8 ++--- tests/ClientTest.php | 7 ++-- tests/GitlabAdapterTest.php | 2 +- 4 files changed, 68 insertions(+), 19 deletions(-) diff --git a/src/Client.php b/src/Client.php index a74c13e..9eda82c 100644 --- a/src/Client.php +++ b/src/Client.php @@ -50,6 +50,30 @@ public function __construct(string $projectId, string $branch, string $baseUrl, $this->baseUrl = $baseUrl; $this->personalAccessToken = $personalAccessToken; } + + /** + * @param $path + * + * @return array + * @throws \GuzzleHttp\Exception\GuzzleException + */ + public function head($path) + { + $path = urlencode($path); + + $response = $this->request('HEAD', "files/$path"); + + $headers = $response->getHeaders(); + $headers = array_filter( + $headers, + function ($key) { + return substr($key, 0, 9) == 'X-Gitlab-'; + }, + ARRAY_FILTER_USE_KEY + ); + + return $headers; + } /** * @param $path @@ -69,16 +93,43 @@ 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, + function ($key) { + return 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', + function($matches) { + return strtoupper($matches[1]); + }, + $key + ); + } + ); + + return array_combine($keys, $values); } /** @@ -238,12 +289,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); } diff --git a/src/GitlabAdapter.php b/src/GitlabAdapter.php index 9633206..94accbc 100644 --- a/src/GitlabAdapter.php +++ b/src/GitlabAdapter.php @@ -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); } @@ -283,9 +281,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); } diff --git a/tests/ClientTest.php b/tests/ClientTest.php index 0f405fb..17a020b 100644 --- a/tests/ClientTest.php +++ b/tests/ClientTest.php @@ -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); } /** diff --git a/tests/GitlabAdapterTest.php b/tests/GitlabAdapterTest.php index 1ebc237..a0805c9 100644 --- a/tests/GitlabAdapterTest.php +++ b/tests/GitlabAdapterTest.php @@ -78,7 +78,7 @@ public function it_can_read_a_file_into_a_stream() $stream = $this->gitlabAdapter->readStream('README.md'); $this->assertIsResource($stream); - $this->assertEquals(stream_get_contents($stream, null, 0), $this->gitlabAdapter->read('README.md')); + $this->assertEquals(stream_get_contents($stream, -1, 0), $this->gitlabAdapter->read('README.md')); } /** From 0ad3cbf314903240d42bfafdb398e35b7cacd706 Mon Sep 17 00:00:00 2001 From: Ronan Giron Date: Wed, 16 Dec 2020 12:45:31 +0100 Subject: [PATCH 2/3] Remove unused method --- src/Client.php | 24 ------------------------ 1 file changed, 24 deletions(-) diff --git a/src/Client.php b/src/Client.php index 9eda82c..a18ae33 100644 --- a/src/Client.php +++ b/src/Client.php @@ -50,30 +50,6 @@ public function __construct(string $projectId, string $branch, string $baseUrl, $this->baseUrl = $baseUrl; $this->personalAccessToken = $personalAccessToken; } - - /** - * @param $path - * - * @return array - * @throws \GuzzleHttp\Exception\GuzzleException - */ - public function head($path) - { - $path = urlencode($path); - - $response = $this->request('HEAD', "files/$path"); - - $headers = $response->getHeaders(); - $headers = array_filter( - $headers, - function ($key) { - return substr($key, 0, 9) == 'X-Gitlab-'; - }, - ARRAY_FILTER_USE_KEY - ); - - return $headers; - } /** * @param $path From b8621c393f30c75cd77f0cc6a1bd4e0ce6e97d97 Mon Sep 17 00:00:00 2001 From: RoyVoetman Date: Wed, 16 Dec 2020 14:57:11 +0100 Subject: [PATCH 3/3] Added arrow functions --- src/Client.php | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/Client.php b/src/Client.php index a18ae33..a7f3165 100644 --- a/src/Client.php +++ b/src/Client.php @@ -81,9 +81,7 @@ public function read($path) $headers = $response->getHeaders(); $headers = array_filter( $headers, - function ($key) { - return substr($key, 0, 9) == 'X-Gitlab-'; - }, + fn($key) => substr($key, 0, 9) == 'X-Gitlab-', ARRAY_FILTER_USE_KEY ); @@ -97,9 +95,7 @@ function(&$key) { $key = strtolower($key); $key = preg_replace_callback( '/[-_]+(.)?/i', - function($matches) { - return strtoupper($matches[1]); - }, + fn($matches) => strtoupper($matches[ 1 ]), $key ); }