33namespace Perfbase \SDK \Http ;
44
55use GuzzleHttp \Client as GuzzleClient ;
6- use GuzzleHttp \Promise \PromiseInterface ;
7- use GuzzleHttp \Promise \Utils ;
86use JsonException ;
97use Perfbase \SDK \Config ;
108use Perfbase \SDK \Exception \PerfbaseApiKeyMissingException ;
@@ -30,12 +28,6 @@ class ApiClient
3028 */
3129 private GuzzleClient $ httpClient ;
3230
33- /**
34- * Promises to settle before the client is destroyed
35- * @var array<PromiseInterface>
36- */
37- private array $ promises = [];
38-
3931 /**
4032 * @throws PerfbaseApiKeyMissingException
4133 */
@@ -46,24 +38,25 @@ public function __construct(Config $config)
4638 }
4739
4840 $ this ->config = $ config ;
49-
5041 $ this ->defaultHeaders = [
5142 'Authorization ' => 'Bearer ' . $ this ->config ->api_key ,
5243 'Accept ' => 'application/json ' ,
5344 'User-Agent ' => 'Perfbase-PHP-SDK/1.0 ' ,
5445 'Content-Type ' => 'application/json ' ,
46+ 'Connection ' => 'keep-alive ' ,
5547 ];
5648
5749 /** @var array<string, mixed> $httpClientConfig */
5850 $ httpClientConfig = [];
59-
6051 $ httpClientConfig ['base_uri ' ] = $ config ->api_url ;
6152 $ httpClientConfig ['timeout ' ] = $ config ->timeout ;
6253
54+ // Set up proxy if configured
6355 if ($ config ->proxy ) {
6456 $ httpClientConfig ['proxy ' ] = $ config ->proxy ;
6557 }
6658
59+ // Set up the HTTP client
6760 $ this ->httpClient = new GuzzleClient ($ httpClientConfig );
6861 }
6962
@@ -72,12 +65,10 @@ public function __construct(Config $config)
7265 *
7366 * @param string $endpoint API endpoint to send the request to
7467 * @param array<mixed> $data Data to send in the request body
75- * @param bool $async If true, send asynchronously; if false, wait for response
76- *
77- * @return string|null Response data from the API, or null if non-blocking
78- * @throws JsonException When the HTTP request fails or returns an error
68+ * @return void
69+ * @throws JsonException
7970 */
80- private function post (string $ endpoint , array $ data, bool $ async = true ): ? string
71+ private function submit (string $ endpoint , array $ data ): void
8172 {
8273 // Prepare request options
8374 $ options = [
@@ -86,38 +77,23 @@ private function post(string $endpoint, array $data, bool $async = true): ?strin
8677 ];
8778
8879 try {
89- if ($ async ) {
90- $ this ->promises [] = $ this ->httpClient ->postAsync ($ endpoint , $ options );
91- return null ;
92- } else {
93- $ response = $ this ->httpClient ->post ($ endpoint , $ options );
94- return (string )$ response ->getBody ();
95- }
80+ $ this ->httpClient ->post ($ endpoint , $ options );
9681 } catch (Throwable $ e ) {
9782 // throw new PerfbaseException('HTTP Request failed: ' . $e->getMessage());
9883 }
99- return null ;
10084 }
10185
10286 /**
10387 * Submits a trace to the Perfbase API
10488 *
10589 * @param array<mixed> $data Data to send in the request body
106- * @param bool $async If true, send asynchronously; if false, wait for response
90+ * @return void
10791 *
108- * @return string|null Response data from the API, or null if non-blocking
10992 * @throws JsonException When the HTTP request fails or returns an error
11093 */
111- public function submitTrace (array $ data, bool $ async = true ): ? string
94+ public function submitTrace (array $ data ): void
11295 {
113- return $ this ->post ('/v1/submit ' , $ data, $ async );
96+ $ this ->submit ('/v1/submit ' , $ data );
11497 }
11598
116- public function __destruct ()
117- {
118- // Attempt to settle all outstanding async HTTP promises without blocking
119- if (!empty ($ this ->promises )) {
120- Utils::settle ($ this ->promises )->wait (false );
121- }
122- }
12399}
0 commit comments