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 1262 api i ps endpoint. In Progress #901

Merged
merged 5 commits into from
Apr 24, 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
128 changes: 116 additions & 12 deletions src/Api/Ip.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@

namespace Mailgun\Api;

use Exception;
use Mailgun\Assert;
use Mailgun\Model\Ip\AvailableIpsResponse;
use Mailgun\Model\Ip\IndexResponse;
use Mailgun\Model\Ip\ShowResponse;
use Mailgun\Model\Ip\UpdateResponse;
Expand All @@ -27,10 +29,11 @@ class Ip extends HttpApi
{
/**
* Returns a list of IPs.
* @param bool|null $dedicated
* @param array $requestHeaders
* @param bool|null $dedicated
* @param array $requestHeaders
* @return IndexResponse|ResponseInterface
* @throws ClientExceptionInterface
* @throws Exception
*/
public function index(?bool $dedicated = null, array $requestHeaders = [])
{
Expand All @@ -47,10 +50,11 @@ public function index(?bool $dedicated = null, array $requestHeaders = [])

/**
* Returns a list of IPs assigned to a domain.
* @param string $domain
* @param array $requestHeaders
* @param string $domain
* @param array $requestHeaders
* @return IndexResponse|ResponseInterface
* @throws ClientExceptionInterface
* @throws Exception
*/
public function domainIndex(string $domain, array $requestHeaders = [])
{
Expand All @@ -63,10 +67,11 @@ public function domainIndex(string $domain, array $requestHeaders = [])

/**
* Returns a single ip.
* @param string $ip
* @param array $requestHeaders
* @param string $ip
* @param array $requestHeaders
* @return ShowResponse|ResponseInterface
* @throws ClientExceptionInterface
* @throws Exception
*/
public function show(string $ip, array $requestHeaders = [])
{
Expand All @@ -79,11 +84,12 @@ public function show(string $ip, array $requestHeaders = [])

/**
* Assign a dedicated IP to the domain specified.
* @param string $domain
* @param string $ip
* @param array $requestHeaders
* @param string $domain
* @param string $ip
* @param array $requestHeaders
* @return UpdateResponse|ResponseInterface
* @throws ClientExceptionInterface
* @throws Exception
*/
public function assign(string $domain, string $ip, array $requestHeaders = [])
{
Expand All @@ -101,11 +107,12 @@ public function assign(string $domain, string $ip, array $requestHeaders = [])

/**
* Unassign an IP from the domain specified.
* @param string $domain
* @param string $ip
* @param array $requestHeaders
* @param string $domain
* @param string $ip
* @param array $requestHeaders
* @return UpdateResponse|ResponseInterface
* @throws ClientExceptionInterface
* @throws Exception
*/
public function unassign(string $domain, string $ip, array $requestHeaders = [])
{
Expand All @@ -116,4 +123,101 @@ public function unassign(string $domain, string $ip, array $requestHeaders = [])

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

/**
* Get all domains of an account where a specific IP is assigned
* @param string $ip
* @param int $limit
* @param int $skip
* @param string|null $search
* @param array $requestHeaders
* @return IndexResponse|ResponseInterface
* @throws ClientExceptionInterface
* @throws Exception
*/
public function domainsByIp(string $ip, int $limit = 10, int $skip = 0, ?string $search = null, array $requestHeaders = [])
{
Assert::stringNotEmpty($ip);

$params = [
'limit' => $limit,
'skip' => $skip,
];
if (null !== $search) {
$params['search'] = $search;
}

$response = $this->httpGet(sprintf('/v3/ips/%s/domains', $ip), $params, $requestHeaders);

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

/**
* Place account IP into a dedicated IP band
* @param string $ip
* @param array $requestHeaders
* @return UpdateResponse|ResponseInterface
* @throws ClientExceptionInterface
* @throws Exception
*/
public function placeAccountIpToBand(string $ip, array $requestHeaders = [])
{
Assert::stringNotEmpty($ip);
Assert::ip($ip);

$payload = [
'ip_band' => $ip,
];

$response = $this->httpPost(sprintf('/v3/ips/%s/ip_band', $ip), $payload, $requestHeaders);

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

/**
* Return the number of IPs available to the account per its billing plan
* @param array $requestHeaders
* @return AvailableIpsResponse|ResponseInterface
* @throws ClientExceptionInterface
* @throws Exception
*/
public function numberOfIps(array $requestHeaders = [])
{
$response = $this->httpGet('v3/ips/request/new', [], $requestHeaders);

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

/**
* Add a new dedicated IP to the account
* @param array $requestHeaders
* @return UpdateResponse|ResponseInterface
* @throws ClientExceptionInterface
* @throws Exception
*/
public function addDedicatedIp(array $requestHeaders = [])
{
$response = $this->httpPost('v3/ips/request/new', [], $requestHeaders);

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

/**
* Remove an IP from the domain pool, unlink a DIPP or remove the domain pool
* @param string $domain
* @param string $ip
* @param array $requestHeaders
* @return UpdateResponse|ResponseInterface
* @throws ClientExceptionInterface
* @throws Exception
*/
public function removeIpOrUnlink(string $domain, string $ip, array $requestHeaders = [])
{
Assert::stringNotEmpty($domain);
Assert::ip($ip);

$response = $this->httpDelete(sprintf('/v3/domains/%s/pool/%s', $domain, $ip), [], $requestHeaders);

return $this->hydrateResponse($response, UpdateResponse::class);
}
}
62 changes: 62 additions & 0 deletions src/Model/Ip/AvailableIpsResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?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\Ip;

use Mailgun\Model\ApiResponse;

/**
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
*/
final class AvailableIpsResponse implements ApiResponse
{
/**
* @var int
*/
private $dedicated;

/**
* @var int
*/
private $shared;

private function __construct()
{
}

/**
* @param array $data
* @return AvailableIpsResponse
*/
public static function create(array $data)
{
$model = new self();
$model->dedicated = $data['allowed']['dedicated'] ?? 0;
$model->shared = $data['allowed']['shared'] ?? 0;
return $model;
}

/**
* @return int
*/
public function getDedicated(): int
{
return $this->dedicated;
}

/**
* @return int
*/
public function getShared(): int
{
return $this->shared;
}
}
Loading