Currency Converter - Auto Update FX Rates

Hi all,

I’m having ongoing issues with making HTTP requests in SugarCRM for a custom package that updates currency rates. Below is a summary of what I’ve tried and the problems I’ve encountered. I’m looking for advice on what’s permissible or alternative approaches within Sugar's environment.

I Created the module in Sugar and added 3 currencies: dollars, pounds and euro. Dollars is set to my base currency and ive entered incorrect values to the conversion rate field for euro and pounds hoping it will update when the scheduler runs but nothing changes.

Attempts Made:

  1. Initial Attempt with ExternalResourceClient:

    • I started with Sugar's built-in ExternalResourceClient for making HTTP requests. However, it wasn’t reliable, and data fetching failed intermittently.
  2. Switch to Guzzle HTTP Client:

    • I moved to Guzzle due to its robust HTTP handling capabilitie
    • Code:
      php
      use GuzzleHttp\Client; class CurrencyUpdater { private $client; public function __construct() { $this->client = new Client(['timeout' => 10.0]); } public function updateCurrencyRates() { $response = $this->client->request('GET', 'https://example.com/api'); // Additional code to process response } }
    • Issues Faced:
      • Errors from SugarCloud due to denylisted functions (fwrite, curl_*, etc.).
      • Despite attempts to restructure the package and adhere to Sugar policies, Guzzle files were consistently blocked.
  3. Switch Back to SugarHttpClient:

    • I reverted to using SugarHttpClient in hopes it would bypass the restrictions.
    • Code:
      php
      use Sugarcrm\Sugarcrm\Util\SugarHttpClient\SugarHttpClient; class CurrencyUpdater { private $client; public function __construct() { $this->client = new SugarHttpClient(); } public function updateCurrencyRates() { $response = $this->client->get('https://example.com/api', ['headers' => ['Accept' => 'application/json']]); // Additional code to process response } }
    • Issues Faced:
      • Still experiencing challenges in getting a consistent and reliable response from the API.

Summary of Challenges:

  • Encountered policy restrictions on SugarCloud that prevented using common HTTP request libraries.
  • Frequent issues with denylisted functions, blocked file types, and unauthorized autoload methods.
  • Adjustments to the package structure did not resolve the core issues, and error handling remains a significant challenge.

I’m looking for guidance on:

  • SugarCRM-approved methods for making HTTP requests.
  • Any way to configure Guzzle or another library within Sugar’s allowed parameters.
  • Recommendations on reliable HTTP client usage within Sugar’s constraints.

I have attached the files and folder structure I've been using.SugarClub_CurrencyConverterV1.zip

Thanks in advance for your help! Any suggestions or insights would be greatly appreciated.

Parents
  • Hi   it is weird that ExternalResourceClient is not working properly. It works for all requests like a charm and it is pretty easy to use!

    The only "trick" regarding it is that it ALWAYS resolve domains (identifies the target IP) and access the IP itself instead of the domain name. Depending uppon the target server, it may not support direct IP request.

    Perhaps this is the pain you are facing.

    Eventually you can share the target url you are accessing so we can help you somehow.

    Cheers

    André Lopes
    Lampada Global
    Skype: andre.lampada
  • Hi Andre.

    Thanks for your input here.

    The API URL im trying to access is https://v6.exchangerate-api.com

    This is the Entrypoint URL i am using in my scheduler to run the job: https://company.sugaropencloud.eu/index.php?entryPoint=CurrencyUpdater

    Cheers,

    Sam

  • Find below a working php script under SugarCRM architecture and sugarcrm.log with today data extracted from its.

    Cheers

    use Sugarcrm\Sugarcrm\Security\HttpClient\ExternalResourceClient as ERCExchangeRate;
    
    $req_url = 'https://v6.exchangerate-api.com/v6/<YOURKEY>/latest/USD';
    
    try {
        $response = (new ERCExchangeRate())->get($req_url);
    } catch (\Exception $e) {
        $GLOBALS['log']->fatal(__METHOD__ . ": Erro na requisição (" . $req_url . ") : " . $e->getMessage());
    
        throw new SugarApiExceptionError($e->getMessage());
    }
    
    if ($response->getStatusCode() == 200) {
        $result = json_decode($response->getBody(), true);
    
        $GLOBALS['log']->fatal("result : " . print_r($result, true));
    } else {
        $GLOBALS['log']->fatal("ERROR");
    }
    Thu Aug 29 15:09:16 2024 [108][1][FATAL] result : Array
    (
        [result] => success
        [documentation] => https://www.exchangerate-api.com/docs
        [terms_of_use] => https://www.exchangerate-api.com/terms
        [time_last_update_unix] => 1724889601
        [time_last_update_utc] => Thu, 29 Aug 2024 00:00:01 +0000
        [time_next_update_unix] => 1724976001
        [time_next_update_utc] => Fri, 30 Aug 2024 00:00:01 +0000
        [base_code] => USD
        [conversion_rates] => Array
            (
                [USD] => 1
                [AED] => 3.6725
                [AFN] => 70.7116
                [ALL] => 89.7991
                [AMD] => 387.8551
                [ANG] => 1.79
                [AOA] => 923.1974
                [ARS] => 950.08
                [AUD] => 1.4742
                [AWG] => 1.79
                [AZN] => 1.6991
                [BAM] => 1.7583
                [BBD] => 2
                [BDT] => 119.241
                [BGN] => 1.7584
                [BHD] => 0.376
                [BIF] => 2892.5617
                [BMD] => 1
                [BND] => 1.3034
                [BOB] => 6.9122
                [BRL] => 5.5273
                [BSD] => 1
                [BTN] => 83.9627
                [BWP] => 13.3001
                [BYN] => 3.2394
                [BZD] => 2
                [CAD] => 1.3468
                [CDF] => 2835.4587
                [CHF] => 0.8423
                [CLP] => 908.2984
                [CNY] => 7.1319
                [COP] => 4044.9443
                [CRC] => 525.5615
                [CUP] => 24
                [CVE] => 99.1306
                [CZK] => 22.5515
                [DJF] => 177.721
                [DKK] => 6.7064
                [DOP] => 59.5961
                [DZD] => 133.878
                [EGP] => 48.6498
                [ERN] => 15
                [ETB] => 111.1963
                [EUR] => 0.899
                [FJD] => 2.199
                [FKP] => 0.7578
                [FOK] => 6.7067
                [GBP] => 0.7578
                [GEL] => 2.6939
                [GGP] => 0.7578
                [GHS] => 15.698
                [GIP] => 0.7578
                [GMD] => 70.4722
                [GNF] => 8644.7278
                [GTQ] => 7.7295
                [GYD] => 209.0499
                [HKD] => 7.8003
                [HNL] => 24.7761
                [HRK] => 6.7737
                [HTG] => 131.7258
                [HUF] => 353.705
                [IDR] => 15447.6669
                [ILS] => 3.6631
                [IMP] => 0.7578
                [INR] => 83.9656
                [IQD] => 1308.4978
                [IRR] => 41959.1483
                [ISK] => 137.4891
                [JEP] => 0.7578
                [JMD] => 156.3715
                [JOD] => 0.709
                [JPY] => 144.4512
                [KES] => 128.6442
                [KGS] => 85.1511
                [KHR] => 4061.5542
                [KID] => 1.4742
                [KMF] => 442.2897
                [KRW] => 1335.5706
                [KWD] => 0.3052
                [KYD] => 0.8333
                [KZT] => 480.9841
                [LAK] => 22022.7868
                [LBP] => 89500
                [LKR] => 300.4642
                [LRD] => 195.1638
                [LSL] => 17.8139
                [LYD] => 4.7574
                [MAD] => 9.6549
                [MDL] => 17.3683
                [MGA] => 4542.2712
                [MKD] => 55.0977
                [MMK] => 2099.6507
                [MNT] => 3356.9325
                [MOP] => 8.0344
                [MRU] => 39.7015
                [MUR] => 46.1102
                [MVR] => 15.4456
                [MWK] => 1738.1261
                [MXN] => 19.6621
                [MYR] => 4.343
                [MZN] => 63.8936
                [NAD] => 17.8139
                [NGN] => 1587.4953
                [NIO] => 36.795
                [NOK] => 10.514
                [NPR] => 134.3404
                [NZD] => 1.6013
                [OMR] => 0.3845
                [PAB] => 1
                [PEN] => 3.7381
                [PGK] => 3.9292
                [PHP] => 56.2516
                [PKR] => 278.6557
                [PLN] => 3.8605
                [PYG] => 7606.0146
                [QAR] => 3.64
                [RON] => 4.4728
                [RSD] => 104.9927
                [RUB] => 91.51
                [RWF] => 1335.1896
                [SAR] => 3.75
                [SBD] => 8.4719
                [SCR] => 13.7291
                [SDG] => 543.7579
                [SEK] => 10.1981
                [SGD] => 1.3034
                [SHP] => 0.7578
                [SLE] => 22.6041
                [SLL] => 22604.0599
                [SOS] => 571.2454
                [SRD] => 28.9198
                [SSP] => 2854.9217
                [STN] => 22.026
                [SYP] => 13076.5056
                [SZL] => 17.8139
                [THB] => 34.0469
                [TJS] => 10.6254
                [TMT] => 3.4981
                [TND] => 3.0445
                [TOP] => 2.3085
                [TRY] => 34.1092
                [TTD] => 6.7635
                [TVD] => 1.4742
                [TWD] => 31.9159
                [TZS] => 2703.1824
                [UAH] => 41.2723
                [UGX] => 3716.6352
                [UYU] => 40.2544
                [UZS] => 12667.5992
                [VES] => 36.5919
                [VND] => 24858.2298
                [VUV] => 118.4004
                [WST] => 2.6947
                [XAF] => 589.7195
                [XCD] => 2.7
                [XDR] => 0.7419
                [XOF] => 589.7195
                [XPF] => 107.2821
                [YER] => 250.1681
                [ZAR] => 17.8194
                [ZMW] => 26.0213
                [ZWL] => 13.8156
            )
    
    )
    
    

    André Lopes
    Lampada Global
    Skype: andre.lampada
Reply
  • Find below a working php script under SugarCRM architecture and sugarcrm.log with today data extracted from its.

    Cheers

    use Sugarcrm\Sugarcrm\Security\HttpClient\ExternalResourceClient as ERCExchangeRate;
    
    $req_url = 'https://v6.exchangerate-api.com/v6/<YOURKEY>/latest/USD';
    
    try {
        $response = (new ERCExchangeRate())->get($req_url);
    } catch (\Exception $e) {
        $GLOBALS['log']->fatal(__METHOD__ . ": Erro na requisição (" . $req_url . ") : " . $e->getMessage());
    
        throw new SugarApiExceptionError($e->getMessage());
    }
    
    if ($response->getStatusCode() == 200) {
        $result = json_decode($response->getBody(), true);
    
        $GLOBALS['log']->fatal("result : " . print_r($result, true));
    } else {
        $GLOBALS['log']->fatal("ERROR");
    }
    Thu Aug 29 15:09:16 2024 [108][1][FATAL] result : Array
    (
        [result] => success
        [documentation] => https://www.exchangerate-api.com/docs
        [terms_of_use] => https://www.exchangerate-api.com/terms
        [time_last_update_unix] => 1724889601
        [time_last_update_utc] => Thu, 29 Aug 2024 00:00:01 +0000
        [time_next_update_unix] => 1724976001
        [time_next_update_utc] => Fri, 30 Aug 2024 00:00:01 +0000
        [base_code] => USD
        [conversion_rates] => Array
            (
                [USD] => 1
                [AED] => 3.6725
                [AFN] => 70.7116
                [ALL] => 89.7991
                [AMD] => 387.8551
                [ANG] => 1.79
                [AOA] => 923.1974
                [ARS] => 950.08
                [AUD] => 1.4742
                [AWG] => 1.79
                [AZN] => 1.6991
                [BAM] => 1.7583
                [BBD] => 2
                [BDT] => 119.241
                [BGN] => 1.7584
                [BHD] => 0.376
                [BIF] => 2892.5617
                [BMD] => 1
                [BND] => 1.3034
                [BOB] => 6.9122
                [BRL] => 5.5273
                [BSD] => 1
                [BTN] => 83.9627
                [BWP] => 13.3001
                [BYN] => 3.2394
                [BZD] => 2
                [CAD] => 1.3468
                [CDF] => 2835.4587
                [CHF] => 0.8423
                [CLP] => 908.2984
                [CNY] => 7.1319
                [COP] => 4044.9443
                [CRC] => 525.5615
                [CUP] => 24
                [CVE] => 99.1306
                [CZK] => 22.5515
                [DJF] => 177.721
                [DKK] => 6.7064
                [DOP] => 59.5961
                [DZD] => 133.878
                [EGP] => 48.6498
                [ERN] => 15
                [ETB] => 111.1963
                [EUR] => 0.899
                [FJD] => 2.199
                [FKP] => 0.7578
                [FOK] => 6.7067
                [GBP] => 0.7578
                [GEL] => 2.6939
                [GGP] => 0.7578
                [GHS] => 15.698
                [GIP] => 0.7578
                [GMD] => 70.4722
                [GNF] => 8644.7278
                [GTQ] => 7.7295
                [GYD] => 209.0499
                [HKD] => 7.8003
                [HNL] => 24.7761
                [HRK] => 6.7737
                [HTG] => 131.7258
                [HUF] => 353.705
                [IDR] => 15447.6669
                [ILS] => 3.6631
                [IMP] => 0.7578
                [INR] => 83.9656
                [IQD] => 1308.4978
                [IRR] => 41959.1483
                [ISK] => 137.4891
                [JEP] => 0.7578
                [JMD] => 156.3715
                [JOD] => 0.709
                [JPY] => 144.4512
                [KES] => 128.6442
                [KGS] => 85.1511
                [KHR] => 4061.5542
                [KID] => 1.4742
                [KMF] => 442.2897
                [KRW] => 1335.5706
                [KWD] => 0.3052
                [KYD] => 0.8333
                [KZT] => 480.9841
                [LAK] => 22022.7868
                [LBP] => 89500
                [LKR] => 300.4642
                [LRD] => 195.1638
                [LSL] => 17.8139
                [LYD] => 4.7574
                [MAD] => 9.6549
                [MDL] => 17.3683
                [MGA] => 4542.2712
                [MKD] => 55.0977
                [MMK] => 2099.6507
                [MNT] => 3356.9325
                [MOP] => 8.0344
                [MRU] => 39.7015
                [MUR] => 46.1102
                [MVR] => 15.4456
                [MWK] => 1738.1261
                [MXN] => 19.6621
                [MYR] => 4.343
                [MZN] => 63.8936
                [NAD] => 17.8139
                [NGN] => 1587.4953
                [NIO] => 36.795
                [NOK] => 10.514
                [NPR] => 134.3404
                [NZD] => 1.6013
                [OMR] => 0.3845
                [PAB] => 1
                [PEN] => 3.7381
                [PGK] => 3.9292
                [PHP] => 56.2516
                [PKR] => 278.6557
                [PLN] => 3.8605
                [PYG] => 7606.0146
                [QAR] => 3.64
                [RON] => 4.4728
                [RSD] => 104.9927
                [RUB] => 91.51
                [RWF] => 1335.1896
                [SAR] => 3.75
                [SBD] => 8.4719
                [SCR] => 13.7291
                [SDG] => 543.7579
                [SEK] => 10.1981
                [SGD] => 1.3034
                [SHP] => 0.7578
                [SLE] => 22.6041
                [SLL] => 22604.0599
                [SOS] => 571.2454
                [SRD] => 28.9198
                [SSP] => 2854.9217
                [STN] => 22.026
                [SYP] => 13076.5056
                [SZL] => 17.8139
                [THB] => 34.0469
                [TJS] => 10.6254
                [TMT] => 3.4981
                [TND] => 3.0445
                [TOP] => 2.3085
                [TRY] => 34.1092
                [TTD] => 6.7635
                [TVD] => 1.4742
                [TWD] => 31.9159
                [TZS] => 2703.1824
                [UAH] => 41.2723
                [UGX] => 3716.6352
                [UYU] => 40.2544
                [UZS] => 12667.5992
                [VES] => 36.5919
                [VND] => 24858.2298
                [VUV] => 118.4004
                [WST] => 2.6947
                [XAF] => 589.7195
                [XCD] => 2.7
                [XDR] => 0.7419
                [XOF] => 589.7195
                [XPF] => 107.2821
                [YER] => 250.1681
                [ZAR] => 17.8194
                [ZMW] => 26.0213
                [ZWL] => 13.8156
            )
    
    )
    
    

    André Lopes
    Lampada Global
    Skype: andre.lampada
Children
No Data