-
Notifications
You must be signed in to change notification settings - Fork 0
/
Api.php
58 lines (50 loc) · 1.45 KB
/
Api.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<?php declare(strict_types=1);
/*
* Copyright (C) Apis Networks, Inc - All Rights Reserved.
*
* Unauthorized copying of this file, via any medium, is
* strictly prohibited without consent. Any dissemination of
* material herein is prohibited.
*
* For licensing inquiries email <licensing@apisnetworks.com>
*
* Written by Matt Saladna <matt@apisnetworks.com>, October 2020
*/
namespace Opcenter\Mail\Providers\Mxroute;
use GuzzleHttp\Client;
class Api
{
public function getCanonicalName(string $nickname): ?string
{
return array_get((array)$this->getMetadata($nickname), 'hostname');
}
/**
* Get metadata from server
*
* @param string $nickname
* @return array|null
*/
public function getMetadata(string $nickname): ?array
{
static $cache = [];
if (isset($cache[$nickname])) {
return $cache[$nickname];
}
return $cache[$nickname ] = collect($this->request('/project/items/mailservers'))->first(static function ($v) use ($nickname) {
return $v['servername'] === $nickname;
});
}
public function getSPF(): string
{
return array_get($this->request('/project/items/spf'), '0.txt');
}
private function request(string $uri): array
{
return array_get((array)json_decode((new Client(['base_uri' => 'https://api.mxrouteapps.com']))->request('GET', $uri, [
'headers' => [
'User-Agent' => PANEL_BRAND . ' ' . APNSCP_VERSION,
'Accept' => 'application/json',
],
])->getBody()->getContents(), true), 'data', []);
}
}