diff --git a/README.md b/README.md index ca63643..834f274 100644 --- a/README.md +++ b/README.md @@ -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). diff --git a/src/DirectAdmin.php b/src/DirectAdmin.php index 86dbbe7..cc36660 100755 --- a/src/DirectAdmin.php +++ b/src/DirectAdmin.php @@ -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; @@ -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()); } diff --git a/src/Enums/Method.php b/src/Enums/Method.php new file mode 100644 index 0000000..ebdcb27 --- /dev/null +++ b/src/Enums/Method.php @@ -0,0 +1,16 @@ +value); + } +}