Skip to content

Commit

Permalink
feat: support both post and get requests
Browse files Browse the repository at this point in the history
  • Loading branch information
ju5t committed Jun 2, 2024
1 parent 0caabb9 commit 736a668
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 5 deletions.
21 changes: 18 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,27 +43,42 @@ variables. These variables are used to authenticate with DirectAdmin

The username can be either an admin, reseller or user.

### Calling the API

You can call any DirectAdmin API by using the `DirectAdmin` facade:

```php
$result = DirectAdmin::call('{DIRECTADMIN_API_CALL}');
$result = DirectAdmin::post('{DIRECTADMIN_API_CALL}');
```

Alternatively you can use the `get` method to call the API. It depends on
the API call whether you should use `post` or `get`. Please refer to the
[DirectAdmin API documentation](https://docs.directadmin.com/directadmin/customizing-workflow/api-all-about.html) for more information.

```php
$result = DirectAdmin::get('{DIRECTADMIN_API_CALL}');
```

This will return a `Collection` of the response data.
### Impersonating a user

If you want to become a different user, and you are authenticated as an admin
user, you can use the `become` method:

```php
$result = DirectAdmin::become('user')->call('{DIRECTADMIN_API_CALL}');
$result = DirectAdmin::become('user')->post('{DIRECTADMIN_API_CALL}');
```

### Passing the DirectAdmin API call as a method

You can also call any DirectAdmin API command by passing it as method to
the `DirectAdmin` facade like so:

```php
$result = DirectAdmin::CMD_API_DOMAIN_OWNERS();
```

This will be a POST request by default.

For more information on the available commands, please refer to the
[DirectAdmin API documentation](https://docs.directadmin.com/directadmin/customizing-workflow/api-all-about.html).

Expand Down
15 changes: 13 additions & 2 deletions src/DirectAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Traits\Tappable;
use JsonException;
use Sensson\DirectAdmin\Enums\Method;
use Sensson\DirectAdmin\Exceptions\AuthenticationFailed;
use Sensson\DirectAdmin\Exceptions\CommandNotFound;
use Sensson\DirectAdmin\Exceptions\ConnectionFailed;
Expand All @@ -32,18 +33,28 @@ public function __construct() {
$this->password = config('directadmin.password');
}

public function post(string $command, array $params = []): Collection
{
return $this->call($command, $params);
}

public function get(string $command, array $params = []): Collection
{
return $this->call($command, $params, Method::GET);
}

/**
* Call the DirectAdmin API by giving it an API command and some
* parameters. This will return an array with processed data.
*/
public function call(string $command, array $params = []): Collection
public function call(string $command, array $params = [], Method $method = Method::POST): Collection
{
try {
$response = Http::acceptJson()
->withBasicAuth($this->username, $this->password)
->withOptions($this->getHttpOptions())
->withQueryParameters($this->getQueryParams())
->post($this->server.'/'.strtoupper($command), $params);
->{$method->verb()}($this->server.'/'.strtoupper($command), $params);
} catch (ConnectionException $e) {
throw ConnectionFailed::create($e->getMessage());
}
Expand Down
16 changes: 16 additions & 0 deletions src/Enums/Method.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Sensson\DirectAdmin\Enums;

enum Method: string
{
case GET = 'GET';
case POST = 'POST';
case PUT = 'PUT';
case DELETE = 'DELETE';

public function verb(): string
{
return strtolower($this->value);
}
}

0 comments on commit 736a668

Please sign in to comment.