Skip to content

Commit

Permalink
Fixed way of sending request with JSON body. loadMetrics works. Adjus…
Browse files Browse the repository at this point in the history
…ted tests. Fixed warning related to the php 8.4 and nullable types
  • Loading branch information
oleksandr-mykhailenko committed Dec 28, 2024
1 parent cc2e0ae commit d5c8ca3
Show file tree
Hide file tree
Showing 15 changed files with 63 additions and 42 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

The change log describes what is "Added", "Removed", "Changed" or "Fixed" between each release.

## 4.3.3
- Fixed way of sending request with JSON body. loadMetrics works. Adjusted tests. Fixed warning related to the php 8.4 and nullable types

## 4.3.2
- Added new API endpoint for getting metrics @see https://documentation.mailgun.com/docs/mailgun/api-reference/openapi-final/tag/Metrics/

Expand Down
28 changes: 16 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -202,20 +202,24 @@ use Mailgun\Mailgun;
$mgClient = Mailgun::create('xxx');
$domain = "xxx.mailgun.org";

$result = $mgClient->metrics()->loadMetrics([
'start' => 'Wed, 11 Sep 2024 18:29:02 +0300',
'end' => 'Wed, 25 Sep 2024 18:29:02 +0300',
'metrics' => [
"failed_count", "opened_count", "sent_count", "delivered_count"
$payload = [
"resolution" => "day",
"metrics" => [
"accepted_count",
"delivered_count",
"clicked_rate",
"opened_rate"
],
'resolution' => 'month',
'precision' => 'day',
'dimensions' => [
'time',
"include_aggregates" => true,
"start" => "Sun, 22 Dec 2024 18:29:02 +0000",
"dimensions" => [
"time"
],
'include_aggregates' => true,
'include_subaccounts' => true,
]);
"end" => "Wed, 25 Dec 2024 18:29:02 +0000",
"include_subaccounts" => true
];

$result = $mgClient->metrics()->loadMetrics($payload);

print_r($result->getItems());

Expand Down
8 changes: 4 additions & 4 deletions src/Api/Domain.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,10 @@ public function show(string $domain, array $requestHeaders = [])
*/
public function create(
string $domain,
string $smtpPass = null,
string $spamAction = null,
bool $wildcard = null,
bool $forceDkimAuthority = null,
?string $smtpPass = null,
?string $spamAction = null,
?bool $wildcard = null,
?bool $forceDkimAuthority = null,
?array $ips = null,
?string $pool_id = null,
string $webScheme = 'http',
Expand Down
3 changes: 1 addition & 2 deletions src/Api/DomainV4.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,7 @@ public function create(
string $dkimKeySize = '1024',
array $requestHeaders = [],
?string $dkimHostName = null,
?string $dkimSelector = null,

?string $dkimSelector = null
) {
Assert::stringNotEmpty($domain);

Expand Down
18 changes: 11 additions & 7 deletions src/Api/HttpApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,17 @@ abstract class HttpApi
*
* @var ClientInterface
*/
protected $httpClient;
protected ClientInterface $httpClient;

/**
* @var Hydrator|null
*/
protected $hydrator;
protected ?Hydrator $hydrator;

/**
* @var RequestBuilder
*/
protected $requestBuilder;
protected RequestBuilder $requestBuilder;

/**
* @param ClientInterface $httpClient
Expand Down Expand Up @@ -117,7 +117,7 @@ protected function handleErrors(ResponseInterface $response): void
* @param string $path Request path
* @param array $parameters GET parameters
* @param array $requestHeaders Request Headers
* @throws ClientExceptionInterface
* @throws ClientExceptionInterface|\JsonException
*/
protected function httpGet(string $path, array $parameters = [], array $requestHeaders = []): ResponseInterface
{
Expand All @@ -142,10 +142,14 @@ protected function httpGet(string $path, array $parameters = [], array $requestH
* @param string $path Request path
* @param array $parameters POST parameters
* @param array $requestHeaders Request headers
* @throws ClientExceptionInterface
* @throws ClientExceptionInterface|\JsonException
*/
protected function httpPost(string $path, array $parameters = [], array $requestHeaders = []): ResponseInterface
{
if (isset($requestHeaders['Content-Type']) && $requestHeaders['Content-Type'] === 'application/json') {
return $this->httpPostRaw($path, $parameters, $requestHeaders);
}

return $this->httpPostRaw($path, $this->createRequestBody($parameters), $requestHeaders);
}

Expand All @@ -155,7 +159,7 @@ protected function httpPost(string $path, array $parameters = [], array $request
* @param string $path Request path
* @param array|string $body Request body
* @param array $requestHeaders Request headers
* @throws ClientExceptionInterface
* @throws ClientExceptionInterface|\JsonException
*/
protected function httpPostRaw(string $path, $body, array $requestHeaders = []): ResponseInterface
{
Expand All @@ -176,7 +180,7 @@ protected function httpPostRaw(string $path, $body, array $requestHeaders = []):
* @param string $path Request path
* @param array $parameters PUT parameters
* @param array $requestHeaders Request headers
* @throws ClientExceptionInterface
* @throws ClientExceptionInterface|\JsonException
*/
protected function httpPut(string $path, array $parameters = [], array $requestHeaders = []): ResponseInterface
{
Expand Down
8 changes: 4 additions & 4 deletions src/Api/MailingList/Member.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class Member extends HttpApi
* @return IndexResponse
* @throws ClientExceptionInterface
*/
public function index(string $address, int $limit = 100, bool $subscribed = null, array $requestHeaders = [])
public function index(string $address, int $limit = 100, ?bool $subscribed = null, array $requestHeaders = [])
{
Assert::stringNotEmpty($address);
Assert::greaterThan($limit, 0);
Expand Down Expand Up @@ -92,10 +92,10 @@ public function show(string $list, string $address, array $requestHeaders = [])
public function create(
string $list,
string $address,
string $name = null,
?string $name = null,
array $vars = [],
bool $subscribed = true,
bool $upsert = false,
?bool $subscribed = true,
?bool $upsert = false,
array $requestHeaders = []
) {
Assert::stringNotEmpty($list);
Expand Down
2 changes: 2 additions & 0 deletions src/Api/Metrics.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ public function loadMetrics(array $payload = [], array $requestHeaders = []): Me
}
}

$requestHeaders['Content-Type'] = 'application/json';

$response = $this->httpPost('/v1/analytics/metrics', $payload, $requestHeaders);

return $this->hydrateResponse($response, MetricsResponse::class);
Expand Down
6 changes: 3 additions & 3 deletions src/Api/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,10 @@ public function create(string $expression, array $actions, string $description,
*/
public function update(
string $routeId,
string $expression = null,
?string $expression = null,
array $actions = [],
string $description = null,
int $priority = null,
?string $description = null,
?int $priority = null,
array $requestHeaders = []
) {
Assert::stringNotEmpty($routeId);
Expand Down
2 changes: 1 addition & 1 deletion src/Api/Suppression/Complaint.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public function show(string $domain, string $address, array $requestHeaders = []
* @return CreateResponse
* @throws ClientExceptionInterface
*/
public function create(string $domain, string $address, string $createdAt = null, array $requestHeaders = [])
public function create(string $domain, string $address, ?string $createdAt = null, array $requestHeaders = [])
{
Assert::stringNotEmpty($domain);
Assert::stringNotEmpty($address);
Expand Down
2 changes: 1 addition & 1 deletion src/Api/Suppression/Unsubscribe.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public function create(string $domain, string $address, array $params = [], arra
* @return DeleteResponse
* @throws ClientExceptionInterface
*/
public function delete(string $domain, string $address, string $tag = null, array $requestHeaders = [])
public function delete(string $domain, string $address, ?string $tag = null, array $requestHeaders = [])
{
Assert::stringNotEmpty($domain);
Assert::stringNotEmpty($address);
Expand Down
9 changes: 9 additions & 0 deletions src/HttpClient/RequestBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class RequestBuilder
* 'filename'=> string (optional)
* 'headers' => array (optinal) ['header-name' => 'header-value']
* )
* @throws \JsonException
*/
public function create(string $method, string $uri, array $headers = [], $body = null): RequestInterface
{
Expand All @@ -60,6 +61,14 @@ public function create(string $method, string $uri, array $headers = [], $body =
return $this->createRequest($method, $uri, $headers, $stream);
}

if (isset($headers['Content-Type']) && $headers['Content-Type'] === 'application/json') {
$jsonBody = json_encode($body, JSON_THROW_ON_ERROR);
$stream = $this->getStreamFactory()->createStream($jsonBody);
$headers['Content-Type'] = 'application/json';

return $this->createRequest($method, $uri, $headers, $stream);
}

$builder = $this->getMultipartStreamBuilder();
foreach ($body as $item) {
$name = $this->getItemValue($item, 'name');
Expand Down
4 changes: 2 additions & 2 deletions src/Mailgun.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ class Mailgun
*/
public function __construct(
HttpClientConfigurator $configurator,
Hydrator $hydrator = null,
RequestBuilder $requestBuilder = null
?Hydrator $hydrator = null,
?RequestBuilder $requestBuilder = null
) {
$this->requestBuilder = $requestBuilder ?: new RequestBuilder();
$this->hydrator = $hydrator ?: new ModelHydrator();
Expand Down
2 changes: 1 addition & 1 deletion src/Message/Exceptions/MissingRequiredParameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class MissingRequiredParameter extends \Exception implements Exception
* @param string|null $message
* @return self
*/
public static function create(string $parameter, string $message = null)
public static function create(string $parameter, ?string $message = null)
{
if (null === $message) {
$message = 'The parameters passed to the API were invalid. Please specify "%s".';
Expand Down
8 changes: 4 additions & 4 deletions src/Message/MessageBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ public function setHtmlBody(string $htmlBody): self
* @param string|null $attachmentName
* @return $this
*/
public function addAttachment(string $attachmentPath, string $attachmentName = null): self
public function addAttachment(string $attachmentPath, ?string $attachmentName = null): self
{
if (!isset($this->message['attachment'])) {
$this->message['attachment'] = [];
Expand All @@ -337,7 +337,7 @@ public function addAttachment(string $attachmentPath, string $attachmentName = n
* @param string|null $attachmentName
* @return $this
*/
public function addStringAttachment(string $attachmentContent, string $attachmentName = null): self
public function addStringAttachment(string $attachmentContent, ?string $attachmentName = null): self
{
if (!isset($this->message['attachment'])) {
$this->message['attachment'] = [];
Expand All @@ -356,7 +356,7 @@ public function addStringAttachment(string $attachmentContent, string $attachmen
* @param string|null $inlineImageName
* @return $this
*/
public function addInlineImage(string $inlineImagePath, string $inlineImageName = null): self
public function addInlineImage(string $inlineImagePath, ?string $inlineImageName = null): self
{
if (!isset($this->message['inline'])) {
$this->message['inline'] = [];
Expand Down Expand Up @@ -466,7 +466,7 @@ public function setClickTracking(bool $enabled, bool $htmlOnly = false): self
* @return $this
* @throws \Exception
*/
public function setDeliveryTime(string $timeDate, string $timeZone = null): self
public function setDeliveryTime(string $timeDate, ?string $timeZone = null): self
{
if (null !== $timeZone) {
$timeZoneObj = new DateTimeZone($timeZone);
Expand Down
2 changes: 1 addition & 1 deletion tests/HttpClient/RequestBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ function (array $data) use ($item0) {

$this->requestBuilder->setMultipartStreamBuilder($multipartStreamBuilder);
$result = $this->requestBuilder
->create('GET', 'http://foo.bar', ['Content-Type' => 'application/json'], [$item0]);
->create('GET', 'http://foo.bar', [], [$item0]);

$this->assertSame($request, $result);
}
Expand Down

0 comments on commit d5c8ca3

Please sign in to comment.