66 lines
		
	
	
		
			1.6 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			66 lines
		
	
	
		
			1.6 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
| <?php
 | |
| 
 | |
| namespace App\Repositories\Daemon;
 | |
| 
 | |
| use GuzzleHttp\Client;
 | |
| use App\Models\Node;
 | |
| use Webmozart\Assert\Assert;
 | |
| use App\Models\Server;
 | |
| use Illuminate\Contracts\Foundation\Application;
 | |
| 
 | |
| abstract class DaemonRepository
 | |
| {
 | |
|     protected ?Server $server;
 | |
| 
 | |
|     protected ?Node $node;
 | |
| 
 | |
|     /**
 | |
|      * DaemonRepository constructor.
 | |
|      */
 | |
|     public function __construct(protected Application $app)
 | |
|     {
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * Set the server model this request is stemming from.
 | |
|      */
 | |
|     public function setServer(Server $server): static
 | |
|     {
 | |
|         $this->server = $server;
 | |
| 
 | |
|         $this->setNode($this->server->node);
 | |
| 
 | |
|         return $this;
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * Set the node model this request is stemming from.
 | |
|      */
 | |
|     public function setNode(Node $node): static
 | |
|     {
 | |
|         $this->node = $node;
 | |
| 
 | |
|         return $this;
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * Return an instance of the Guzzle HTTP Client to be used for requests.
 | |
|      */
 | |
|     public function getHttpClient(array $headers = []): Client
 | |
|     {
 | |
|         Assert::isInstanceOf($this->node, Node::class);
 | |
| 
 | |
|         return new Client([
 | |
|             'verify' => $this->app->environment('production'),
 | |
|             'base_uri' => $this->node->getConnectionAddress(),
 | |
|             'timeout' => config('panel.guzzle.timeout'),
 | |
|             'connect_timeout' => config('panel.guzzle.connect_timeout'),
 | |
|             'headers' => array_merge($headers, [
 | |
|                 'Authorization' => 'Bearer ' . $this->node->getDecryptedKey(),
 | |
|                 'Accept' => 'application/json',
 | |
|                 'Content-Type' => 'application/json',
 | |
|             ]),
 | |
|         ]);
 | |
|     }
 | |
| }
 | 
