Feedback on ExternalResourceClient vs Guzzle

Hi Sugar Devs,

We'd like to collect some feedback on our Sugar ExternalResourceClient over Guzzle.

Are there functions or functionality that you use in Guzzle that aren't available in ExternalResourceClient?

Could you provide us with some examples that we can use to improve ERC?

Thank you very much,

Rafael Fernandes

Parents Reply Children
  • Hi  

    Yes, async.

    The use case is to send the multiple asynchronous requests and to not wait for the one request.

    https://docs.guzzlephp.org/en/stable/quickstart.html

    We have three HTTP requests, and currently using ExternalResourceClient and making requests one by one, so we call second and third request only once first one is finished. This leads to extra processing time.

    use GuzzleHttp\Client;
    use GuzzleHttp\Promise\Utils;
    
    $client = new Client(['base_uri' => 'https://api.example.com']);
    
    $requests = [
        'service_one'   => ['POST', '/service-one/data', ['param1' => 'value1', 'param2' => 'value2']],
        'service_two'   => ['POST', '/service-two/data', ['paramA' => 'valueA', 'paramB' => 'valueB']],
        'service_three' => ['POST', '/service-three/data', ['paramX' => 'valueX', 'paramY' => 'valueY']],
    ];
    
    $headers = ['Authorization' => 'Bearer YOUR_ACCESS_TOKEN']; // Example authorization header
    $promises = [];
    
    foreach ($requests as $key => [$method, $endpoint, $data]) {
        $cleanEndpoint = ltrim($endpoint, '/');
        $promises[$key] = $client->requestAsync(strtoupper($method), $cleanEndpoint, [
            'headers' => $headers,
            'json' => $data
        ]);
    }
    
    $responses = Utils::settle($promises)->wait();
    
    foreach ($responses as $key => $response) {
        if ($response['state'] === 'fulfilled') {
            echo "Response from $key: " . $response['value']->getBody() . PHP_EOL;
        } else {
            echo "Error in $key request: " . $response['reason']->getMessage() . PHP_EOL;
        }
    }

    We would like to do with ExternalResourceClient if that is possible. Please confirm.

    Also confirm, what's the plan for guzzle. Is it going to be depreciated or it will remain in vendor?

    What's the suggestion to achieve this?

    Thanks.