|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Intercom\IpAllowlist; |
| 4 | + |
| 5 | +use GuzzleHttp\ClientInterface; |
| 6 | +use Intercom\Core\Client\RawClient; |
| 7 | +use Intercom\Types\IpAllowlist; |
| 8 | +use Intercom\Exceptions\IntercomException; |
| 9 | +use Intercom\Exceptions\IntercomApiException; |
| 10 | +use Intercom\Core\Json\JsonApiRequest; |
| 11 | +use Intercom\Environments; |
| 12 | +use Intercom\Core\Client\HttpMethod; |
| 13 | +use JsonException; |
| 14 | +use GuzzleHttp\Exception\RequestException; |
| 15 | +use Psr\Http\Client\ClientExceptionInterface; |
| 16 | + |
| 17 | +class IpAllowlistClient |
| 18 | +{ |
| 19 | + /** |
| 20 | + * @var array{ |
| 21 | + * baseUrl?: string, |
| 22 | + * client?: ClientInterface, |
| 23 | + * maxRetries?: int, |
| 24 | + * timeout?: float, |
| 25 | + * headers?: array<string, string>, |
| 26 | + * } $options @phpstan-ignore-next-line Property is used in endpoint methods via HttpEndpointGenerator |
| 27 | + */ |
| 28 | + private array $options; |
| 29 | + |
| 30 | + /** |
| 31 | + * @var RawClient $client |
| 32 | + */ |
| 33 | + private RawClient $client; |
| 34 | + |
| 35 | + /** |
| 36 | + * @param RawClient $client |
| 37 | + * @param ?array{ |
| 38 | + * baseUrl?: string, |
| 39 | + * client?: ClientInterface, |
| 40 | + * maxRetries?: int, |
| 41 | + * timeout?: float, |
| 42 | + * headers?: array<string, string>, |
| 43 | + * } $options |
| 44 | + */ |
| 45 | + public function __construct( |
| 46 | + RawClient $client, |
| 47 | + ?array $options = null, |
| 48 | + ) { |
| 49 | + $this->client = $client; |
| 50 | + $this->options = $options ?? []; |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Retrieve the current IP allowlist configuration for the workspace. |
| 55 | + * |
| 56 | + * @param ?array{ |
| 57 | + * baseUrl?: string, |
| 58 | + * maxRetries?: int, |
| 59 | + * timeout?: float, |
| 60 | + * headers?: array<string, string>, |
| 61 | + * queryParameters?: array<string, mixed>, |
| 62 | + * bodyProperties?: array<string, mixed>, |
| 63 | + * } $options |
| 64 | + * @return IpAllowlist |
| 65 | + * @throws IntercomException |
| 66 | + * @throws IntercomApiException |
| 67 | + */ |
| 68 | + public function getIpAllowlist(?array $options = null): IpAllowlist |
| 69 | + { |
| 70 | + $options = array_merge($this->options, $options ?? []); |
| 71 | + try { |
| 72 | + $response = $this->client->sendRequest( |
| 73 | + new JsonApiRequest( |
| 74 | + baseUrl: $options['baseUrl'] ?? $this->client->options['baseUrl'] ?? Environments::UsProduction->value, |
| 75 | + path: "ip_allowlist", |
| 76 | + method: HttpMethod::GET, |
| 77 | + ), |
| 78 | + $options, |
| 79 | + ); |
| 80 | + $statusCode = $response->getStatusCode(); |
| 81 | + if ($statusCode >= 200 && $statusCode < 400) { |
| 82 | + $json = $response->getBody()->getContents(); |
| 83 | + return IpAllowlist::fromJson($json); |
| 84 | + } |
| 85 | + } catch (JsonException $e) { |
| 86 | + throw new IntercomException(message: "Failed to deserialize response: {$e->getMessage()}", previous: $e); |
| 87 | + } catch (RequestException $e) { |
| 88 | + $response = $e->getResponse(); |
| 89 | + if ($response === null) { |
| 90 | + throw new IntercomException(message: $e->getMessage(), previous: $e); |
| 91 | + } |
| 92 | + throw new IntercomApiException( |
| 93 | + message: "API request failed", |
| 94 | + statusCode: $response->getStatusCode(), |
| 95 | + body: $response->getBody()->getContents(), |
| 96 | + ); |
| 97 | + } catch (ClientExceptionInterface $e) { |
| 98 | + throw new IntercomException(message: $e->getMessage(), previous: $e); |
| 99 | + } |
| 100 | + throw new IntercomApiException( |
| 101 | + message: 'API request failed', |
| 102 | + statusCode: $statusCode, |
| 103 | + body: $response->getBody()->getContents(), |
| 104 | + ); |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Update the IP allowlist configuration for the workspace. |
| 109 | + * |
| 110 | + * {% admonition type="warning" name="Lockout Protection" %} |
| 111 | + * The API will reject updates that would lock out the caller's IP address. Ensure your current IP is included in the allowlist when enabling the feature. |
| 112 | + * {% /admonition %} |
| 113 | + * |
| 114 | + * @param IpAllowlist $request |
| 115 | + * @param ?array{ |
| 116 | + * baseUrl?: string, |
| 117 | + * maxRetries?: int, |
| 118 | + * timeout?: float, |
| 119 | + * headers?: array<string, string>, |
| 120 | + * queryParameters?: array<string, mixed>, |
| 121 | + * bodyProperties?: array<string, mixed>, |
| 122 | + * } $options |
| 123 | + * @return IpAllowlist |
| 124 | + * @throws IntercomException |
| 125 | + * @throws IntercomApiException |
| 126 | + */ |
| 127 | + public function updateIpAllowlist(IpAllowlist $request, ?array $options = null): IpAllowlist |
| 128 | + { |
| 129 | + $options = array_merge($this->options, $options ?? []); |
| 130 | + try { |
| 131 | + $response = $this->client->sendRequest( |
| 132 | + new JsonApiRequest( |
| 133 | + baseUrl: $options['baseUrl'] ?? $this->client->options['baseUrl'] ?? Environments::UsProduction->value, |
| 134 | + path: "ip_allowlist", |
| 135 | + method: HttpMethod::PUT, |
| 136 | + body: $request, |
| 137 | + ), |
| 138 | + $options, |
| 139 | + ); |
| 140 | + $statusCode = $response->getStatusCode(); |
| 141 | + if ($statusCode >= 200 && $statusCode < 400) { |
| 142 | + $json = $response->getBody()->getContents(); |
| 143 | + return IpAllowlist::fromJson($json); |
| 144 | + } |
| 145 | + } catch (JsonException $e) { |
| 146 | + throw new IntercomException(message: "Failed to deserialize response: {$e->getMessage()}", previous: $e); |
| 147 | + } catch (RequestException $e) { |
| 148 | + $response = $e->getResponse(); |
| 149 | + if ($response === null) { |
| 150 | + throw new IntercomException(message: $e->getMessage(), previous: $e); |
| 151 | + } |
| 152 | + throw new IntercomApiException( |
| 153 | + message: "API request failed", |
| 154 | + statusCode: $response->getStatusCode(), |
| 155 | + body: $response->getBody()->getContents(), |
| 156 | + ); |
| 157 | + } catch (ClientExceptionInterface $e) { |
| 158 | + throw new IntercomException(message: $e->getMessage(), previous: $e); |
| 159 | + } |
| 160 | + throw new IntercomApiException( |
| 161 | + message: 'API request failed', |
| 162 | + statusCode: $statusCode, |
| 163 | + body: $response->getBody()->getContents(), |
| 164 | + ); |
| 165 | + } |
| 166 | +} |
0 commit comments