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

Allows for more flexible requests with params #5

Merged
merged 1 commit into from
Feb 13, 2022
Merged
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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "avametix/directapi",
"type": "library",
"version": "1.0.1",
"version": "1.0.2",
"description": "An API wrapper for the DirectAdmin API",
"keywords": [
"directadmin",
Expand Down
40 changes: 25 additions & 15 deletions src/Avametix/DirectApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,12 @@ public function logout() {
*
* @access public
* @param string $command The DirectAdmin API command to execute
* @param ?array $data The data to transmit with the DirectAdmin API request
* @param ?array $params The parameters to transmit with the DirectAdmin API request
* @param ?string $prefix The prefix to use with the DirectAdmin API request, allows for accessing plugin commands/API's
* @return array The parsed data
*/
public function get_api(string $command, ?array $data = null) {
return $this->get("/CMD_API_$command", $data);
public function get_api(string $command, ?array $params = null, ?string $prefix = "/CMD_API_") {
return $this->get("$prefix$command", $params);
}

/**
Expand All @@ -92,10 +93,12 @@ public function get_api(string $command, ?array $data = null) {
* @access public
* @param string $command The DirectAdmin API command to execute
* @param array $data The data to transmit with the DirectAdmin API request
* @param ?array $params The parameters to transmit with the DirectAdmin API request
* @param ?string $prefix The prefix to use with the DirectAdmin API request, allows for accessing plugin commands/API's
* @return array The parsed data
*/
public function post_api(string $command, array $data) {
return $this->post("/CMD_API_$command", $data);
public function post_api(string $command, array $data, ?array $params = null, ?string $prefix = "/CMD_API_") {
return $this->post("$prefix$command", $data, $params);
}

/**
Expand Down Expand Up @@ -168,23 +171,22 @@ private function parse_result(string $input) {
*
* @access private
* @param string $destination The page the request should go to
* @param ?array $data The data to send with the request
* @param ?array $params The parameters to send with the request
* @return object The parsed result
*/
private function get(string $destination, ?array $data = null) {
private function get(string $destination, ?array $params = null) {
// Combine already set variables to create a valid endpoint
$url = $this->protocol . "://" . $this->host . ":" . $this->port . $destination;

// Add data to GET request if data is set
if (!is_null($data) && isset($data))
$url .= "?" . http_build_query($data);
// Add parameters to GET request url if params are set
if (!is_null($params) && isset($params))
$url .= "?" . http_build_query($params);

// Set correct headers
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\nAuthorization: Basic " . base64_encode($this->username() . ":" . $this->password) . "\r\n",
'method' => 'GET',
'content' => ''
'method' => 'GET'
)
);

Expand All @@ -208,15 +210,23 @@ private function get(string $destination, ?array $data = null) {
* @access private
* @param string $destination The page the request should go to
* @param ?array $data The data to send with the request
* @param ?array $params The url parameters to send with the request
* @return object The parsed result
*/
private function post(string $destination, $data) {
private function post(string $destination, ?array $data = null, ?array $params = null) {
// Combine already set variables to create a valid endpoint
$url = $this->protocol . "://" . $this->host . ":" . $this->port . $destination;

foreach ($data as $key => $value) {
$data[$key] = str_replace("|password|", $this->password, $value);
// Replace data fields where necessary
if (!is_null($data)) {
foreach ($data as $key => $value) {
$data[$key] = str_replace("|password|", $this->password, $value);
}
}

// Add params to the end of the query
if (!is_null($params))
$url .= "?" . http_build_query($params);

// Set correct headers
$options = array(
Expand Down