From dacac500787ed6d443ce2f395d9fc4e55c0339b6 Mon Sep 17 00:00:00 2001 From: Hugo Combe Date: Thu, 19 Dec 2024 16:13:12 +0100 Subject: [PATCH 1/3] Added ability to manually set Content-Type header in Request calls, so we can manually encode JSON with custom flags --- src/Mailjet/Client.php | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/Mailjet/Client.php b/src/Mailjet/Client.php index 8fec6d0..e9f0421 100644 --- a/src/Mailjet/Client.php +++ b/src/Mailjet/Client.php @@ -90,13 +90,13 @@ public function __construct(string $key, ?string $secret = null, bool $call = tr * @param array $options * @return Response */ - public function post(array $resource, array $args = [], array $options = []): Response + public function post(array $resource, array $args = [], array $options = [], string $contentType = 'application/json'): Response { if (!empty($options)) { $this->setOptions($options, $resource); } - $result = $this->_call('POST', $resource[0], $resource[1], $args); + $result = $this->_call('POST', $resource[0], $resource[1], $args, $contentType); if (!empty($this->changed)) { $this->setSettings(); @@ -113,13 +113,13 @@ public function post(array $resource, array $args = [], array $options = []): Re * @param array $options * @return Response */ - public function get(array $resource, array $args = [], array $options = []): Response + public function get(array $resource, array $args = [], array $options = [], string $contentType = 'application/json'): Response { if (!empty($options)) { $this->setOptions($options, $resource); } - $result = $this->_call('GET', $resource[0], $resource[1], $args); + $result = $this->_call('GET', $resource[0], $resource[1], $args, $contentType); if (isset($resource['normalizer']) && class_exists($resource['normalizer'])) { /** @@ -146,13 +146,13 @@ public function get(array $resource, array $args = [], array $options = []): Res * @param array $options * @return Response */ - public function put(array $resource, array $args = [], array $options = []): Response + public function put(array $resource, array $args = [], array $options = [], string $contentType = 'application/json'): Response { if (!empty($options)) { $this->setOptions($options, $resource); } - $result = $this->_call('PUT', $resource[0], $resource[1], $args); + $result = $this->_call('PUT', $resource[0], $resource[1], $args, $contentType); if (!empty($this->changed)) { $this->setSettings(); @@ -169,13 +169,13 @@ public function put(array $resource, array $args = [], array $options = []): Res * @param array $options * @return Response */ - public function delete(array $resource, array $args = [], array $options = []): Response + public function delete(array $resource, array $args = [], array $options = [], string $contentType = 'application/json'): Response { if (!empty($options)) { $this->setOptions($options, $resource); } - $result = $this->_call('DELETE', $resource[0], $resource[1], $args); + $result = $this->_call('DELETE', $resource[0], $resource[1], $args, $contentType); if (!empty($this->changed)) { $this->setSettings(); @@ -306,9 +306,10 @@ public function setAuthentication(string $key, ?string $secret, bool $call, arra * @param string $resource mailjet resource * @param string $action mailjet resource action * @param array $args Request arguments + * @param string $contentType Request Content-type * @return Response server response */ - private function _call(string $method, string $resource, string $action, array $args): Response + private function _call(string $method, string $resource, string $action, array $args, string $contentType = 'application/json'): Response { $args = array_merge([ 'id' => '', @@ -319,7 +320,6 @@ private function _call(string $method, string $resource, string $action, array $ $url = $this->buildURL($resource, $action, (string)$args['id'], $args['actionid']); - $contentType = 'application/json'; if ('csvdata/text:plain' === $action) { $contentType = 'text/plain'; } elseif ('csverror/text:csv' === $action) { From 6c3317987e215b03159f4f180d5cbdd594e23ec1 Mon Sep 17 00:00:00 2001 From: Hugo Combe Date: Thu, 19 Dec 2024 16:41:58 +0100 Subject: [PATCH 2/3] Differentiation between json and body in API calls --- src/Mailjet/Client.php | 9 +++++++-- src/Mailjet/Request.php | 10 +++++----- test/Mailjet/RequestTest.php | 12 +++++++++++- 3 files changed, 23 insertions(+), 8 deletions(-) diff --git a/src/Mailjet/Client.php b/src/Mailjet/Client.php index e9f0421..294145a 100644 --- a/src/Mailjet/Client.php +++ b/src/Mailjet/Client.php @@ -315,9 +315,14 @@ private function _call(string $method, string $resource, string $action, array $ 'id' => '', 'actionid' => '', 'filters' => [], - 'body' => 'GET' === $method ? null : '{}', + 'body' => null, + 'json' => null, ], array_change_key_case($args)); + if ('GET' !== $method && null === $args['body'] && null === $args['json']) { + $args['body'] = "{}"; + } + $url = $this->buildURL($resource, $action, (string)$args['id'], $args['actionid']); if ('csvdata/text:plain' === $action) { @@ -334,7 +339,7 @@ private function _call(string $method, string $resource, string $action, array $ $method, $url, $args['filters'], - $args['body'], + $args['body'] ?? $args['json'], $contentType, $this->requestOptions ); diff --git a/src/Mailjet/Request.php b/src/Mailjet/Request.php index 9be6665..33ee04a 100644 --- a/src/Mailjet/Request.php +++ b/src/Mailjet/Request.php @@ -42,7 +42,7 @@ class Request private $filters; /** - * @var array + * @var array|string */ private $body; @@ -82,7 +82,7 @@ public function __construct( string $method, string $url, array $filters, - $body, + array|string $body, string $type, array $requestOptions = [] ) { @@ -113,7 +113,7 @@ public function call($call): Response { $payload = [ 'query' => $this->filters, - ('application/json' === $this->type ? 'json' : 'body') => $this->body, + (is_array($this->body) ? 'json' : 'body') => $this->body, ]; $authArgsCount = \count($this->auth); @@ -179,9 +179,9 @@ public function getUrl(): string /** * Request body getter. * - * @return array request body + * @return array|string request body */ - public function getBody(): array + public function getBody(): array|string { return $this->body; } diff --git a/test/Mailjet/RequestTest.php b/test/Mailjet/RequestTest.php index 9ebdb56..88bb5a8 100644 --- a/test/Mailjet/RequestTest.php +++ b/test/Mailjet/RequestTest.php @@ -19,7 +19,7 @@ */ final class RequestTest extends TestCase { - public function testRequest() + public function testJsonRequest(): void { $request = new Request(['test', 'test2'], 'GET', 'test.com', ['fkey' => 'fvalue'], ['bkey' => 'bvalue'], 'test', ['rok' => 'rov']); @@ -29,4 +29,14 @@ public function testRequest() $this->assertEquals(['bkey' => 'bvalue'], $request->getBody()); $this->assertEquals(['test', 'test2'], $request->getAuth()); } + public function testStringRequest(): void + { + $request = new Request(['test', 'test2'], 'GET', 'test.com', ['fkey' => 'fvalue'], json_encode(['bkey' => '✉️'], JSON_UNESCAPED_UNICODE), 'test', ['rok' => 'rov']); + + $this->assertEquals(['fkey' => 'fvalue'], $request->getFilters()); + $this->assertEquals('GET', $request->getMethod()); + $this->assertEquals('test.com', $request->getUrl()); + $this->assertEquals('{"bkey":"✉️"}', $request->getBody()); + $this->assertEquals(['test', 'test2'], $request->getAuth()); + } } From 81be68fa93bce36488be0b9db9ab5f514804d8e7 Mon Sep 17 00:00:00 2001 From: Hugo Combe Date: Fri, 17 Jan 2025 09:26:32 +0100 Subject: [PATCH 3/3] Make body nullable in Request.php --- src/Mailjet/Request.php | 22 +++++++++++----------- test/Mailjet/RequestTest.php | 10 ++++++++++ 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/src/Mailjet/Request.php b/src/Mailjet/Request.php index d496f55..960d9bf 100644 --- a/src/Mailjet/Request.php +++ b/src/Mailjet/Request.php @@ -42,7 +42,7 @@ class Request private array $filters; /** - * @var array|string + * @var array|string|null */ private $body; @@ -69,20 +69,20 @@ class Request /** * Build a new Http request. * - * @param array $auth [apikey, apisecret] - * @param string $method http method - * @param string $url call url - * @param array $filters Mailjet resource filters - * @param mixed $body Mailjet resource body - * @param string $type Request Content-type - * @param array $requestOptions + * @param array $auth [apikey, apisecret] + * @param string $method http method + * @param string $url call url + * @param array $filters Mailjet resource filters + * @param array|string|null $body Mailjet resource body + * @param string $type Request Content-type + * @param array $requestOptions */ public function __construct( array $auth, string $method, string $url, array $filters, - array|string $body, + array|string|null $body, string $type, array $requestOptions = [] ) { @@ -179,9 +179,9 @@ public function getUrl(): string /** * Request body getter. * - * @return array|string request body + * @return array|string|null request body */ - public function getBody(): array|string + public function getBody(): array|string|null { return $this->body; } diff --git a/test/Mailjet/RequestTest.php b/test/Mailjet/RequestTest.php index 88bb5a8..164b3dd 100644 --- a/test/Mailjet/RequestTest.php +++ b/test/Mailjet/RequestTest.php @@ -39,4 +39,14 @@ public function testStringRequest(): void $this->assertEquals('{"bkey":"✉️"}', $request->getBody()); $this->assertEquals(['test', 'test2'], $request->getAuth()); } + public function testStringRequestEmptyBody(): void + { + $request = new Request(['test', 'test2'], 'GET', 'test.com', ['fkey' => 'fvalue'], null, 'test', ['rok' => 'rov']); + + $this->assertEquals(['fkey' => 'fvalue'], $request->getFilters()); + $this->assertEquals('GET', $request->getMethod()); + $this->assertEquals('test.com', $request->getUrl()); + $this->assertNotNull($request->getBody()); + $this->assertEquals(['test', 'test2'], $request->getAuth()); + } }