move to HttpClient

This commit is contained in:
O K
2025-12-07 23:10:14 +02:00
parent eb8c9fe18b
commit 428e24f961
3 changed files with 106 additions and 66 deletions

View File

@@ -1,5 +1,10 @@
<?php
use Symfony\Component\HttpClient\HttpClient;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
class UspsV3Client
{
private $token;
@@ -10,15 +15,14 @@ class UspsV3Client
{
$this->token = $token;
$this->isLive = $isLive;
// URLs from the OpenAPI spec
$this->baseUrl = $this->isLive
? 'https://apis.usps.com/prices/v3'
// Base URLs per OpenAPI Spec
$this->baseUrl = $this->isLive
? 'https://apis.usps.com/prices/v3'
: 'https://apis-tem.usps.com/prices/v3';
}
/**
* Call Domestic Prices v3 API
* Endpoint: /base-rates/search
* Get Domestic Rate (Rates v3)
*/
public function getDomesticRate($payload)
{
@@ -26,56 +30,65 @@ class UspsV3Client
}
/**
* Call International Prices v3 API
* Endpoint: /base-rates/search (Under International Base path)
* Note: The spec shows a different base URL for International
* Get International Rate (Rates v3)
*/
public function getInternationalRate($payload)
{
// International uses a different base URL structure in the spec
$intlBaseUrl = $this->isLive
? 'https://apis.usps.com/international-prices/v3'
// International endpoint uses a different base structure per spec
$intlBaseUrl = $this->isLive
? 'https://apis.usps.com/international-prices/v3'
: 'https://apis-tem.usps.com/international-prices/v3';
return $this->post('/base-rates/search', $payload, $intlBaseUrl);
}
/**
* Internal POST logic using Symfony HTTP Client
*/
private function post($endpoint, $payload, $overrideUrl = null)
{
$url = ($overrideUrl ? $overrideUrl : $this->baseUrl) . $endpoint;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($payload));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Authorization: Bearer ' . $this->token,
'Content-Type: application/json',
'Accept: application/json'
$client = HttpClient::create([
'timeout' => 15,
'verify_peer' => false,
'verify_host' => false,
]);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
try {
$response = $client->request('POST', $url, [
'headers' => [
'Authorization' => 'Bearer ' . $this->token,
'Content-Type' => 'application/json',
'Accept' => 'application/json'
],
'json' => $payload
]);
if (curl_errno($ch)) {
$error = curl_error($ch);
// toArray(false) prevents exception on 4xx/5xx responses so we can parse the error body
$data = $response->toArray(false);
$statusCode = $response->getStatusCode();
return ['error' => 'CURL Error: ' . $error];
}
// Handle API Errors (400 Bad Request, 401 Unauthorized, etc)
if ($statusCode >= 400) {
$msg = isset($data['error']['message']) ? $data['error']['message'] : 'Unknown Error';
// Try to extract deeper error detail (e.g., from 'errors' array)
if (isset($data['error']['errors'][0]['detail'])) {
$msg .= ' - ' . $data['error']['errors'][0]['detail'];
} elseif (isset($data['error']['code'])) {
$msg .= ' (' . $data['error']['code'] . ')';
}
$data = json_decode($response, true);
// Check for HTTP errors (400, 401, 403, etc)
if ($httpCode >= 400) {
$msg = isset($data['error']['message']) ? $data['error']['message'] : 'Unknown Error';
if (isset($data['error']['errors'][0]['detail'])) {
$msg .= ' - ' . $data['error']['errors'][0]['detail'];
return ['error' => "API HTTP $statusCode: $msg"];
}
return ['error' => "HTTP $httpCode: $msg"];
}
return $data;
return $data;
} catch (TransportExceptionInterface $e) {
return ['error' => 'Network/Transport Error: ' . $e->getMessage()];
} catch (\Exception $e) {
return ['error' => 'Client Error: ' . $e->getMessage()];
}
}
}
}