PHP code example of hampel / binarylane-api-laravel

1. Go to this page and download the library: Download hampel/binarylane-api-laravel library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

hampel / binarylane-api-laravel example snippets


'providers' => [
    Hampel\BinaryLane\Api\Laravel\BinaryLaneServiceProvider::class,
],

'default' => 'production',

'accounts' => [
    'production' => [
        'token' => env('BINARYLANE_API_TOKEN'),
    ],

    'reseller' => [
        'token' => env('RESELLER_BINARYLANE_API_TOKEN'),
    ],
],

'per_page' => env('BINARYLANE_PER_PAGE'),   // 1 to 200; null uses the API's default of 20
'base_uri' => env('BINARYLANE_API_URL'),    // null uses BinaryLane's own host

'timeout' => 10,
'connect_timeout' => 5,

use Hampel\BinaryLane\Api\Laravel\Facades\BinaryLane;

$account = BinaryLane::verify();               // does this token work, and is the account usable?

$servers = BinaryLane::servers()->list();      // the first page - 20 unless configured otherwise
$server = BinaryLane::servers()->get(1234);

$servers = BinaryLane::client('reseller')->servers()->list();

use Hampel\BinaryLane\Api\Laravel\BinaryLaneManager;

public function __construct(private readonly BinaryLaneManager $binarylane) {}

$this->binarylane->client('reseller')->servers()->list();

use Hampel\BinaryLane\Api\Laravel\Jobs\AwaitAction;

$action = BinaryLane::serverActions()->powerOn(1234);

if ($action !== null) {
    dispatch(new AwaitAction($action));
}

$created = BinaryLane::servers()->create($request);

foreach ($created->actionIds() as $id) {
    dispatch(new AwaitAction($id, timeout: 7200));
}

new AwaitAction($action, account: 'reseller', timeout: 3600, interval: 10);

use Hampel\BinaryLane\Api\Laravel\Events\ActionBlocked;
use Hampel\BinaryLane\Api\Laravel\Events\ActionCompleted;
use Hampel\BinaryLane\Api\Laravel\Events\ActionFailed;
use Illuminate\Support\Facades\Event;

Event::listen(function (ActionCompleted $event) {
    // $event->action->resourceId is the server; $event->action->type is what was done to it
});

Event::listen(function (ActionFailed $event) {
    // $event->action->failureReason() when BinaryLane gave one - often it did not
});

Event::listen(function (ActionBlocked $event) {
    // answer with actions()->proceed(), or pay invoice $event->action->blockingInvoiceId,
    // then dispatch a new AwaitAction if the outcome still matters
});

use Hampel\BinaryLane\Api\Exception\NotAuthenticatedException;
use Hampel\BinaryLane\Api\Exception\NotFoundException;
use Hampel\BinaryLane\Api\Exception\ValidationException;

try {
    $server = BinaryLane::servers()->get($id);
} catch (NotFoundException $e) {
    // no such server on this account
} catch (NotAuthenticatedException $e) {
    // the token is no good. A configuration error, not an empty result.
}

use Illuminate\Support\Facades\Http;

Http::preventStrayRequests();

Http::fake([
    'api.binarylane.com.au/*' => Http::response([
        'server' => ['id' => 1234, 'name' => 'vps01.example.com'],
    ]),
]);

$server = BinaryLane::servers()->get(1234);

Http::assertSent(fn ($request) => $request->hasHeader('Authorization'));

$job = (new AwaitAction(5001))->withFakeQueueInteractions();

$job->handle(app(BinaryLaneManager::class), app('events'), app('log'));

$job->assertReleased(10);

use Hampel\BinaryLane\Api\Laravel\BinaryLaneServiceProvider;

$this->app->singleton(BinaryLaneServiceProvider::HTTP_CLIENT, fn () => $myPsr18Client);
bash
php artisan vendor:publish --tag=binarylane-config