PHPackages                             cyberfusion/cluster-api-client - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [API Development](/categories/api)
4. /
5. cyberfusion/cluster-api-client

Abandoned → [cyberfusion/core-api-client](/?search=cyberfusion%2Fcore-api-client)Library[API Development](/categories/api)

cyberfusion/cluster-api-client
==============================

Client for Cyberfusion Core API

v1.123.4(5mo ago)57.3k3MITPHPPHP ^8.1CI passing

Since Jan 21Pushed 5mo ago4 watchersCompare

[ Source](https://github.com/CyberfusionIO/cyberfusion-cluster-api-client)[ Packagist](https://packagist.org/packages/cyberfusion/cluster-api-client)[ Docs](https://github.com/CyberfusionIO/cyberfusion-cluster-api-client)[ RSS](/packages/cyberfusion-cluster-api-client/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)Dependencies (8)Versions (197)Used By (0)

⚠️ Replaced
===========

[](#️-replaced)

Use the new, supported, stable PHP client: [Cyberfusion Core API client](https://github.com/CyberfusionIO/php-core-api-client)

The new client is **auto-generated**, with a much better **developer experience**.

Want to migrate from this client? See the [migration guide](https://github.com/CyberfusionIO/php-core-api-client/blob/main/migration.md).

The current client will be **supported indefinitely**, but **new features** in the Core API will **not be added**. As the Core API is still in a state where breaking changes may be made, fixes in this client may be implemented with a delay. The new client is always up-to-date, as it is auto-generated. If your code relies on the Core API, please **upgrade to the new client**.

Cyberfusion Core API client
===========================

[](#cyberfusion-core-api-client)

Client for the [Cyberfusion Core API](https://core-api.cyberfusion.io/).

This client was built for and tested on the **1.238.0** version of the API.

Support
-------

[](#support)

This client is officially supported by Cyberfusion. If you have any questions, open an issue on GitHub or email .

The client was created by @dvdheiden.

Requirements
------------

[](#requirements)

This client requires PHP 8.1 or higher and uses Guzzle.

Installation
------------

[](#installation)

This client can be used in any PHP project and with any framework.

Install the client with Composer:

`composer require cyberfusion/cluster-api-client`

Usage
-----

[](#usage)

Refer to the [API documentation](https://core-api.cyberfusion.io/) for information about API requests.

### Getting started

[](#getting-started)

```
use Cyberfusion\ClusterApi\Client;
use Cyberfusion\ClusterApi\Configuration;
use Cyberfusion\ClusterApi\ClusterApi;

// Create the configuration with your username/password
$configuration = Configuration::withCredentials('username', 'password');

// Start the client once and authorize
$client = new Client($configuration);

// Initialize the API
$api = new ClusterApi($client);

// Perform the request
$result = $api->virtualHosts()->list();

// Access the virtual host models
$virtualHosts = $result->getData('virtualHosts');
```

### Requests

[](#requests)

The endpoint methods may ask for filters, models and IDs. The method type hints tell you which input is requested.

#### Models

[](#models)

The endpoint may request a model. Most create and update requests do.

```
$unixUserUsername = 'foo';

$unixUser = (new UnixUser())
    ->setUsername($unixUserUsername)
    ->setPassword('bar')
    ->setDefaultPhpVersion('7.4')
    ->setVirtualHostsDirectory(sprintf('/home/%d', $unixUserUsername))
    ->setClusterId(1);

$result = $api
    ->unixUsers()
    ->create($unixUser);
```

When models need to be provided, the required properties are checked before executing the request.

`RequestException` is thrown when properties are missing. See the error message for more details.

#### Filtering data

[](#filtering-data)

Some endpoints require a `ListFilter` and return a list of models according to the filter. It's also possible to change the sort order.

##### Model filters

[](#model-filters)

A `ListFilter` can be initialized for a model, so it automatically validates if the provided fields are available for the model.

```
use Cyberfusion\ClusterApi\Enums\Sort;
use Cyberfusion\ClusterApi\Models\VirtualHost;
use Cyberfusion\ClusterApi\Support\FilterEntry;
use Cyberfusion\ClusterApi\Support\SortEntry;

$listFilter = VirtualHost::listFilter()
    ->filter(new FilterEntry('server_software_name', 'Apache'))
    ->filter(new FilterEntry('domain', 'cyberfusion.nl'))
    ->sort(new SortEntry('domain', Sort::DESC));
```

##### Manually creating filters

[](#manually-creating-filters)

You can initialize the `ListFilter` manually, but fields are not validated if they are available for the model.

```
$listFilter = (new ListFilter())
    ->filter(new FilterEntry('server_software_name', 'Apache'))
    ->filter(new FilterEntry('domain', 'cyberfusion.nl'))
    ->sort(new SortEntry('domain', Sort::DESC));
```

Or provide the entries and sort directly:

```
$listFilter = (new ListFilter())
    ->setFilters([
        new FilterEntry('server_software_name', 'Apache'),
        new FilterEntry('domain', 'cyberfusion.nl'),
    ])
    ->setSort([
        new SortEntry('domain', Sort::DESC)
    ]);
);
```

##### Alternative method

[](#alternative-method)

This package used to offer this way to build the filter. Due to backwards compatibility, this functionality is still available.

```
$listFilter = ListFilter::forModel(new Cluster())
    ->filter('server_software_name', 'Apache')
    ->filter('domain', 'cyberfusion.nl')
    ->sort('domain', Sort::DESC);
```

Or provide the entries and sort directly:

```
$listFilter = (new ListFilter())
    ->setFilters([
        ['field' => 'server_software_name', 'value' => 'Apache'],
        ['field' => 'domain', 'value' => 'cyberfusion.nl'],
    ])
    ->setSort([
        ['field' => 'domain', 'value' => Sort::DESC],
    ]);
);
```

#### Manually make requests

[](#manually-make-requests)

To use the API directly, use the `request()` method on the `Client`. This method needs a `Request` class. See the class itself for its options.

### Responses

[](#responses)

The endpoint methods throw exceptions when requests fail due to timeouts. When the API replies with HTTP protocol errors, the `Response` class is returned nonetheless. Check if the request succeeded with: `$response->isSuccess()`. API responses are automatically converted to models.

### Authentication

[](#authentication)

The API is authenticated with a username and password and returns an access token. This client takes care of authentication. To get credentials, contact Cyberfusion.

```
$configuration = Configuration::withCredentials('username', 'password');
```

When you authenticate with username and password, this client automatically retrieves the access token.

The access token is valid for 30 minutes, so there's no need to store it. To store it anyway, access it with `$configuration->getAccessToken()`.

#### Manually authenticate

[](#manually-authenticate)

```
use Cyberfusion\ClusterApi\Client;
use Cyberfusion\ClusterApi\ClusterApi;
use Cyberfusion\ClusterApi\Configuration;
use Cyberfusion\ClusterApi\Models\Login;

// Initialize the configuration without any credentials or access token
$configuration = new Configuration();

// Start the client with manual authentication
$client = new Client($configuration, true);

// Initialize the API
$api = new ClusterApi($client);

// Create the request
$login = (new Login())
    ->setUsername('username')
    ->setPassword('password');

// Perform the request
$response = $api
    ->authentication()
    ->login($login);

// Store the access token in the configuration
if ($response->isSuccess()) {
    $configuration->setAccessToken($response->getData('access_token'));
}
```

### Enums

[](#enums)

Some properties should contain certain values. These values can be found in the enum classes.

### Exceptions

[](#exceptions)

In case of errors, the client throws an exception which extends `ClusterApiException`.

All exceptions have a code. These can be found in the `ClusterApiException` class.

### Laravel

[](#laravel)

This client can be easily used in any Laravel application. Add your API credentials to the `.env` file:

```
CLUSTER_USERNAME=username
CLUSTER_PASSWORD=password

```

Next, create a config file called `cluster.php` in the `config` directory:

```
