Skip to content

SuGDevelopers/connector

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

S&G Connect Package

This package is the basis for connections to OneApi or TruckApi.

Install

composer require sug/connect
php artisan vendor:publish --provider="Spatie\LaravelSettings\LaravelSettingsServiceProvider" --tag="migrations"
php artisan migrate

php artisan vendor:publish
// Search SugConnectServiceProvider and choose it

Clear Cache when necessary

php artisan optimize:clear
php artisan optimize

How to use?

Create a OneApiServer Instance class, example:

use Sug\Connect\OneApiServer;
use Illuminate\Http\Client\PendingRequest;
use GuzzleHttp\Promise\PromiseInterface;
use Illuminate\Http\Client\Response;

final class VDH extends OneApiServer
{
    /**
    * This function makes a POST call to the API endpoint,
    * and searches for the VIN of the vehicle.
    * 
    * @param PendingRequest $request
    * @return void
    */
    public function search(PendingRequest $request, string $vin):  PromiseInterface|Response
    {
        $content = json_encode(['vin' => $vin]);
    
        return $request
            ->asJson()
            ->acceptJson()
            ->withBody($content)
            ->post('v1/vehicle/information/enrich');
    }
    
    public function getBaseUrl()
    {
        return app($this->setting)->vdh_base_url;
    }

    protected function key(): string
    {
        return 'vdh_' . $this->supplier;
    }
}

In Controller

$vdh = VDH::make();
$response = $vdh->search(fin: $this->identifier);

if ($response->validate()) {
    // Vehicle found
    $data = $response->json(); 
}

Customize the Response

You can change the way the response is processed in the following way

use SearchResponse;

final class VDH extends OneApiServer
{
    public $responses = [
        'search' => SearchResponse::class,
    ];
}

Class SearchResponse

use ResponseBase;

final class SearchResponse extends ResponseBase
{
    /**
    * Response identifier, in this case the name of the Service.
    * @return string
    */
    protected function getResponseIdent(): string
    {
        return 'VDH';
    }

    /**
    * Message prefix in the Log
    * @return string
    */
    protected function getMessagePrefix(): string
    {
        return 'vehicle_search';
    }

    /**
    * Name of the Log
    * @return string
    */
    protected function getLogFilename(): string
    {
        return 'vdh_vehicle_search.log';
    }
    
    /**
    * Validation of Response 
    * @return bool
    */
    public function validate(): bool
    {
        //validation logic
    }
}

Use Channel Log

Log::channel('api')->error($this->json(), [
    'identifier' => Str::upper($this->getResponseIdent()),
    'filename' => $this->getResponseIdent(). '_' . Str::upper($this->getLogFilename()),
]);

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages