Multiple fixes
* Implemented ScriptRunner with environment expansion and cleaner code. * Added ApiClient plugin (com.noccy.apiclient) * Renamed CHANGELOG.md to VERSIONS.md * Shuffled buildtools * Added first unittests
This commit is contained in:
		
							
								
								
									
										91
									
								
								plugins/com.noccy.apiclient/Request/HttpRequest.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										91
									
								
								plugins/com.noccy.apiclient/Request/HttpRequest.php
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,91 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
namespace SparkPlug\Com\Noccy\ApiClient\Request;
 | 
			
		||||
 | 
			
		||||
use GuzzleHttp\Client;
 | 
			
		||||
use GuzzleHttp\Psr7\Response;
 | 
			
		||||
 | 
			
		||||
class HttpRequest extends Request
 | 
			
		||||
{
 | 
			
		||||
 | 
			
		||||
    private string $method = 'GET';
 | 
			
		||||
 | 
			
		||||
    private string $version = '1.1';
 | 
			
		||||
 | 
			
		||||
    private ?string $url = null;
 | 
			
		||||
 | 
			
		||||
    private array $query = [];
 | 
			
		||||
 | 
			
		||||
    private array $headers = [];
 | 
			
		||||
 | 
			
		||||
    public function __construct(array $props)
 | 
			
		||||
    {
 | 
			
		||||
        foreach ($props as $prop=>$value) {
 | 
			
		||||
 | 
			
		||||
            if (str_starts_with($prop, 'http.')) {
 | 
			
		||||
                $this->handleHttpProp(substr($prop,5), $value);
 | 
			
		||||
            } elseif ($prop == 'url') {
 | 
			
		||||
                $this->url = $value;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private function handleHttpProp(string $prop, $value)
 | 
			
		||||
    {
 | 
			
		||||
 | 
			
		||||
        if (str_starts_with($prop, 'query.')) {
 | 
			
		||||
            $this->query[substr($prop, 6)] = $value;
 | 
			
		||||
        } elseif (str_starts_with($prop, 'header.')) {
 | 
			
		||||
            $this->headers[substr($prop, 7)] = $value;
 | 
			
		||||
        } elseif ($prop === 'method') {
 | 
			
		||||
            $this->method = strtoupper($value);
 | 
			
		||||
        } elseif ($prop === 'version') {
 | 
			
		||||
            $this->version = $value;
 | 
			
		||||
        } else {
 | 
			
		||||
            fprintf(STDERR, "Warning: unhandled prop: http.%s (%s)\n", $prop, $value);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function getInfo(): array
 | 
			
		||||
    {
 | 
			
		||||
        $query = http_build_query($this->query);
 | 
			
		||||
        $headers = [];
 | 
			
		||||
        foreach ($this->headers as $k=>$v) {
 | 
			
		||||
            // Convert to Proper-Case unless UPPERCASE
 | 
			
		||||
            if ($k !== strtoupper($k))
 | 
			
		||||
                $k = ucwords($k, '-');
 | 
			
		||||
            // Build the header
 | 
			
		||||
            $headers[] = sprintf("<options=bold>%s</>: %s", $k, $v);
 | 
			
		||||
        }
 | 
			
		||||
        return [
 | 
			
		||||
            'protocol' => sprintf("HTTP/%s %s", $this->version, $this->method),
 | 
			
		||||
            'query' => $this->url . "?" . $query,
 | 
			
		||||
            'body' => "Empty body"
 | 
			
		||||
        ];
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function getHeaders(): array
 | 
			
		||||
    {
 | 
			
		||||
        $headers = [];
 | 
			
		||||
        foreach ($this->headers as $k=>$v) {
 | 
			
		||||
            // Convert to Proper-Case unless UPPERCASE
 | 
			
		||||
            if ($k !== strtoupper($k))
 | 
			
		||||
                $k = ucwords($k, '-');
 | 
			
		||||
            // Build the header
 | 
			
		||||
            $headers[$k] = (array)$v;
 | 
			
		||||
        }
 | 
			
		||||
        return $headers;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function send(): ?Response
 | 
			
		||||
    {
 | 
			
		||||
        $query = http_build_query($this->query);
 | 
			
		||||
        $url = $this->url . ($query?'?'.$query:''); 
 | 
			
		||||
        $config = [];
 | 
			
		||||
        $client = new Client($config);
 | 
			
		||||
        $options = [];
 | 
			
		||||
        $response = $client->request($this->method, $url, $options);
 | 
			
		||||
        return $response;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										26
									
								
								plugins/com.noccy.apiclient/Request/JsonRpcRequest.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										26
									
								
								plugins/com.noccy.apiclient/Request/JsonRpcRequest.php
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,26 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
namespace SparkPlug\Com\Noccy\ApiClient\Request;
 | 
			
		||||
 | 
			
		||||
use GuzzleHttp\Psr7\Response;
 | 
			
		||||
 | 
			
		||||
class JsonRpcRequest extends Request
 | 
			
		||||
{
 | 
			
		||||
 | 
			
		||||
    public function getInfo(): array
 | 
			
		||||
    {
 | 
			
		||||
        return [
 | 
			
		||||
        ];
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function send(): ?Response
 | 
			
		||||
    {
 | 
			
		||||
        return null;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function getHeaders(): array
 | 
			
		||||
    {
 | 
			
		||||
        return [];
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										16
									
								
								plugins/com.noccy.apiclient/Request/Request.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										16
									
								
								plugins/com.noccy.apiclient/Request/Request.php
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,16 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
namespace SparkPlug\Com\Noccy\ApiClient\Request;
 | 
			
		||||
 | 
			
		||||
use GuzzleHttp\Psr7\Response;
 | 
			
		||||
 | 
			
		||||
abstract class Request
 | 
			
		||||
{
 | 
			
		||||
 | 
			
		||||
    abstract public function send(): ?Response;
 | 
			
		||||
 | 
			
		||||
    abstract public function getInfo(): array;
 | 
			
		||||
 | 
			
		||||
    abstract public function getHeaders(): array;
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										100
									
								
								plugins/com.noccy.apiclient/Request/RequestBuilder.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										100
									
								
								plugins/com.noccy.apiclient/Request/RequestBuilder.php
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,100 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
namespace SparkPlug\Com\Noccy\ApiClient\Request;
 | 
			
		||||
 | 
			
		||||
use SparkPlug\Com\Noccy\ApiClient\Api\Catalog;
 | 
			
		||||
use SparkPlug\Com\Noccy\ApiClient\Api\Method;
 | 
			
		||||
use SparkPlug\Com\Noccy\ApiClient\Api\Profile;
 | 
			
		||||
 | 
			
		||||
class RequestBuilder
 | 
			
		||||
{
 | 
			
		||||
    public static $Protocols = [
 | 
			
		||||
        'http' => HttpRequest::class,
 | 
			
		||||
        'websocket' => WebSocketRequest::class,
 | 
			
		||||
        'jsonrpc' => JsonRpcRequest::class,
 | 
			
		||||
    ];
 | 
			
		||||
 | 
			
		||||
    private ?Catalog $catalog = null;
 | 
			
		||||
 | 
			
		||||
    private ?Method $method = null;
 | 
			
		||||
 | 
			
		||||
    private ?Profile $profile = null;
 | 
			
		||||
 | 
			
		||||
    private array $props = [];
 | 
			
		||||
 | 
			
		||||
    public function setCatalog(?Catalog $catalog)
 | 
			
		||||
    {
 | 
			
		||||
        $this->catalog = $catalog;
 | 
			
		||||
        return $this;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function setMethod(?Method $method)
 | 
			
		||||
    {
 | 
			
		||||
        $this->method = $method;
 | 
			
		||||
        return $this;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function setProfile(?Profile $profile)
 | 
			
		||||
    {
 | 
			
		||||
        $this->profile = $profile;
 | 
			
		||||
        return $this;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function setProperties(array $properties)
 | 
			
		||||
    {
 | 
			
		||||
        $this->props = $properties;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function addProperties(array $properties)
 | 
			
		||||
    {
 | 
			
		||||
        $this->props = array_merge(
 | 
			
		||||
            $this->props,
 | 
			
		||||
            $properties
 | 
			
		||||
        );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private function buildProperties()
 | 
			
		||||
    {
 | 
			
		||||
        $props = [];
 | 
			
		||||
        if ($this->catalog) {
 | 
			
		||||
            $add = $this->catalog->getProperties();
 | 
			
		||||
            $props = array_merge($props, $add);
 | 
			
		||||
        }
 | 
			
		||||
        if ($this->method) {
 | 
			
		||||
            $add = $this->method->getProperties();
 | 
			
		||||
            $props = array_merge($props, $add);
 | 
			
		||||
        }
 | 
			
		||||
        if ($this->profile) {
 | 
			
		||||
            $add = $this->profile->getProperties();
 | 
			
		||||
            $props = array_merge($props, $add);
 | 
			
		||||
        }
 | 
			
		||||
        $props = array_merge($props, $this->props);
 | 
			
		||||
        $props = array_filter($props);
 | 
			
		||||
        return $props;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function getCalculatedProperty(string $name)
 | 
			
		||||
    {
 | 
			
		||||
        $props = $this->buildProperties();
 | 
			
		||||
        return $props[$name] ?? null;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function getRequest(): Request
 | 
			
		||||
    {
 | 
			
		||||
        $props = $this->buildProperties();
 | 
			
		||||
        $protocol = $props['protocol']??'http';
 | 
			
		||||
 | 
			
		||||
        if (!$handler = self::$Protocols[$protocol]??null) {
 | 
			
		||||
            throw new \Exception("Invalid protocol for request: {$protocol}");
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        $base = $props['urlbase']??null;
 | 
			
		||||
        $url = $props['url']??null;
 | 
			
		||||
        if ($base) {
 | 
			
		||||
            $props['url'] = $base . $url;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        $request = new $handler($props);
 | 
			
		||||
        return $request;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										26
									
								
								plugins/com.noccy.apiclient/Request/WebsocketRequest.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										26
									
								
								plugins/com.noccy.apiclient/Request/WebsocketRequest.php
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,26 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
namespace SparkPlug\Com\Noccy\ApiClient\Request;
 | 
			
		||||
 | 
			
		||||
use GuzzleHttp\Psr7\Response;
 | 
			
		||||
 | 
			
		||||
class WebsocketRequest extends Request
 | 
			
		||||
{
 | 
			
		||||
 | 
			
		||||
    public function getInfo(): array
 | 
			
		||||
    {
 | 
			
		||||
        return [
 | 
			
		||||
        ];
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function send(): ?Response
 | 
			
		||||
    {
 | 
			
		||||
        return null;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function getHeaders(): array
 | 
			
		||||
    {
 | 
			
		||||
        return [];
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user