Field Manual Brokerage / Linux / Laravel
← Laravel

app/Services/ExchangeClient.php

Paste the complete file at this path. Names and paths are case-sensitive on Linux.

What it is

Complete brokerage practice file. The Assemble project page lists every file in order.

When to use it

Paste the complete file at this path. Names and paths are case-sensitive on Linux.

Syntax

mkdir -p 'app/Services'
# Paste the Example below into app/Services/ExchangeClient.php

Example

<?php

declare(strict_types=1);

namespace App\Services;

use App\Exceptions\ExchangeException;
use Illuminate\Http\Client\ConnectionException;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;

class ExchangeClient
{
    public function marketData(): array
    {
        if (config('services.exchange.demo')) {
            return json_decode(file_get_contents(resource_path('data/market.json')), true, flags: JSON_THROW_ON_ERROR);
        }

        return $this->request('GET', '/v1/market-data');
    }

    public function submitOrder(array $payload): array
    {
        if (config('services.exchange.demo')) {
            return [
                'exchange_order_id' => 'DEMO-'.$payload['client_order_id'],
                'client_order_id' => $payload['client_order_id'],
                'status' => 'accepted',
            ];
        }

        return $this->request('POST', '/v1/orders', $payload);
    }

    private function request(string $method, string $path, array $payload = []): array
    {
        try {
            $response = Http::withToken(config('services.exchange.api_key'))
                ->baseUrl(config('services.exchange.base_url'))
                ->acceptJson()
                ->asJson()
                ->timeout(5)
                ->connectTimeout(2)
                ->send($method, $path, $method === 'POST' ? ['json' => $payload] : []);
        } catch (ConnectionException $exception) {
            Log::warning('Exchange connection failed', ['path' => $path]);
            throw new ExchangeException('The exchange is temporarily unavailable.', 503);
        }

        if (!$response->successful()) {
            Log::warning('Exchange request failed', ['path' => $path, 'status' => $response->status()]);
            if ($method === 'POST' && $response->clientError()) {
                throw new ExchangeException('The exchange rejected the order. Please check your order details.', 422);
            }
            throw new ExchangeException('The exchange is temporarily unavailable.', 503);
        }

        $data = $response->json();
        $valid = is_array($data) && ($method === 'GET'
            ? isset($data['data']) && is_array($data['data'])
            : is_string($data['exchange_order_id'] ?? null) && ($data['status'] ?? null) === 'accepted');
        if (!$valid) {
            Log::warning('Exchange returned an unexpected response', ['path' => $path]);
            throw new ExchangeException('The exchange returned an unexpected response.', 503);
        }

        return $data;
    }
}

Common mistakes

Use the setup runbook first. Adapt provisional API assumptions to the supplied contract. Merge into supplied applications; do not duplicate the orders migration.

Related topics

API contract: change these points first