Browse Source

classes to handle the underlying Deegraph connection

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

46
composer.json

@ -1,20 +1,26 @@
{ {
"name": "darksparrow/deegraph-php", "name": "darksparrow/deegraph-php",
"description": "Deegraph Interactions Wrapper", "description": "Deegraph Interactions Wrapper",
"type": "library", "type": "library",
"license": "MPL-2.0", "license": "MPL-2.0",
"authors": [ "authors": [
{ {
"name": "Cerys Lewis", "name": "Cerys Lewis",
"email": "cerys@darksparrow.uk", "email": "cerys@darksparrow.uk",
"homepage": "https://ceryslewis.dev", "homepage": "https://ceryslewis.dev",
"role": "Lead Developer" "role": "Lead Developer"
} }
], ],
"support": { "support": {
"email": "cerys@darksparrow.uk" "email": "cerys@darksparrow.uk"
}, },
"require": { "autoload": {
"php": ">=8.1" "psr-4": {
} "Darksparrow\\DeegraphPHP\\": "src/"
} }
},
"require": {
"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", "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically" "This file is @generated automatically"
], ],
"content-hash": "023300fef7b855f6db1c05b88104e2db", "content-hash": "c0a0b3068a66e16e648ccfb67f35b37d",
"packages": [], "packages": [],
"packages-dev": [], "packages-dev": [],
"aliases": [], "aliases": [],
@ -13,7 +13,8 @@
"prefer-stable": false, "prefer-stable": false,
"prefer-lowest": false, "prefer-lowest": false,
"platform": { "platform": {
"php": ">=8.1" "php": ">=8.1",
"ext-curl": "*"
}, },
"platform-dev": [], "platform-dev": [],
"plugin-api-version": "2.6.0" "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