Skip to content

Conversation

Hailong
Copy link
Member

@Hailong Hailong commented Apr 23, 2019

Description (*)

Free Currency Converter API now requires API key, this change is to add the API key setting to backend and append the key to request url.

Fixed Issues (if relevant)

#22468

Manual testing scenarios (*)

  1. Navigate to Stores -> Configuration -> General -> Currency Setup
  2. Make sure the new API Key field is added for Currency Converter API.
  3. Navigate to Stores -> Currency Rates
  4. Select Currency Converter API as Import Service and click Import.
  5. Make sure the rated value(s) are refreshed.

Contribution checklist (*)

  • Pull request has a meaningful description of its purpose
  • All commits are accompanied by meaningful commit messages
  • All new or changed code is covered with unit/integration tests (if applicable)
  • All automated tests passed successfully (all builds on Travis CI are green)

@m2-assistant
Copy link

m2-assistant bot commented Apr 23, 2019

Hi @Hailong. Thank you for your contribution
Here is some useful tips how you can test your changes using Magento test environment.
Add the comment under your pull request to deploy test or vanilla Magento instance:

  • @magento-engcom-team give me test instance - deploy test instance based on PR changes
  • @magento-engcom-team give me 2.3-develop instance - deploy vanilla Magento instance

For more details, please, review the Magento Contributor Assistant documentation

* @param array $currenciesTo
* @return array
*/
protected function _makeEmptyResponse(array $currenciesTo): array
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method was originally in FixerIo.php, moved it into this base class so that it can be reused by CurrencyConverterApi. But I just see the AbstractImport class is marked by @api annotation, so probably I can not add any new method in it. I can remove it if needed.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Hailong yep, please let this method be private as it was initially.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just reverted the changes in AbstractImport and FixerIo, but cloned makeEmptyResponse method in CurrencyConverterApi class.

@@ -12,7 +12,7 @@ class CurrencyConverterApi extends AbstractImport
/**
* @var string
*/
const CURRENCY_CONVERTER_URL = 'http://free.currencyconverterapi.com/api/v3/convert?q={{CURRENCY_FROM}}_{{CURRENCY_TO}}&compact=ultra'; //@codingStandardsIgnoreLine
const CURRENCY_CONVERTER_URL = 'http://free.currencyconverterapi.com/api/v6/convert?q={{CURRENCY_FROM}}_{{CURRENCY_TO}}&compact=ultra&apiKey={{API_KEY}}'; //@codingStandardsIgnoreLine
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happened to V3 API?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both v3 and v6 require API Key, but since the latest version is v6, I changed it to v6.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then I suppose some functionality is broken, please report steps to reproduce as an issue and mention it in description.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me add the steps in description now.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@orlangur I just added the steps to reproduce the original error in description. Thanks!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Hailong please report an issue separately in https://github.com/magento/magento2/issues and then add it here to description

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@ghost ghost assigned orlangur Apr 23, 2019
@Hailong Hailong force-pushed the add-api-key-for-free-currency-converter-api branch from 8164bc9 to 7baa877 Compare April 23, 2019 13:26
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
);
if (empty($apiKey)) {
$this->_messages[] = __('No API Key was specified or an invalid API Key was specified.');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see how it can be not empty here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If user gives an API Key from Currency Setup of backend, $apiKey would be not empty. Otherwise, it would be empty.

@@ -47,7 +47,12 @@
</group>
<group id="currencyconverterapi" translate="label" sortOrder="45" showInDefault="1" showInWebsite="0" showInStore="0">
<label>Currency Converter API</label>
<field id="timeout" translate="label" type="text" sortOrder="0" showInDefault="1" showInWebsite="0" showInStore="0">
<field id="api_key" translate="label" type="obscure" sortOrder="5" showInDefault="1" showInWebsite="0" showInStore="0">
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add validation as this field seems to be mandatory.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

User may input the API key for either Fixer.io or Currency Converter API on the backend, so not mandatory seems more user friendly.

);
if (empty($apiKey)) {
$this->_messages[] = __('No API Key was specified or an invalid API Key was specified.');
$data[$currencyFrom] = $this->makeEmptyResponse($currenciesTo);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Returning such response is not a option, please throw some error instead.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me see how to throw a proper error from there, and change it accordingly.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@orlangur by reviewing the code again, I found setting the _messages[] array here seems a good idea. Because the caller will check the _messages[] array and report error to users. For this part I actually cloned the code from the FixerIo class.

Here is a screenshot of the warning message: https://pasteboard.co/IbuUilQ.png

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, good to know. Still, we should not produce an empty response.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with you. Let me check what kind of response is better for this case later today, and refine the code properly.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@orlangur I have changed it to throw a LocalizedException, in this way there would be a red error message shown in the UI when the key is empty. Screenshot

@Hailong Hailong force-pushed the add-api-key-for-free-currency-converter-api branch 2 times, most recently from 314c0f6 to 1cbf202 Compare April 26, 2019 00:20
@Hailong Hailong force-pushed the add-api-key-for-free-currency-converter-api branch from 1cbf202 to 10518b7 Compare April 26, 2019 00:36
@Hailong Hailong requested a review from orlangur April 27, 2019 11:41
@magento-engcom-team
Copy link
Contributor

Hi @orlangur, thank you for the review.
ENGCOM-4835 has been created to process this Pull Request

@VasylShvorak
Copy link
Contributor

✔️ QA passed

@sivaschenko
Copy link
Member

@magento run all tests

Copy link
Contributor

@orlangur orlangur left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ysapiga please squash all build-fixing commits into one.

@ysapiga ysapiga force-pushed the add-api-key-for-free-currency-converter-api branch from 1976e27 to 12f6b88 Compare June 11, 2019 09:51
@ysapiga
Copy link
Contributor

ysapiga commented Jun 11, 2019

@orlangur please review.

@sidolov
Copy link
Contributor

sidolov commented Aug 16, 2019

Hi @Hailong , looks like the same changes were merged in the scope of PR: #24008
I'm closing this PR as not relevant, I'm sorry for the inconvenience.

@sidolov sidolov closed this Aug 16, 2019
@m2-assistant
Copy link

m2-assistant bot commented Aug 16, 2019

Hi @Hailong, thank you for your contribution!
Please, complete Contribution Survey, it will take less than a minute.
Your feedback will help us to improve contribution process.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants