From 77ac906960f6d6c21c2b8a879350e47f325829b8 Mon Sep 17 00:00:00 2001 From: achyutkneupane Date: Thu, 30 Oct 2025 15:01:09 +0545 Subject: [PATCH 1/6] refactor: updates HamroCDN client to include X-Upload-Medium header Modifies the HamroCDN client to add a custom header for upload medium. Also updates test cases to use the .test domain for consistency. --- src/HamroCDN.php | 11 +++++++++++ tests/UploadTest.php | 24 ++++++++++++------------ 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/src/HamroCDN.php b/src/HamroCDN.php index 048477c..7dd404c 100644 --- a/src/HamroCDN.php +++ b/src/HamroCDN.php @@ -36,6 +36,17 @@ public function __construct(?string $apiKey = null, ?string $baseUrl = null, ?Cl 'Accept' => 'application/json', ], ]); + + $currentConfig = $this->client->getConfig(); + $currentHeaders = $currentConfig['headers'] ?? []; + + $this->client = new Client( + array_merge($currentConfig, [ + 'headers' => array_merge($currentHeaders, [ + 'X-Upload-Medium' => 'php_sdk', + ]), + ]) + ); } /** diff --git a/tests/UploadTest.php b/tests/UploadTest.php index 035f6cf..7627116 100644 --- a/tests/UploadTest.php +++ b/tests/UploadTest.php @@ -33,11 +33,11 @@ 'nanoId' => 'abc123', 'user' => [ 'name' => 'John Doe', - 'email' => 'john@hamrocdn.com', + 'email' => 'john@hamrocdn.test', ], 'delete_at' => null, 'original' => [ - 'url' => 'https://hamrocdn.com/abc123.png', + 'url' => 'https://hamrocdn.test/abc123.png', 'size' => 2048, ], ]; @@ -48,14 +48,14 @@ expect($upload->getNanoId())->toBe('abc123'); expect($upload->getDeleteAt())->toBeNull(); - expect($upload->getOriginal()->getUrl())->toBe('https://hamrocdn.com/abc123.png'); + expect($upload->getOriginal()->getUrl())->toBe('https://hamrocdn.test/abc123.png'); expect($upload->getOriginal()->getSize())->toBe(2048); expect($upload->getUser()?->getName())->toBe('John Doe'); - expect($upload->getUser()?->getEmail())->toBe('john@hamrocdn.com'); + expect($upload->getUser()?->getEmail())->toBe('john@hamrocdn.test'); }); it('returns an array of HamroCDN objects from index', function () { - $client = new HamroCDN('test-api-key', 'https://hamrocdn.com/api'); + $client = new HamroCDN('test-api-key', 'https://hamrocdn.test/api'); $uploads = $client->index(); @@ -75,7 +75,7 @@ }); it('uploads a file and returns a HamroCDN object', function () { - $client = new HamroCDN('test-api-key', 'https://hamrocdn.com/api'); + $client = new HamroCDN('test-api-key', 'https://hamrocdn.test/api'); $filePath = __DIR__.'/test.png'; $upload = $client->upload($filePath); @@ -91,7 +91,7 @@ }); it('uploads a file by URL and returns a HamroCDN object', function () { - $client = new HamroCDN('test-api-key', 'https://hamrocdn.com/api'); + $client = new HamroCDN('test-api-key', 'https://hamrocdn.test/api'); $fileUrl = 'https://placehold.co/1000x1000/000000/FFFFFF?text=HamroCDN'; @@ -111,7 +111,7 @@ }); it('throws exception when uploading a non-existing file', function () { - $client = new HamroCDN('test-api-key', 'https://hamrocdn.com/api'); + $client = new HamroCDN('test-api-key', 'https://hamrocdn.test/api'); $filePath = __DIR__.'/non-existing-file.png'; @@ -126,7 +126,7 @@ $handlerStack = GuzzleHttp\HandlerStack::create($mockHandler); $guzzleClient = new GuzzleHttp\Client(['handler' => $handlerStack]); - $client = new HamroCDN('test-api-key', 'https://hamrocdn.com/api', $guzzleClient); + $client = new HamroCDN('test-api-key', 'https://hamrocdn.test/api', $guzzleClient); $this->expectException(HamroCDNException::class); $client->index(); @@ -139,7 +139,7 @@ $handlerStack = GuzzleHttp\HandlerStack::create($mockHandler); $guzzleClient = new GuzzleHttp\Client(['handler' => $handlerStack]); - $client = new HamroCDN('test-api-key', 'https://hamrocdn.com/api', $guzzleClient); + $client = new HamroCDN('test-api-key', 'https://hamrocdn.test/api', $guzzleClient); $filePath = __DIR__.'/test.png'; @@ -148,14 +148,14 @@ }); it('throws network error when Guzzle cannot connect to server. (GET)', function () { - $client = new HamroCDN('test-api-key', 'https://hamrocdn.com/invalid-api'); + $client = new HamroCDN('test-api-key', 'https://hamrocdn.test/invalid-api'); $this->expectException(HamroCDNException::class); $client->index(); }); it('throws network error when Guzzle cannot connect to server. (POST)', function () { - $client = new HamroCDN('test-api-key', 'https://hamrocdn.com/invalid-api'); + $client = new HamroCDN('test-api-key', 'https://hamrocdn.test/invalid-api'); $filePath = __DIR__.'/test.png'; From 142a6ba09b402c698f756c8b39f2227ee220b77a Mon Sep 17 00:00:00 2001 From: achyutkneupane Date: Thu, 30 Oct 2025 15:56:02 +0545 Subject: [PATCH 2/6] refactor: updates X-Upload-Medium header value in HamroCDN client --- src/HamroCDN.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/HamroCDN.php b/src/HamroCDN.php index 7dd404c..40a66d1 100644 --- a/src/HamroCDN.php +++ b/src/HamroCDN.php @@ -43,7 +43,7 @@ public function __construct(?string $apiKey = null, ?string $baseUrl = null, ?Cl $this->client = new Client( array_merge($currentConfig, [ 'headers' => array_merge($currentHeaders, [ - 'X-Upload-Medium' => 'php_sdk', + 'X-Upload-Medium' => 'sdk_php', ]), ]) ); From 691f512c557813bdb691f732d88728e5f9806f51 Mon Sep 17 00:00:00 2001 From: achyutkneupane Date: Thu, 30 Oct 2025 15:59:09 +0545 Subject: [PATCH 3/6] refactor: updates X-Upload-Medium header value to use constant --- src/HamroCDN.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/HamroCDN.php b/src/HamroCDN.php index 40a66d1..6c8ff68 100644 --- a/src/HamroCDN.php +++ b/src/HamroCDN.php @@ -23,6 +23,8 @@ final class HamroCDN implements HamroCDNContract */ use HasConfigValues, Requestable; + private const HEADER_UPLOAD_MEDIUM = 'php_sdk'; + public function __construct(?string $apiKey = null, ?string $baseUrl = null, ?Client $client = null) { [$this->apiKey, $this->baseUrl] = $this->resolveConfig($apiKey, $baseUrl); @@ -43,7 +45,7 @@ public function __construct(?string $apiKey = null, ?string $baseUrl = null, ?Cl $this->client = new Client( array_merge($currentConfig, [ 'headers' => array_merge($currentHeaders, [ - 'X-Upload-Medium' => 'sdk_php', + 'X-Upload-Medium' => self::HEADER_UPLOAD_MEDIUM, ]), ]) ); From 22590d94eea3d8a4f54c24a2163bb410b4fcd23c Mon Sep 17 00:00:00 2001 From: achyutkneupane Date: Thu, 30 Oct 2025 16:03:53 +0545 Subject: [PATCH 4/6] refactor: updates client configuration to merge headers correctly --- src/HamroCDN.php | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/src/HamroCDN.php b/src/HamroCDN.php index 6c8ff68..9442a04 100644 --- a/src/HamroCDN.php +++ b/src/HamroCDN.php @@ -29,18 +29,20 @@ public function __construct(?string $apiKey = null, ?string $baseUrl = null, ?Cl { [$this->apiKey, $this->baseUrl] = $this->resolveConfig($apiKey, $baseUrl); - $this->client = $client ?? new Client([ + $clientConfig = $client?->getConfig() ?? []; + $currentHeaders = array_key_exists( + 'headers', + $clientConfig ?? []) + ? $clientConfig['headers'] + : [ + 'X-API-KEY' => $this->apiKey, + 'Accept' => 'application/json', + ]; + $currentConfig = $clientConfig ?? [ 'base_uri' => "{$this->baseUrl}/", 'timeout' => 15, 'verify' => true, - 'headers' => [ - 'X-API-KEY' => $this->apiKey, - 'Accept' => 'application/json', - ], - ]); - - $currentConfig = $this->client->getConfig(); - $currentHeaders = $currentConfig['headers'] ?? []; + ]; $this->client = new Client( array_merge($currentConfig, [ From 0cdbb84f61981f7c04ff02c2bdc4e5b47431f7a6 Mon Sep 17 00:00:00 2001 From: achyutkneupane Date: Thu, 30 Oct 2025 16:11:16 +0545 Subject: [PATCH 5/6] refactor: improves client configuration handling for headers Enhances the way client configuration merges headers, ensuring the X-Upload-Medium header is correctly included. --- src/HamroCDN.php | 33 +++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/src/HamroCDN.php b/src/HamroCDN.php index 9442a04..5d24a66 100644 --- a/src/HamroCDN.php +++ b/src/HamroCDN.php @@ -29,28 +29,41 @@ public function __construct(?string $apiKey = null, ?string $baseUrl = null, ?Cl { [$this->apiKey, $this->baseUrl] = $this->resolveConfig($apiKey, $baseUrl); - $clientConfig = $client?->getConfig() ?? []; + /** @var array|null $paramConfig */ + $paramConfig = $client?->getConfig(); + + /** @var array $currentHeaders */ $currentHeaders = array_key_exists( 'headers', - $clientConfig ?? []) - ? $clientConfig['headers'] + $paramConfig ?? [] + ) + ? $paramConfig['headers'] : [ 'X-API-KEY' => $this->apiKey, 'Accept' => 'application/json', ]; - $currentConfig = $clientConfig ?? [ + + /** @var array $currentConfig */ + $currentConfig = $paramConfig ?? [ 'base_uri' => "{$this->baseUrl}/", 'timeout' => 15, 'verify' => true, ]; - $this->client = new Client( - array_merge($currentConfig, [ - 'headers' => array_merge($currentHeaders, [ - 'X-Upload-Medium' => self::HEADER_UPLOAD_MEDIUM, - ]), - ]) + $clientHeaders = array_merge( + $currentHeaders, + [ + 'X-Upload-Medium' => self::HEADER_UPLOAD_MEDIUM, + ] ); + $clientConfig = array_merge( + $currentConfig, + [ + 'headers' => $clientHeaders, + ] + ); + + $this->client = new Client($clientConfig); } /** From 64214cb9a7e2410ab545798b3deecb9b7b02af54 Mon Sep 17 00:00:00 2001 From: achyutkneupane Date: Thu, 30 Oct 2025 16:14:08 +0545 Subject: [PATCH 6/6] refactor: updates HamroCDN client URLs to use the correct domain --- tests/UploadTest.php | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/tests/UploadTest.php b/tests/UploadTest.php index 7627116..035f6cf 100644 --- a/tests/UploadTest.php +++ b/tests/UploadTest.php @@ -33,11 +33,11 @@ 'nanoId' => 'abc123', 'user' => [ 'name' => 'John Doe', - 'email' => 'john@hamrocdn.test', + 'email' => 'john@hamrocdn.com', ], 'delete_at' => null, 'original' => [ - 'url' => 'https://hamrocdn.test/abc123.png', + 'url' => 'https://hamrocdn.com/abc123.png', 'size' => 2048, ], ]; @@ -48,14 +48,14 @@ expect($upload->getNanoId())->toBe('abc123'); expect($upload->getDeleteAt())->toBeNull(); - expect($upload->getOriginal()->getUrl())->toBe('https://hamrocdn.test/abc123.png'); + expect($upload->getOriginal()->getUrl())->toBe('https://hamrocdn.com/abc123.png'); expect($upload->getOriginal()->getSize())->toBe(2048); expect($upload->getUser()?->getName())->toBe('John Doe'); - expect($upload->getUser()?->getEmail())->toBe('john@hamrocdn.test'); + expect($upload->getUser()?->getEmail())->toBe('john@hamrocdn.com'); }); it('returns an array of HamroCDN objects from index', function () { - $client = new HamroCDN('test-api-key', 'https://hamrocdn.test/api'); + $client = new HamroCDN('test-api-key', 'https://hamrocdn.com/api'); $uploads = $client->index(); @@ -75,7 +75,7 @@ }); it('uploads a file and returns a HamroCDN object', function () { - $client = new HamroCDN('test-api-key', 'https://hamrocdn.test/api'); + $client = new HamroCDN('test-api-key', 'https://hamrocdn.com/api'); $filePath = __DIR__.'/test.png'; $upload = $client->upload($filePath); @@ -91,7 +91,7 @@ }); it('uploads a file by URL and returns a HamroCDN object', function () { - $client = new HamroCDN('test-api-key', 'https://hamrocdn.test/api'); + $client = new HamroCDN('test-api-key', 'https://hamrocdn.com/api'); $fileUrl = 'https://placehold.co/1000x1000/000000/FFFFFF?text=HamroCDN'; @@ -111,7 +111,7 @@ }); it('throws exception when uploading a non-existing file', function () { - $client = new HamroCDN('test-api-key', 'https://hamrocdn.test/api'); + $client = new HamroCDN('test-api-key', 'https://hamrocdn.com/api'); $filePath = __DIR__.'/non-existing-file.png'; @@ -126,7 +126,7 @@ $handlerStack = GuzzleHttp\HandlerStack::create($mockHandler); $guzzleClient = new GuzzleHttp\Client(['handler' => $handlerStack]); - $client = new HamroCDN('test-api-key', 'https://hamrocdn.test/api', $guzzleClient); + $client = new HamroCDN('test-api-key', 'https://hamrocdn.com/api', $guzzleClient); $this->expectException(HamroCDNException::class); $client->index(); @@ -139,7 +139,7 @@ $handlerStack = GuzzleHttp\HandlerStack::create($mockHandler); $guzzleClient = new GuzzleHttp\Client(['handler' => $handlerStack]); - $client = new HamroCDN('test-api-key', 'https://hamrocdn.test/api', $guzzleClient); + $client = new HamroCDN('test-api-key', 'https://hamrocdn.com/api', $guzzleClient); $filePath = __DIR__.'/test.png'; @@ -148,14 +148,14 @@ }); it('throws network error when Guzzle cannot connect to server. (GET)', function () { - $client = new HamroCDN('test-api-key', 'https://hamrocdn.test/invalid-api'); + $client = new HamroCDN('test-api-key', 'https://hamrocdn.com/invalid-api'); $this->expectException(HamroCDNException::class); $client->index(); }); it('throws network error when Guzzle cannot connect to server. (POST)', function () { - $client = new HamroCDN('test-api-key', 'https://hamrocdn.test/invalid-api'); + $client = new HamroCDN('test-api-key', 'https://hamrocdn.com/invalid-api'); $filePath = __DIR__.'/test.png';