From 28b1279d6e56fde8748c0ff742f037ef10bea3ab Mon Sep 17 00:00:00 2001 From: Cerys Lewis Date: Wed, 8 May 2024 12:42:12 +0100 Subject: [PATCH] classes to handle the underlying Deegraph connection --- composer.json | 46 ++++++++++-------- composer.lock | 5 +- src/Core/DeegraphServer.php | 79 +++++++++++++++++++++++++++++++ src/DataStructures/ServerInfo.php | 23 +++++++++ 4 files changed, 131 insertions(+), 22 deletions(-) mode change 100644 => 100755 composer.json mode change 100644 => 100755 composer.lock create mode 100755 src/Core/DeegraphServer.php create mode 100755 src/DataStructures/ServerInfo.php diff --git a/composer.json b/composer.json old mode 100644 new mode 100755 index 9cd3714..ed71111 --- a/composer.json +++ b/composer.json @@ -1,20 +1,26 @@ -{ - "name": "darksparrow/deegraph-php", - "description": "Deegraph Interactions Wrapper", - "type": "library", - "license": "MPL-2.0", - "authors": [ - { - "name": "Cerys Lewis", - "email": "cerys@darksparrow.uk", - "homepage": "https://ceryslewis.dev", - "role": "Lead Developer" - } - ], - "support": { - "email": "cerys@darksparrow.uk" - }, - "require": { - "php": ">=8.1" - } -} +{ + "name": "darksparrow/deegraph-php", + "description": "Deegraph Interactions Wrapper", + "type": "library", + "license": "MPL-2.0", + "authors": [ + { + "name": "Cerys Lewis", + "email": "cerys@darksparrow.uk", + "homepage": "https://ceryslewis.dev", + "role": "Lead Developer" + } + ], + "support": { + "email": "cerys@darksparrow.uk" + }, + "autoload": { + "psr-4": { + "Darksparrow\\DeegraphPHP\\": "src/" + } + }, + "require": { + "php": ">=8.1", + "ext-curl": "*" + } +} diff --git a/composer.lock b/composer.lock old mode 100644 new mode 100755 index af972ba..7ea328c --- a/composer.lock +++ b/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" diff --git a/src/Core/DeegraphServer.php b/src/Core/DeegraphServer.php new file mode 100755 index 0000000..be2969b --- /dev/null +++ b/src/Core/DeegraphServer.php @@ -0,0 +1,79 @@ +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 "


"; + $response = $this->RunRawRequest( + endpoint: "/api/v1/@query", + method: "POST", + body: $query + ); + var_dump($response); + } +} diff --git a/src/DataStructures/ServerInfo.php b/src/DataStructures/ServerInfo.php new file mode 100755 index 0000000..4c4b5ae --- /dev/null +++ b/src/DataStructures/ServerInfo.php @@ -0,0 +1,23 @@ +InstanceID = $temp["@instance_id"]; + $builder->InstanceFQDN = $temp["@instance_fqdn"]; + $builder->PublicKeys = $temp["@public_keys"]; + + return $builder; + } +}