Skip to content
Closed
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
63 changes: 62 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@
* [Delete](#webhook-delete)
* [Campaign language API](#campaign-language-read)
* [Timezone API](#timezone-read)
* [Batch API](#batch-send)
* [Batch API](#batch-send)
* [Optional PSR transport (no BC break)](#optional-psr-transport-no-bc-break)
* [E-commerce (Products / Orders / Customers)](#e-commerce)
* [Testing](#testing)
* [License](#license)

Expand Down Expand Up @@ -766,6 +768,65 @@ $response = $mailerLite->timezones->get();
## Batch API
More information about request parameters on https://developers.mailerlite.com/docs/batching.html

<a name="e-commerce"></a>
## E-commerce (Products / Orders / Customers)

E-commerce endpoints follow:
```php
/api/ecommerce/shops/{shopId}/...
```
`shopId` is provided per call (there is no separate `/shops` API).

**Products**
```php
$res = $mailerLite->ecommerceProducts->create('YOUR_SHOP_ID', [
'title' => 'Red T-shirt',
'price' => 19.99,
'currency' => 'USD',
]);

$res = $mailerLite->ecommerceProducts->find('YOUR_SHOP_ID', 'PRODUCT_ID');

$res = $mailerLite->ecommerceProducts->update('YOUR_SHOP_ID', 'PRODUCT_ID', [
'title' => 'New title',
]);
```
**Orders**
```php
$res = $mailerLite->ecommerceOrders->create('YOUR_SHOP_ID', [
'order_id' => 'ORD-1001',
]);

$res = $mailerLite->ecommerceOrders->find('YOUR_SHOP_ID', 'ORD-1001');

$res = $mailerLite->ecommerceOrders->update('YOUR_SHOP_ID', 'ORD-1001', [
'status' => 'paid',
]);
```
**Customers**
```php
$res = $mailerLite->ecommerceCustomers->get('YOUR_SHOP_ID', ['limit' => 50]);
$res = $mailerLite->ecommerceCustomers->create('YOUR_SHOP_ID', ['email' => 'user@example.com']);
$res = $mailerLite->ecommerceCustomers->find('YOUR_SHOP_ID', 'CUSTOMER_ID');
```
## Optional PSR transport (no BC break)
```php
use MailerLite\MailerLite;
use MailerLite\Http\Adapters\Psr17FactoryAggregate;
use Nyholm\Psr7\Factory\Psr17Factory;
use GuzzleHttp\Client as GuzzleHttpClient;
$ml = new MailerLite(['api_key' => 'YOUR_API_KEY']);

$psr17 = new Psr17Factory();
$factories = new Psr17FactoryAggregate($psr17, $psr17);

$httpClient = new GuzzleHttpClient();

$ml->enablePsrTransport($httpClient, $factories, 'YOUR_API_KEY', 'https://connect.mailerlite.com');

$ml->subscribers->create(['email' => 'subscriber@example.com']);
```

<a name="batch-send"></a>
### Send

Expand Down
12 changes: 10 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,15 @@
"php-http/discovery": "^1.9",
"php-http/httplug": "^2.1",
"psr/http-client-implementation": "^1.0",
"psr/http-message": "^1.0 || ^2.0"
"psr/http-message": "^1.0 || ^2.0",
"psr/http-client": "^1.0",
"psr/http-factory": "^1.0"
},
"suggest": {
"guzzlehttp/guzzle": "^7.8 || ^8.0",
"nyholm/psr7": "^1.8",
"symfony/http-client": "^7.0"
},
"require-dev": {
"phpunit/phpunit": "^7.5.15 || ^8.4 || ^9.0",
"php-http/mock-client": "^1.0",
Expand All @@ -36,7 +43,8 @@
"http-interop/http-factory-guzzle": "^1.0",
"php-http/guzzle7-adapter": "^0.1",
"friendsofphp/php-cs-fixer": "^2.18",
"phpstan/phpstan": "^1.10"
"phpstan/phpstan": "^1.10",
"nyholm/psr7": "^1.8"
},
"autoload": {
"psr-4": {
Expand Down
124 changes: 124 additions & 0 deletions src/Common/HttpLayerPsr.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
<?php

namespace MailerLite\Common;

use MailerLite\Http\ClientInterface;
use MailerLite\Http\RequestFactoryInterface;
use MailerLite\Http\HttpErrorMapper;
use Psr\Http\Message\ResponseInterface;

final class HttpLayerPsr
{
/** @var ClientInterface */
private $client;

/** @var RequestFactoryInterface */
private $requestFactory;

/** @var string */
private $apiKey;

/** @var string */
private $baseUrl;

/** @var array<string,string|string[]> */
private $defaultHeaders;

/**
* @param array<string,string|string[]> $defaultHeaders
*/
public function __construct(
ClientInterface $client,
RequestFactoryInterface $requestFactory,
string $apiKey,
string $baseUrl = 'https://connect.mailerlite.com',
array $defaultHeaders = [
'Content-Type' => 'application/json',
'Accept' => 'application/json',
]
) {
$this->client = $client;
$this->requestFactory = $requestFactory;
$this->apiKey = $apiKey;
$this->baseUrl = $baseUrl;
$this->defaultHeaders = $defaultHeaders;
}

/**
* @param array<string, array<mixed>|bool|float|int|string|null> $query
*/
public function get(string $path, array $query = []): ResponseInterface
{
$uri = $this->buildUri($path, $query);
$req = $this->requestFactory->create('GET', $uri, $this->headers());
$res = $this->client->sendRequest($req);
HttpErrorMapper::throwIfError($res);
return $res;
}

/**
* @param array<mixed> $payload
*/
public function post(string $path, array $payload = []): ResponseInterface
{
$uri = $this->buildUri($path);
$body = $this->encodeJsonBody($payload);
$req = $this->requestFactory->create('POST', $uri, $this->headers(), $body);
$res = $this->client->sendRequest($req);
HttpErrorMapper::throwIfError($res);
return $res;
}

/**
* @param array<mixed> $payload
*/
public function put(string $path, array $payload = []): ResponseInterface
{
$uri = $this->buildUri($path);
$body = $this->encodeJsonBody($payload);
$req = $this->requestFactory->create('PUT', $uri, $this->headers(), $body);
$res = $this->client->sendRequest($req);
HttpErrorMapper::throwIfError($res);
return $res;
}

public function delete(string $path): ResponseInterface
{
$uri = $this->buildUri($path);
$req = $this->requestFactory->create('DELETE', $uri, $this->headers());
$res = $this->client->sendRequest($req);
HttpErrorMapper::throwIfError($res);
return $res;
}

/**
* @param array<string, array<mixed>|bool|float|int|string|null> $query
*/
private function buildUri(string $path, array $query = []): string
{
$uri = rtrim($this->baseUrl, '/') . '/' . ltrim($path, '/');
if ($query !== []) {
$uri .= '?' . http_build_query($query, '', '&', PHP_QUERY_RFC3986);
}
return $uri;
}

/**
* @param array<mixed> $payload
*/
private function encodeJsonBody(array $payload): ?string
{
if ($payload === []) {
return null;
}
return json_encode($payload, JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR);
}

/**
* @return array<string,string|string[]>
*/
private function headers(): array
{
return ['Authorization' => 'Bearer ' . $this->apiKey] + $this->defaultHeaders;
}
}
154 changes: 154 additions & 0 deletions src/Common/HttpLayerPsrBridge.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
<?php

namespace MailerLite\Common;

use MailerLite\Exceptions\MailerLiteException;
use Psr\Http\Message\ResponseInterface;

final class HttpLayerPsrBridge extends HttpLayer
{
/** @var HttpLayerPsr */
private $psrLayer;

public function __construct(HttpLayerPsr $psrLayer)
{
$this->psrLayer = $psrLayer;
}

/** @return array{status_code:int,headers:array<string,string[]>,body:mixed,response:ResponseInterface} */
public function get(string $uri, array $body = []): array
{
/** @var array<string, array<mixed>|bool|float|int|string|null> $query */
$query = $this->sanitizeQuery($body);
return $this->executeAndConvert(fn() => $this->psrLayer->get($uri, $query));
}

/** @return array{status_code:int,headers:array<string,string[]>,body:mixed,response:ResponseInterface} */
public function post(string $uri, array $body = []): array
{
return $this->executeAndConvert(fn() => $this->psrLayer->post($uri, $body));
}

/** @return array{status_code:int,headers:array<string,string[]>,body:mixed,response:ResponseInterface} */
public function put(string $uri, array $body): array
{
return $this->executeAndConvert(fn() => $this->psrLayer->put($uri, $body));
}

/** @return array{status_code:int,headers:array<string,string[]>,body:mixed,response:ResponseInterface} */
public function delete(string $uri, array $body = []): array
Comment thread
ishifoev marked this conversation as resolved.
{
// Note: $body parameter is ignored for DELETE requests as per HTTP semantics
return $this->executeAndConvert(fn() => $this->psrLayer->delete($uri));
}

/** @return array{status_code:int,headers:array<string,string[]>,body:mixed,response:ResponseInterface} */
public function request(string $method, string $uri, string $body = ''): array
{
$method = strtoupper($method);
$decodedBody = $this->decodeJsonBody($body);

switch ($method) {
case 'GET':
return $this->get($uri, []);
case 'POST':
return $this->post($uri, $decodedBody);
case 'PUT':
return $this->put($uri, $decodedBody);
case 'DELETE':
return $this->delete($uri, []);
default:
throw new MailerLiteException("Unsupported HTTP method: {$method}");
}
}

/**
* Execute PSR layer method and convert response to compatibility format
* @param callable(): ResponseInterface $psrMethod
* @return array{status_code:int,headers:array<string,string[]>,body:mixed,response:ResponseInterface}
*/
private function executeAndConvert(callable $psrMethod): array
{
$response = $psrMethod();
return $this->compatResponse($response);
}

/**
* @return array<mixed>
*/
private function decodeJsonBody(string $body): array
{
if ($body === '') {
return [];
}

$decoded = json_decode($body, true);
return $decoded !== null ? (array) $decoded : [];
}

/**
* @param array<string, mixed> $in
* @return array<string, array<mixed>|bool|float|int|string|null>
*/
private function sanitizeQuery(array $in): array
{
/** @var array<string, array<mixed>|bool|float|int|string|null> $out */
$out = [];
foreach ($in as $k => $v) {
if (is_array($v)) {
$out[$k] = array_map(
/** @return array<mixed>|bool|float|int|string|null */
static function ($x) {
if (is_bool($x) || is_int($x) || is_float($x) || is_string($x) || $x === null) {
return $x;
}
if (is_object($x) && method_exists($x, '__toString')) {
return (string) $x;
}
$json = json_encode($x);
return $json !== false ? $json : get_debug_type($x);
},
$v
);
} elseif (is_bool($v) || is_int($v) || is_float($v) || is_string($v) || $v === null) {
$out[$k] = $v;
} else {
if (is_object($v) && method_exists($v, '__toString')) {
$out[$k] = (string) $v;
} else {
$json = json_encode($v);
$out[$k] = $json !== false ? $json : get_debug_type($v);
}
}
}
return $out;
}

/**
* @return array{status_code:int,headers:array<string,string[]>,body:mixed,response:ResponseInterface}
*/
private function compatResponse(ResponseInterface $response): array
{
$headers = $response->getHeaders();
$statusCode = $response->getStatusCode();

$contentTypes = $response->getHeader('Content-Type');
$contentType = $response->hasHeader('Content-Type') ? (string) reset($contentTypes) : null;

switch (true) {
case $contentType !== null && stripos($contentType, 'application/json') === 0:
$bodyStr = (string) $response->getBody();
$body = $bodyStr !== '' ? json_decode($bodyStr, true) : null;
break;
default:
$body = (string) $response->getBody();
}

return [
'status_code' => $statusCode,
'headers' => $headers,
'body' => $body,
'response' => $response,
];
}
}
Loading
Loading