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

feat: Add Images #231

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Each API call is provided via a similarly named function within various classes
- [x] Crypto
- [x] Load Balancers
- [x] Firewall Settings
- [x] [Images](https://www.cloudflare.com/products/cloudflare-images/)

Note that this repository is currently under development, additional classes and endpoints being actively added.

Expand All @@ -42,6 +43,16 @@ $user = new Cloudflare\API\Endpoints\User($adapter);
echo $user->getUserID();
```

## Getting started with images

```php
$key = new Cloudflare\API\Auth\APIToken('apiToken');
$adapter = new Cloudflare\API\Adapter\Guzzle($key);
$images = new Cloudflare\API\Endpoints\Images($adapter);

var_dump($images->listImages('accountId'));
```

## Contributions

We welcome community contribution to this repository. [CONTRIBUTING.md](CONTRIBUTING.md) will help you start contributing.
Expand Down
2 changes: 1 addition & 1 deletion src/Adapter/Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function get(string $uri, array $data = [], array $headers = []): Respons
*
* @return mixed
*/
public function post(string $uri, array $data = [], array $headers = []): ResponseInterface;
public function post(string $uri, array $data = [], array $headers = [], bool $upload = false): ResponseInterface;

/**
* @param string $uri
Expand Down
8 changes: 4 additions & 4 deletions src/Adapter/Guzzle.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ public function get(string $uri, array $data = [], array $headers = []): Respons
/**
* @inheritDoc
*/
public function post(string $uri, array $data = [], array $headers = []): ResponseInterface
public function post(string $uri, array $data = [], array $headers = [], bool $upload = false): ResponseInterface
{
return $this->request('post', $uri, $data, $headers);
return $this->request('post', $uri, $data, $headers, $upload);
}

/**
Expand Down Expand Up @@ -73,7 +73,7 @@ public function delete(string $uri, array $data = [], array $headers = []): Resp
/**
* @SuppressWarnings(PHPMD.StaticAccess)
*/
public function request(string $method, string $uri, array $data = [], array $headers = [])
public function request(string $method, string $uri, array $data = [], array $headers = [], bool $upload = false)
{
if (!in_array($method, ['get', 'post', 'put', 'patch', 'delete'])) {
throw new \InvalidArgumentException('Request method must be get, post, put, patch, or delete');
Expand All @@ -82,7 +82,7 @@ public function request(string $method, string $uri, array $data = [], array $he
try {
$response = $this->client->$method($uri, [
'headers' => $headers,
($method === 'get' ? 'query' : 'json') => $data,
($method === 'get' ? 'query' : ($upload ? 'multipart' : 'json')) => $data,
]);
} catch (RequestException $err) {
throw ResponseException::fromRequestException($err);
Expand Down
28 changes: 25 additions & 3 deletions src/Endpoints/CustomHostnames.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public function __construct(Adapter $adapter)
* @param bool $wildcard
* @param string $bundleMethod
* @param array $customSsl
* @param string $sslCertificateAuthority
* @return \stdClass
*/
public function addHostname(
Expand All @@ -46,7 +47,8 @@ public function addHostname(
string $customOriginServer = '',
bool $wildcard = false,
string $bundleMethod = '',
array $customSsl = []
array $customSsl = [],
string $sslCertificateAuthority = 'lets_encrypt'
): \stdClass {
$options = [
'hostname' => $hostname,
Expand Down Expand Up @@ -74,6 +76,10 @@ public function addHostname(
$options['ssl']['custom_certificate'] = $customSsl['certificate'];
}

if (!empty($sslCertificateAuthority)) {
$options['ssl']['certificate_authority'] = $sslCertificateAuthority;
}

$zone = $this->adapter->post('zones/'.$zoneID.'/custom_hostnames', $options);
$this->body = json_decode($zone->getBody());
return $this->body->result;
Expand All @@ -98,7 +104,9 @@ public function listHostnames(
int $perPage = 20,
string $order = '',
string $direction = '',
int $ssl = 0
int $ssl = 0,
string $hostname_status = '',
string $ssl_status = ''
): \stdClass {
$query = [
'page' => $page,
Expand All @@ -122,6 +130,14 @@ public function listHostnames(
$query['direction'] = $direction;
}

if (!empty($hostname_status)) {
$query['hostname_status'] = $hostname_status;
}

if (!empty($ssl_status)) {
$query['ssl_status'] = $ssl_status;
}

$zone = $this->adapter->get('zones/'.$zoneID.'/custom_hostnames', $query);
$this->body = json_decode($zone->getBody());

Expand Down Expand Up @@ -155,6 +171,7 @@ public function getHostname(string $zoneID, string $hostnameID)
* @param bool|null $wildcard
* @param string $bundleMethod
* @param array $customSsl
* @param string $sslCertificateAuthority
* @return \stdClass
*/
public function updateHostname(
Expand All @@ -166,7 +183,8 @@ public function updateHostname(
string $customOriginServer = '',
bool $wildcard = null,
string $bundleMethod = '',
array $customSsl = []
array $customSsl = [],
string $sslCertificateAuthority = 'lets_encrypt'
): \stdClass {
$query = [];
$options = [];
Expand Down Expand Up @@ -199,6 +217,10 @@ public function updateHostname(
$query['custom_certificate'] = $customSsl['certificate'];
}

if (!empty($sslCertificateAuthority)) {
$query['certificate_authority'] = $sslCertificateAuthority;
}

if (!empty($query)) {
$options = [
'ssl' => $query
Expand Down
Loading