Cerys Lewis
7 months ago
4 changed files with 131 additions and 22 deletions
@ -0,0 +1,79 @@ |
|||
<?php |
|||
|
|||
namespace Darksparrow\DeegraphPHP\Core; |
|||
|
|||
use Darksparrow\DeegraphPHP\DataStructures\ServerInfo; |
|||
use Darksparrow\DeegraphPHP\QueryBuilder\QueryBuilders\Put; |
|||
|
|||
class DeegraphServer |
|||
{ |
|||
private string $ServerDomain; |
|||
private int $Port; |
|||
private string $Token; |
|||
private bool $AllowSelfSignedCerts; |
|||
|
|||
public function __construct(string $server, int $port, string $token, bool $allowSelfSignedCerts = false) |
|||
{ |
|||
$this->ServerDomain = $server; |
|||
$this->Port = $port; |
|||
$this->Token = $token; |
|||
$this->AllowSelfSignedCerts = $allowSelfSignedCerts; |
|||
} |
|||
|
|||
/** |
|||
* Runs an API request against the Deegraph Server, and returns back information about the Deegraph Server. |
|||
* @return ServerInfo |
|||
*/ |
|||
public function ServerInfo(): ServerInfo |
|||
{ |
|||
$response = $this->RunRawRequest(endpoint: "/api/v1/@server_info"); |
|||
return ServerInfo::FromAPIResponse(response: $response); |
|||
} |
|||
|
|||
private function RunRawRequest(string $endpoint, string $method = "GET", string $body = null): string |
|||
{ |
|||
$ch = curl_init("https://{$this->ServerDomain}:{$this->Port}{$endpoint}"); |
|||
curl_setopt($ch, CURLOPT_HTTPHEADER, [ |
|||
'Content-Type: application/json', |
|||
"Authorization: Bearer {$this->Token}", |
|||
]); |
|||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
|||
|
|||
if ($method == "POST") { |
|||
curl_setopt($ch, CURLOPT_POST, 1); |
|||
} |
|||
if($this->AllowSelfSignedCerts) |
|||
{ |
|||
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); |
|||
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); |
|||
} |
|||
|
|||
if ($method == "POST" || $method == "PUT") { |
|||
if ($body != null) { |
|||
curl_setopt($ch, CURLOPT_POSTFIELDS, $body); |
|||
} |
|||
} |
|||
|
|||
|
|||
$result = curl_exec($ch); |
|||
curl_close($ch); |
|||
|
|||
return $result; |
|||
} |
|||
|
|||
/** |
|||
* @param Put $query Takes in a Query Builder object. |
|||
* @return void |
|||
*/ |
|||
public function RunQuery(Put $query): void |
|||
{ |
|||
echo $query; |
|||
echo "<br><br><br>"; |
|||
$response = $this->RunRawRequest( |
|||
endpoint: "/api/v1/@query", |
|||
method: "POST", |
|||
body: $query |
|||
); |
|||
var_dump($response); |
|||
} |
|||
} |
@ -0,0 +1,23 @@ |
|||
<?php |
|||
|
|||
namespace Darksparrow\DeegraphPHP\DataStructures; |
|||
|
|||
class ServerInfo |
|||
{ |
|||
public string $InstanceID; |
|||
public string $InstanceFQDN; |
|||
public array $PublicKeys; |
|||
|
|||
public static function FromAPIResponse(string $response): ServerInfo |
|||
{ |
|||
$temp = json_decode($response, true); |
|||
|
|||
$builder = new ServerInfo(); |
|||
|
|||
$builder->InstanceID = $temp["@instance_id"]; |
|||
$builder->InstanceFQDN = $temp["@instance_fqdn"]; |
|||
$builder->PublicKeys = $temp["@public_keys"]; |
|||
|
|||
return $builder; |
|||
} |
|||
} |
Loading…
Reference in new issue