Skip to content

Commit d5aa759

Browse files
Merge pull request #930 from mailgun/DE-1253-api-domains-keys-endpoint
Domain Keys API Endpoints
2 parents ba0a5db + 756afee commit d5aa759

File tree

8 files changed

+499
-0
lines changed

8 files changed

+499
-0
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,3 +359,6 @@ If you find a bug, please submit the issue in Github directly.
359359

360360
As always, if you need additional assistance, drop us a note through your account at
361361
[https://app.mailgun.com/support](https://app.mailgun.com/support).
362+
363+
## Examples section
364+
[Examples section](doc/examples.md) contains examples of how to use the SDK.

doc/examples.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
## Domain Keys Example
2+
3+
```php
4+
<?php
5+
require 'vendor/autoload.php';
6+
7+
use Mailgun\Mailgun;
8+
9+
$mgClient = Mailgun::create('xxx');
10+
$domain = "xxx.mailgun.org";
11+
12+
try {
13+
$res = $mgClient->domainKeys()->listKeysForDomains();
14+
print_r($res);
15+
} catch (Throwable $t) {
16+
print_r($t->getMessage());
17+
print_r($t->getTraceAsString());
18+
}
19+
20+
try {
21+
$res = $mgClient->domainKeys()->deleteDomainKey($domain, 'xxx');
22+
} catch (Throwable $t) {
23+
print_r($t->getMessage());
24+
print_r($t->getTraceAsString());
25+
}
26+
27+
try {
28+
$res = $mgClient->domainKeys()->listDomainKeys($domain);
29+
print_r($res);
30+
} catch (Throwable $t) {
31+
print_r($t->getMessage());
32+
print_r($t->getTraceAsString());
33+
}
34+
try {
35+
$res = $mgClient->domainKeys()->createDomainKey($domain, sprintf('key-%s', time()));
36+
print_r($res);
37+
} catch (Throwable $t) {
38+
print_r($t->getMessage());
39+
print_r($t->getTraceAsString());
40+
}
41+
42+
try {
43+
$res = $mgClient->domainKeys()->deleteDomainKey($domain, 'key-xxx');
44+
} catch (Throwable $t) {
45+
print_r($t->getMessage());
46+
print_r($t->getTraceAsString());
47+
}
48+
49+
50+
try {
51+
$res = $mgClient->domainKeys()->createDomainKey($domain, sprintf('key-%s', time()));
52+
print_r($res);
53+
} catch (Throwable $t) {
54+
print_r($t->getMessage());
55+
print_r($t->getTraceAsString());
56+
}
57+
58+
```

src/Api/DomainKeys.php

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* Copyright (C) 2013 Mailgun
7+
*
8+
* This software may be modified and distributed under the terms
9+
* of the MIT license. See the LICENSE file for details.
10+
*/
11+
12+
namespace Mailgun\Api;
13+
14+
use Exception;
15+
use Mailgun\Assert;
16+
use Mailgun\Model\Domain\DeleteResponse;
17+
use Mailgun\Model\Domain\DomainKeyResponse;
18+
use Mailgun\Model\Domain\IndexResponse;
19+
use Psr\Http\Client\ClientExceptionInterface;
20+
use Psr\Http\Message\ResponseInterface;
21+
22+
/**
23+
* @see https://documentation.mailgun.com/docs/mailgun/api-reference/openapi-final/tag/Domain-Keys/
24+
*
25+
*/
26+
class DomainKeys extends HttpApi
27+
{
28+
private const BITS_SIZE = ['1024', '2048'];
29+
30+
/**
31+
* Returns a list of domains on the account.
32+
* @param int|null $limit
33+
* @param string|null $page
34+
* @param string|null $signingDomain
35+
* @param string|null $selector
36+
* @param array $requestHeaders
37+
* @return IndexResponse|array
38+
* @throws ClientExceptionInterface
39+
* @throws \JsonException
40+
* @throws Exception
41+
*/
42+
public function listKeysForDomains(?int $limit = null, ?string $page = null, ?string $signingDomain = null, ?string $selector = null, array $requestHeaders = [])
43+
{
44+
$params = [];
45+
if (isset($limit)) {
46+
Assert::range($limit, 1, 1000);
47+
$params['limit'] = $limit;
48+
}
49+
50+
if (isset($page)) {
51+
Assert::stringNotEmpty($page);
52+
$params['page'] = $page;
53+
}
54+
55+
if (isset($signingDomain)) {
56+
$params['signing_domain'] = $signingDomain;
57+
}
58+
59+
if (isset($selector)) {
60+
$params['selector'] = $signingDomain;
61+
}
62+
63+
$response = $this->httpGet('/v1/dkim/keys', $params, $requestHeaders);
64+
65+
return $this->hydrateResponse($response, IndexResponse::class);
66+
}
67+
68+
/**
69+
* Returns a list of domains on the account.
70+
* @param string $authorityName
71+
* @param array $requestHeaders
72+
* @return IndexResponse|array
73+
* @throws ClientExceptionInterface
74+
* @throws \JsonException
75+
* @throws Exception
76+
*/
77+
public function listDomainKeys(string $authorityName, array $requestHeaders = [])
78+
{
79+
Assert::stringNotEmpty($authorityName);
80+
81+
$response = $this->httpGet(sprintf('/v4/domains/%s/keys', $authorityName), [], $requestHeaders);
82+
83+
return $this->hydrateResponse($response, DomainKeyResponse::class);
84+
}
85+
86+
/**
87+
* @param string $signingDomain
88+
* @param string $selector
89+
* @param string|null $bits
90+
* @param array $requestHeaders
91+
* @return mixed|ResponseInterface
92+
* @throws ClientExceptionInterface
93+
* @throws \JsonException
94+
* @throws Exception
95+
*/
96+
public function createDomainKey(string $signingDomain, string $selector, ?string $bits = null, array $requestHeaders = [])
97+
{
98+
Assert::stringNotEmpty($signingDomain);
99+
Assert::stringNotEmpty($selector);
100+
101+
$params = [
102+
'signing_domain' => $signingDomain,
103+
'selector' => $selector,
104+
];
105+
106+
if (!empty($bits)) {
107+
Assert::oneOf(
108+
$bits,
109+
self::BITS_SIZE,
110+
'Length of your domain’s generated DKIM key must be 1024 or 2048'
111+
);
112+
$params['bits'] = $bits;
113+
}
114+
115+
$response = $this->httpPost('/v1/dkim/keys', $params, $requestHeaders);
116+
117+
return $this->hydrateResponse($response, DomainKeyResponse::class);
118+
}
119+
120+
/**
121+
* @param string $signingDomain
122+
* @param string $selector
123+
* @param array $requestHeaders
124+
* @return mixed|ResponseInterface
125+
* @throws ClientExceptionInterface
126+
* @throws \JsonException
127+
*/
128+
public function deleteDomainKey(string $signingDomain, string $selector, array $requestHeaders = [])
129+
{
130+
Assert::stringNotEmpty($signingDomain);
131+
Assert::stringNotEmpty($selector);
132+
133+
$params = [
134+
'signing_domain' => $signingDomain,
135+
'selector' => $selector,
136+
];
137+
138+
$response = $this->httpDelete('/v1/dkim/keys', $params, $requestHeaders);
139+
140+
return $this->hydrateResponse($response, DeleteResponse::class);
141+
}
142+
}

src/Mailgun.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Http\Client\Common\PluginClient;
1515
use Mailgun\Api\Attachment;
1616
use Mailgun\Api\Domain;
17+
use Mailgun\Api\DomainKeys;
1718
use Mailgun\Api\EmailValidation;
1819
use Mailgun\Api\EmailValidationV4;
1920
use Mailgun\Api\Event;
@@ -263,4 +264,12 @@ public function metrics(): Metrics
263264
{
264265
return new Metrics($this->httpClient, $this->requestBuilder, $this->hydrator);
265266
}
267+
268+
/**
269+
* @return DomainKeys
270+
*/
271+
public function domainKeys(): Api\DomainKeys
272+
{
273+
return new Api\DomainKeys($this->httpClient, $this->requestBuilder, $this->hydrator);
274+
}
266275
}

src/Model/Domain/DnsRecord.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ final class DnsRecord
2424
private ?string $priority;
2525
private ?string $valid;
2626
private array $cached;
27+
private bool $isActive;
2728

2829
public static function create(array $data): self
2930
{
@@ -34,6 +35,7 @@ public static function create(array $data): self
3435
$model->priority = $data['priority'] ?? null;
3536
$model->valid = $data['valid'] ?? null;
3637
$model->cached = $data['cached'] ?? [];
38+
$model->isActive = $data['is_active'] ?? false;
3739

3840
return $model;
3941
}
@@ -95,4 +97,21 @@ public function getCached(): array
9597
{
9698
return $this->cached;
9799
}
100+
101+
/**
102+
* @return bool
103+
*/
104+
public function isActive(): bool
105+
{
106+
return $this->isActive;
107+
}
108+
109+
/**
110+
* @param bool $isActive
111+
* @return void
112+
*/
113+
public function setIsActive(bool $isActive): void
114+
{
115+
$this->isActive = $isActive;
116+
}
98117
}
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* Copyright (C) 2013 Mailgun
7+
*
8+
* This software may be modified and distributed under the terms
9+
* of the MIT license. See the LICENSE file for details.
10+
*/
11+
12+
namespace Mailgun\Model\Domain;
13+
14+
use Mailgun\Model\ApiResponse;
15+
16+
final class DomainKeyResponse implements ApiResponse
17+
{
18+
private array $items = [];
19+
private string $signingDomain;
20+
private string $selector;
21+
private DnsRecord $dnsRecord;
22+
23+
/**
24+
* @param array $data
25+
* @return self
26+
*/
27+
public static function create(array $data): self
28+
{
29+
if (isset($data['items'])) {
30+
$object = new self();
31+
$items = [];
32+
foreach ($data['items'] as $item) {
33+
$model = new self();
34+
$model->setSelector($item['selector'] ?? '');
35+
$model->setSigningDomain($item['signing_domain'] ?? '');
36+
if (!empty($item['dns_record'])) {
37+
$model->setDnsRecord(DnsRecord::create($item['dns_record']));
38+
}
39+
40+
$items[] = $model;
41+
}
42+
$object->setItems($items);
43+
44+
return $object;
45+
}
46+
47+
$model = new self();
48+
$model->setSelector($data['selector'] ?? '');
49+
$model->setDnsRecord(DnsRecord::create($data));
50+
$model->setSigningDomain($data['signing_domain'] ?? '');
51+
52+
return $model;
53+
}
54+
55+
/**
56+
* @return string
57+
*/
58+
public function getSigningDomain(): string
59+
{
60+
return $this->signingDomain;
61+
}
62+
63+
/**
64+
* @param string $signingDomain
65+
*/
66+
public function setSigningDomain(string $signingDomain): void
67+
{
68+
$this->signingDomain = $signingDomain;
69+
}
70+
71+
/**
72+
* @return string
73+
*/
74+
public function getSelector(): string
75+
{
76+
return $this->selector;
77+
}
78+
79+
/**
80+
* @param string $selector
81+
*/
82+
public function setSelector(string $selector): void
83+
{
84+
$this->selector = $selector;
85+
}
86+
87+
/**
88+
* @return DnsRecord
89+
*/
90+
public function getDnsRecord(): DnsRecord
91+
{
92+
return $this->dnsRecord;
93+
}
94+
95+
/**
96+
* @param DnsRecord $dnsRecord
97+
*/
98+
public function setDnsRecord(DnsRecord $dnsRecord): void
99+
{
100+
$this->dnsRecord = $dnsRecord;
101+
}
102+
103+
/**
104+
* @return array
105+
*/
106+
public function getItems(): array
107+
{
108+
return $this->items;
109+
}
110+
111+
/**
112+
* @param array $items
113+
* @return void
114+
*/
115+
public function setItems(array $items): void
116+
{
117+
$this->items = $items;
118+
}
119+
120+
private function __construct()
121+
{
122+
}
123+
}

0 commit comments

Comments
 (0)