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

De 1260 api mailing lists endpoint #903

Merged
merged 5 commits into from
Apr 27, 2024
Merged
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
56 changes: 56 additions & 0 deletions src/Api/MailingList.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Mailgun\Api\MailingList\Member;
use Mailgun\Assert;
use Mailgun\Model\EmailValidation\ValidateResponse;
use Mailgun\Model\MailingList\BulkResponse;
use Mailgun\Model\MailingList\CreateResponse;
use Mailgun\Model\MailingList\DeleteResponse;
use Mailgun\Model\MailingList\PagesResponse;
Expand Down Expand Up @@ -207,4 +208,59 @@ public function cancelValidation(string $address, array $requestHeaders = [])

return $this->hydrateResponse($response, ValidationCancelResponse::class);
}

/**
* Bulk upload members to a mailing list (JSON)
* @param string $mailList
* @param array $members
* @param bool $isUpsert
* @param array $requestHeaders
* @return BulkResponse
* @throws ClientExceptionInterface
*/
public function bulkUploadJson(string $mailList, array $members, bool $isUpsert = false, array $requestHeaders = [])
{
Assert::stringNotEmpty($mailList);

$query = [
'members' => json_encode($members),
'upsert' => $isUpsert,
];

$response = $this->httpPost(
sprintf('/v3/lists/%s/members.json?%s', $mailList, http_build_query($query)),
[],
$requestHeaders
);

return $this->hydrateResponse($response, BulkResponse::class);
}

/**
* Bulk upload members to a mailing list (CSV)
* //TODO
* @param string $mailList
* @param array $members
* @param bool $isUpsert
* @param array $requestHeaders
* @return BulkResponse
* @throws ClientExceptionInterface
*/
public function bulkUploadCsv(string $mailList, array $members, bool $isUpsert = false, array $requestHeaders = [])
{
Assert::stringNotEmpty($mailList);

$payload = [
'members' => implode(",", $members),
'upsert' => $isUpsert ? 'true' : 'false'
];

$response = $this->httpPost(
sprintf('/v3/lists/%s/members.csv', $mailList),
$payload,
$requestHeaders
);

return $this->hydrateResponse($response, BulkResponse::class);
}
}
65 changes: 65 additions & 0 deletions src/Model/MailingList/BulkResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

declare(strict_types=1);

/*
* Copyright (C) 2013 Mailgun
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/

namespace Mailgun\Model\MailingList;

use Mailgun\Model\ApiResponse;

final class BulkResponse implements ApiResponse
{
/**
* @var array|null
*/
private $list;

/**
* @var string|null
*/
private $message;
/**
* @var string|null
*/
private $taskId;

public static function create(array $data): self
{
$model = new self();
$model->list = $data['list'] ?? null;
$model->message = $data['message'] ?? null;
$model->taskId = $data['task-id'] ?? null;

return $model;
}

/**
* @return array|null
*/
public function getList(): ?array
{
return $this->list;
}

/**
* @return string|null
*/
public function getMessage(): ?string
{
return $this->message;
}

/**
* @return string|null
*/
public function getTaskId(): ?string
{
return $this->taskId;
}
}
Loading