imamnc/efihub-client

Laravel client for EFIHUB Integration Service

Installs: 175

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 0

Forks: 0

pkg:composer/imamnc/efihub-client

v1.0.0 2025-06-16 04:21 UTC

This package is auto-updated.

Last update: 2025-10-15 07:28:53 UTC


README

EFIHUB

EFIHUB PHP/Laravel Client

A modern SDK to integrate with the EFIHUB platform using the OAuth 2.0 Client Credentials flow.

packagist version license author

Description

A modern SDK for integrating with the EFIHUB platform using the OAuth 2.0 Client Credentials flow. It provides simple HTTP helpers (GET/POST/PUT/DELETE), automatic token management, and native integration with the Laravel ecosystem.

EFIHUB is PT EFI’s centralized integration platform that connects multiple EFI applications into a single ecosystem. It offers:

  • API Sharing Platform: discoverable and secured APIs across internal apps
  • Central Webhook Hub: real-time notifications with routing, retries, and logging
  • Central Scheduler: task automation, cron jobs, and background processing
  • Enterprise Security: OAuth 2.0, JWT, and audit trails
  • Unified Dashboard: observability across APIs, webhooks, schedulers, and more

This package, imamnc/efihub-client, is the official PHP/Laravel client for EFIHUB’s REST API. It authenticates using the OAuth 2.0 Client Credentials flow and exposes simple HTTP helpers for GET, POST, PUT and DELETE.

Important: because the Client Credentials flow requires a client secret, this library must only be used in trusted server-side environments (backend). Keep your credentials in environment variables or a secure secrets manager and never expose them to browsers or public clients.

Features

  • ✅ OAuth 2.0 Client Credentials authentication
  • ✅ Automatic access token management & caching
  • ✅ Automatic refresh on 401 (expired token) with one retry
  • ✅ HTTP client wrapper based on Laravel Http responses
  • ✅ Facade and Service Provider (auto-discovery)
  • ✅ Environment-based configuration

Requirements

  • PHP ^8.0
  • Laravel ^8.0 | ^9.0 | ^10.0 | ^11.0 | ^12.0
  • Guzzle HTTP ^7.0

Installation

1) Install via Composer

composer require imamnc/efihub-client

2) Publish configuration

php artisan vendor:publish --provider="Efihub\EfihubServiceProvider" --tag=config

3) Environment variables

Add to your .env:

EFIHUB_CLIENT_ID=your_client_id
EFIHUB_CLIENT_SECRET=your_client_secret
EFIHUB_TOKEN_URL=https://efihub.morefurniture.id/oauth/token
EFIHUB_API_URL=https://efihub.morefurniture.id/api

Configuration

The config/efihub.php file (after publishing):

return [
    'client_id' => env('EFIHUB_CLIENT_ID'),
    'client_secret' => env('EFIHUB_CLIENT_SECRET'),
    'token_url' => env('EFIHUB_TOKEN_URL', 'https://efihub.morefurniture.id/oauth/token'),
    'api_base_url' => env('EFIHUB_API_URL', 'https://efihub.morefurniture.id/api'),
];

You can override the defaults via .env if you use a different EFIHUB endpoint.

Quick start

A minimal example using the Facade in a controller or service:

use Efihub\Facades\Efihub;

// Get list of users (GET) with query params
$response = Efihub::get('/user', ['page' => 1]);

if ($response->successful()) {
    $users = $response->json();
}

Usage

Laravel Facade

use Efihub\Facades\Efihub;

// GET with query params
$res = Efihub::get('/user', ['page' => 2, 'per_page' => 20]);

// POST JSON body
$res = Efihub::post('/orders', ['sku' => 'ABC', 'qty' => 2]);

// PUT JSON body
$res = Efihub::put('/orders/123', ['qty' => 3]);

// DELETE
$res = Efihub::delete('/orders/123');

Note: the second parameter will be sent as query parameters for GET requests or as the request body for POST/PUT requests following the Laravel HTTP client behavior.

Dependency Injection (Service/Controller)

use Efihub\EfihubClient;

class UserService
{
    public function __construct(private EfihubClient $efihub) {}

    public function getAll(): array
    {
        $res = $this->efihub->get('/user');
        return $res->successful() ? $res->json() : [];
    }
}

Response & error handling

All methods return Illuminate\Http\Client\Response:

$res = Efihub::get('/user/123');

if ($res->successful()) {
    $data = $res->json();
} elseif ($res->failed()) {
    // access error details from body/status
    logger()->error('EFIHUB error', [
        'status' => $res->status(),
        'body' => $res->json(),
    ]);
}

Authentication behavior

  • Access tokens are obtained via Client Credentials and cached (approx. 55 minutes)
  • If a request returns 401, the token is cleared, refreshed, and the request is retried once automatically

API

All methods live on Efihub\\EfihubClient and are also available via the Efihub Facade.

  • get(string $endpoint, array $options = []) : Response
  • post(string $endpoint, array $options = []) : Response
  • put(string $endpoint, array $options = []) : Response
  • delete(string $endpoint, array $options = []) : Response
  • request(string $method, string $endpoint, array $options = []) : Response
  • getAccessToken() : string — returns the cached access token

Return type: Illuminate\\Http\\Client\\Response.

Testing

You can fake HTTP requests for testing:

use Illuminate\Support\Facades\Http;

Http::fake([
    'efihub.morefurniture.id/oauth/token' => Http::response([
        'access_token' => 'fake-token',
        'expires_in' => 3600,
    ], 200),
    'efihub.morefurniture.id/api/*' => Http::response([
        'data' => ['users' => []],
    ], 200),
]);

Security notes

  • Do not use this library in browser/public clients. It is intended for trusted server-side environments only.
  • Store credentials in environment variables or a secure secrets manager.

Contributing

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

License

MIT © Imam Nurcholis. See the LICENSE file for details.

Author & links