Browse Source

classes to handle the underlying Deegraph connection

pull/1/head
Cerys Lewis 7 months ago
parent
commit
28b1279d6e
  1. 8
      composer.json
  2. 5
      composer.lock
  3. 79
      src/Core/DeegraphServer.php
  4. 23
      src/DataStructures/ServerInfo.php

8
composer.json

@ -14,7 +14,13 @@
"support": {
"email": "cerys@darksparrow.uk"
},
"autoload": {
"psr-4": {
"Darksparrow\\DeegraphPHP\\": "src/"
}
},
"require": {
"php": ">=8.1"
"php": ">=8.1",
"ext-curl": "*"
}
}

5
composer.lock

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "023300fef7b855f6db1c05b88104e2db",
"content-hash": "c0a0b3068a66e16e648ccfb67f35b37d",
"packages": [],
"packages-dev": [],
"aliases": [],
@ -13,7 +13,8 @@
"prefer-stable": false,
"prefer-lowest": false,
"platform": {
"php": ">=8.1"
"php": ">=8.1",
"ext-curl": "*"
},
"platform-dev": [],
"plugin-api-version": "2.6.0"

79
src/Core/DeegraphServer.php

@ -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);
}
}

23
src/DataStructures/ServerInfo.php

@ -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…
Cancel
Save