Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added ability to pass raw data to post, get, put and delete so we can encode data as we want #315

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 17 additions & 12 deletions src/Mailjet/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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'])) {
/**
Expand All @@ -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();
Expand All @@ -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();
Expand Down Expand Up @@ -306,20 +306,25 @@ 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' => '',
'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']);

$contentType = 'application/json';
if ('csvdata/text:plain' === $action) {
$contentType = 'text/plain';
} elseif ('csverror/text:csv' === $action) {
Expand All @@ -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
);
Expand Down
24 changes: 12 additions & 12 deletions src/Mailjet/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class Request
private array $filters;

/**
* @var array
* @var array|string|null
*/
private $body;

Expand All @@ -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,
$body,
array|string|null $body,
string $type,
array $requestOptions = []
) {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -179,9 +179,9 @@ public function getUrl(): string
/**
* Request body getter.
*
* @return array request body
* @return array|string|null request body
*/
public function getBody(): array
public function getBody(): array|string|null
{
return $this->body;
}
Expand Down
22 changes: 21 additions & 1 deletion test/Mailjet/RequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);

Expand All @@ -29,4 +29,24 @@ 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());
}
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());
}
}