如何在 Laravel 中将数据从一个 class 传输到另一个 class?
How to transfer data from one class to another class in Laravel?
目前我正在授权 Amadeus API 并通过服务 class 接收访问令牌,即 Amadeus\Client
。多个函数有多个 API 端点,因此我想为单独的端点维护单独的 classes。每个端点都需要访问令牌来处理请求。如何将授权令牌从 Amadeus\Client
传输到 Amadeus\FlightOffersSearch
,以便我可以将访问令牌传递到端点的 headers。有人可以帮我吗?
Amadeus\Client
class Client
{
public function __construct(
protected string $uri,
protected string $client_id,
protected string $client_secret,
protected string $grant_type,
) {}
public function authorization() {
$uri = $this->uri;
$auth_data = array(
'client_id' => $this->client_id,
'client_secret' => $this->client_secret,
'grant_type' => $this->grant_type
);
$requests_response = Http::asForm()->post($uri, $auth_data);
$response_body = json_decode($requests_response->body());
$access_token = $response_body->access_token;
return $access_token;
}
Amadeus\FlightOffersSearch
class Flight
{
public function get_flight() {
if(isset($access_token)){
$endpoint = 'https://test.api.amadeus.com/v2/shopping/flight-offers';
$travel_data = array(
'originLocationCode' => 'BOS',
'destinationLocationCode' => 'PAR',
'departureDate' => '2022-06-14',
'adults' => 2
);
$params = http_build_query($travel_data);
$url = $endpoint.'?'.$params;
$headers = array('Authorization' => 'Bearer '.$access_token);
}
}
}
这比 Laravel 更 PHP 具体。我建议您将客户端设为单例 class,在其中封装所有身份验证逻辑,并在需要时使用静态方法获取客户端实例。
我还强烈建议使用 GuzzleHttp 作为 HTTP 客户端。
类似于这个草稿。
<?php
namespace App;
use GuzzleHttp\Client;
class AmadeusClient
{
private static $instance;
private $client;
private $token;
public function __construct()
{
throw_if(static::$instance, 'There should be only one instance of this class');
static::$instance = $this;
$this->client = new Client([
'base_uri' => 'https://test.api.amadeus.com/v2/',
]);
}
private function authenticate()
{
if (!$this->token) {
$client_id = env('api_client_id');
$client_secret = env('api_client_secret');
$grant_type = env('api_grant_type$a');
$this->token = ''; // TODO: get authorization token using $this->client;
}
}
public static function getInstance()
{
return static::$instance ?: (new static());
}
public function get($uri, array $options = [])
{
$this->authenticate();
return $this->client->get($uri, $options);
}
public function post($uri, array $options = [])
{
$this->authenticate();
return $this->client->post($uri, $options);
}
}
然后你会像这样使用它:
$client = AmadeusClient::getInstance();
$client->get('shopping/flight-offers', $options);
目前我正在授权 Amadeus API 并通过服务 class 接收访问令牌,即 Amadeus\Client
。多个函数有多个 API 端点,因此我想为单独的端点维护单独的 classes。每个端点都需要访问令牌来处理请求。如何将授权令牌从 Amadeus\Client
传输到 Amadeus\FlightOffersSearch
,以便我可以将访问令牌传递到端点的 headers。有人可以帮我吗?
Amadeus\Client
class Client
{
public function __construct(
protected string $uri,
protected string $client_id,
protected string $client_secret,
protected string $grant_type,
) {}
public function authorization() {
$uri = $this->uri;
$auth_data = array(
'client_id' => $this->client_id,
'client_secret' => $this->client_secret,
'grant_type' => $this->grant_type
);
$requests_response = Http::asForm()->post($uri, $auth_data);
$response_body = json_decode($requests_response->body());
$access_token = $response_body->access_token;
return $access_token;
}
Amadeus\FlightOffersSearch
class Flight
{
public function get_flight() {
if(isset($access_token)){
$endpoint = 'https://test.api.amadeus.com/v2/shopping/flight-offers';
$travel_data = array(
'originLocationCode' => 'BOS',
'destinationLocationCode' => 'PAR',
'departureDate' => '2022-06-14',
'adults' => 2
);
$params = http_build_query($travel_data);
$url = $endpoint.'?'.$params;
$headers = array('Authorization' => 'Bearer '.$access_token);
}
}
}
这比 Laravel 更 PHP 具体。我建议您将客户端设为单例 class,在其中封装所有身份验证逻辑,并在需要时使用静态方法获取客户端实例。
我还强烈建议使用 GuzzleHttp 作为 HTTP 客户端。
类似于这个草稿。
<?php
namespace App;
use GuzzleHttp\Client;
class AmadeusClient
{
private static $instance;
private $client;
private $token;
public function __construct()
{
throw_if(static::$instance, 'There should be only one instance of this class');
static::$instance = $this;
$this->client = new Client([
'base_uri' => 'https://test.api.amadeus.com/v2/',
]);
}
private function authenticate()
{
if (!$this->token) {
$client_id = env('api_client_id');
$client_secret = env('api_client_secret');
$grant_type = env('api_grant_type$a');
$this->token = ''; // TODO: get authorization token using $this->client;
}
}
public static function getInstance()
{
return static::$instance ?: (new static());
}
public function get($uri, array $options = [])
{
$this->authenticate();
return $this->client->get($uri, $options);
}
public function post($uri, array $options = [])
{
$this->authenticate();
return $this->client->post($uri, $options);
}
}
然后你会像这样使用它:
$client = AmadeusClient::getInstance();
$client->get('shopping/flight-offers', $options);