From 0e768856a9a9b12c9caf11be41d2550a3c7fab39 Mon Sep 17 00:00:00 2001 From: dante di domenico Date: Tue, 29 Aug 2023 15:34:09 +0200 Subject: [PATCH 1/6] feat: options for get,patch,post,delete --- src/Controller/ApiProxyTrait.php | 82 +++++++++++++++++++++----------- 1 file changed, 53 insertions(+), 29 deletions(-) diff --git a/src/Controller/ApiProxyTrait.php b/src/Controller/ApiProxyTrait.php index 5486cbd..ff764b8 100644 --- a/src/Controller/ApiProxyTrait.php +++ b/src/Controller/ApiProxyTrait.php @@ -55,7 +55,7 @@ trait ApiProxyTrait protected $response; /** - * BEdita4 API client + * BEdita API client * * @var \BEdita\SDK\BEditaClient */ @@ -101,63 +101,87 @@ protected function setBaseUrl($path): void } /** - * Proxy for GET requests to BEdita4 API + * Proxy for GET requests to BEdita API * * @param string $path The path for API request + * @param array $options The request options * @return void */ - public function get($path = ''): void + public function get($path = '', array $options = []): void { - $this->apiRequest([ - 'method' => 'get', - 'path' => $path, - 'query' => $this->request->getQueryParams(), - ]); + $this->apiRequest( + array_merge( + $options, + [ + 'method' => 'get', + 'path' => $path, + 'query' => $this->request->getQueryParams(), + ] + ) + ); } /** - * Proxy for POST requests to BEdita4 API + * Proxy for POST requests to BEdita API * * @param string $path The path for API request + * @param array $options The request options * @return void */ - public function post($path = ''): void + public function post($path = '', array $options = []): void { - $this->apiRequest([ - 'method' => 'post', - 'path' => $path, - 'body' => $this->request->getData(), - ]); + $this->apiRequest( + array_merge( + $options, + [ + 'method' => 'post', + 'path' => $path, + 'body' => $this->request->getData(), + ] + ) + ); } /** - * Proxy for PATCH requests to BEdita4 API + * Proxy for PATCH requests to BEdita API * * @param string $path The path for API request + * @param array $options The request options * @return void */ - public function patch($path = ''): void + public function patch($path = '', array $options = []): void { - $this->apiRequest([ - 'method' => 'patch', - 'path' => $path, - 'body' => $this->request->getData(), - ]); + $this->apiRequest( + array_merge( + $options, + [ + 'method' => 'patch', + 'path' => $path, + 'body' => $this->request->getData(), + ] + ) + ); } /** - * Proxy for DELETE requests to BEdita4 API + * Proxy for DELETE requests to BEdita API * * @param string $path The path for API request + * @param array $options The request options * @return void */ - public function delete($path = ''): void + public function delete($path = '', array $options = []): void { - $this->apiRequest([ - 'method' => 'delete', - 'path' => $path, - 'body' => $this->request->getData(), - ]); + $this->apiRequest( + array_merge( + $options, + [ + 'method' => 'delete', + 'path' => $path, + 'body' => $this->request->getData(), + ] + ) + ); } /** From 1fb891367f7ad74c8ce6373b2f5bf2d13708e1ec Mon Sep 17 00:00:00 2001 From: dante di domenico Date: Tue, 29 Aug 2023 15:42:48 +0200 Subject: [PATCH 2/6] fix. options headers --- src/Controller/ApiProxyTrait.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/Controller/ApiProxyTrait.php b/src/Controller/ApiProxyTrait.php index ff764b8..0f173bf 100644 --- a/src/Controller/ApiProxyTrait.php +++ b/src/Controller/ApiProxyTrait.php @@ -204,9 +204,12 @@ protected function apiRequest(array $options): void 'path' => '', 'query' => null, 'body' => null, - 'headers' => null, ]; + if (empty($options['headers'])) { + $options['headers'] = null; + } + if (empty($options['body'])) { $options['body'] = null; } From 72ce077dd84576dcad4ce76619e9490be670dd59 Mon Sep 17 00:00:00 2001 From: dante di domenico Date: Tue, 29 Aug 2023 16:07:21 +0200 Subject: [PATCH 3/6] refactor: header content-type overrideable --- src/Controller/ApiProxyTrait.php | 101 ++++++++++++++----------------- 1 file changed, 45 insertions(+), 56 deletions(-) diff --git a/src/Controller/ApiProxyTrait.php b/src/Controller/ApiProxyTrait.php index 0f173bf..1c0ad28 100644 --- a/src/Controller/ApiProxyTrait.php +++ b/src/Controller/ApiProxyTrait.php @@ -40,6 +40,13 @@ trait ApiProxyTrait { use ViewVarsTrait; + /** + * Default allowed headers on proxied requests. + * + * @var array + */ + protected $allowedHeaders = ['Content-Type']; + /** * An instance of a \Cake\Http\ServerRequest object that contains information about the current request. * @@ -55,7 +62,7 @@ trait ApiProxyTrait protected $response; /** - * BEdita API client + * BEdita4 API client * * @var \BEdita\SDK\BEditaClient */ @@ -101,87 +108,63 @@ protected function setBaseUrl($path): void } /** - * Proxy for GET requests to BEdita API + * Proxy for GET requests to BEdita4 API * * @param string $path The path for API request - * @param array $options The request options * @return void */ - public function get($path = '', array $options = []): void + public function get($path = ''): void { - $this->apiRequest( - array_merge( - $options, - [ - 'method' => 'get', - 'path' => $path, - 'query' => $this->request->getQueryParams(), - ] - ) - ); + $this->apiRequest([ + 'method' => 'get', + 'path' => $path, + 'query' => $this->request->getQueryParams(), + ]); } /** - * Proxy for POST requests to BEdita API + * Proxy for POST requests to BEdita4 API * * @param string $path The path for API request - * @param array $options The request options * @return void */ - public function post($path = '', array $options = []): void + public function post($path = ''): void { - $this->apiRequest( - array_merge( - $options, - [ - 'method' => 'post', - 'path' => $path, - 'body' => $this->request->getData(), - ] - ) - ); + $this->apiRequest([ + 'method' => 'post', + 'path' => $path, + 'body' => $this->request->getData(), + ]); } /** - * Proxy for PATCH requests to BEdita API + * Proxy for PATCH requests to BEdita4 API * * @param string $path The path for API request - * @param array $options The request options * @return void */ - public function patch($path = '', array $options = []): void + public function patch($path = ''): void { - $this->apiRequest( - array_merge( - $options, - [ - 'method' => 'patch', - 'path' => $path, - 'body' => $this->request->getData(), - ] - ) - ); + $this->apiRequest([ + 'method' => 'patch', + 'path' => $path, + 'body' => $this->request->getData(), + ]); } /** - * Proxy for DELETE requests to BEdita API + * Proxy for DELETE requests to BEdita4 API * * @param string $path The path for API request - * @param array $options The request options * @return void */ - public function delete($path = '', array $options = []): void + public function delete($path = ''): void { - $this->apiRequest( - array_merge( - $options, - [ - 'method' => 'delete', - 'path' => $path, - 'body' => $this->request->getData(), - ] - ) - ); + $this->apiRequest([ + 'method' => 'delete', + 'path' => $path, + 'body' => $this->request->getData(), + ]); } /** @@ -204,10 +187,16 @@ protected function apiRequest(array $options): void 'path' => '', 'query' => null, 'body' => null, + 'headers' => null, ]; - - if (empty($options['headers'])) { - $options['headers'] = null; + $headers = array_filter( + $this->request->getHeaders(), + function ($item) { + return in_array($item, $this->allowedHeaders); + } + ); + if (!empty($headers)) { + $options['headers'] = $headers; } if (empty($options['body'])) { From 4f333196fa0f2a508d886503f47a5b0a46609f63 Mon Sep 17 00:00:00 2001 From: dante di domenico Date: Tue, 29 Aug 2023 17:18:25 +0200 Subject: [PATCH 4/6] fix: test --- src/Controller/ApiProxyTrait.php | 7 ++++--- tests/TestCase/Controller/ApiProxyTraitTest.php | 13 +++++++++++++ 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/src/Controller/ApiProxyTrait.php b/src/Controller/ApiProxyTrait.php index 1c0ad28..409f314 100644 --- a/src/Controller/ApiProxyTrait.php +++ b/src/Controller/ApiProxyTrait.php @@ -191,9 +191,10 @@ protected function apiRequest(array $options): void ]; $headers = array_filter( $this->request->getHeaders(), - function ($item) { - return in_array($item, $this->allowedHeaders); - } + function ($key) { + return in_array($key, $this->allowedHeaders); + }, + ARRAY_FILTER_USE_KEY ); if (!empty($headers)) { $options['headers'] = $headers; diff --git a/tests/TestCase/Controller/ApiProxyTraitTest.php b/tests/TestCase/Controller/ApiProxyTraitTest.php index b1ec25e..64963db 100644 --- a/tests/TestCase/Controller/ApiProxyTraitTest.php +++ b/tests/TestCase/Controller/ApiProxyTraitTest.php @@ -122,6 +122,19 @@ public function testGet(): void } } + /** + * Test get() method + * + * @return void + * @covers ::get() + */ + public function testGetWithHeaders(): void + { + $this->_request['headers'] = ['Content-type' => 'text/plain']; + $this->get('/api/users/1'); + $this->assertResponseOk(); + } + /** * Test non found error proxied from API. * From 865e740e31f459616f890aaea60ba868d1d71d0d Mon Sep 17 00:00:00 2001 From: dante di domenico Date: Wed, 30 Aug 2023 08:40:02 +0200 Subject: [PATCH 5/6] refactor: ApiProxyTrait::apiRequest --- src/Controller/ApiProxyTrait.php | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/src/Controller/ApiProxyTrait.php b/src/Controller/ApiProxyTrait.php index 409f314..d93557b 100644 --- a/src/Controller/ApiProxyTrait.php +++ b/src/Controller/ApiProxyTrait.php @@ -189,15 +189,14 @@ protected function apiRequest(array $options): void 'body' => null, 'headers' => null, ]; - $headers = array_filter( - $this->request->getHeaders(), - function ($key) { - return in_array($key, $this->allowedHeaders); - }, - ARRAY_FILTER_USE_KEY - ); - if (!empty($headers)) { - $options['headers'] = $headers; + foreach ($this->allowedHeaders as $headerName) { + if (empty($this->request->getHeader($headerName))) { + continue; + } + if ($options['headers'] === null) { + $options['headers'] = []; + } + $options['headers'][$headerName] = $this->request->getHeader($headerName); } if (empty($options['body'])) { From 8e37aa652551dff9ddc45eac0e3cb49bf7b37fd2 Mon Sep 17 00:00:00 2001 From: dante di domenico Date: Wed, 30 Aug 2023 09:22:08 +0200 Subject: [PATCH 6/6] fix: headers --- src/Controller/ApiProxyTrait.php | 4 +--- tests/TestCase/Controller/ApiProxyTraitTest.php | 2 +- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/src/Controller/ApiProxyTrait.php b/src/Controller/ApiProxyTrait.php index d93557b..eeecb6f 100644 --- a/src/Controller/ApiProxyTrait.php +++ b/src/Controller/ApiProxyTrait.php @@ -193,9 +193,7 @@ protected function apiRequest(array $options): void if (empty($this->request->getHeader($headerName))) { continue; } - if ($options['headers'] === null) { - $options['headers'] = []; - } + $options['headers'] = $options['headers'] ?? []; $options['headers'][$headerName] = $this->request->getHeader($headerName); } diff --git a/tests/TestCase/Controller/ApiProxyTraitTest.php b/tests/TestCase/Controller/ApiProxyTraitTest.php index 64963db..18d63e8 100644 --- a/tests/TestCase/Controller/ApiProxyTraitTest.php +++ b/tests/TestCase/Controller/ApiProxyTraitTest.php @@ -126,7 +126,7 @@ public function testGet(): void * Test get() method * * @return void - * @covers ::get() + * @covers ::apiRequest() */ public function testGetWithHeaders(): void {