Drupal 8 - Consume an ASMX SOAP service (Exchange rate | BCCR) - Version 2

By kenneth, Tue, 05/19/2020 - 09:43

Authored on

Image
bccr-official-page

The last year, I wrote about how to consume a ASMX SOAP service to be precise I am talking about the Banco Central de Costa Rica webservice to grab the exchange rate (Official documentation: https://www.bccr.fi.cr/seccion-indicadores-economicos/servicio-web/gu%C3%ADa-de-uso). 

It looks like they now changed their services, so I am writing this up to be explain how consume it now. I updated the repository with the new version here: https://github.com/kenneth-bolivar-castro/exchange-rate 

The custom module codebase is almost the same one, so for sake of this article I won't explain everything again, nevertheless I will mention the main changes I made in order to make it work once again! In order to do that we gonna follow along the commit I made to fix it: https://github.com/kenneth-bolivar-castro/exchange-rate/commit/918bfefb5bd5f61e5f128320e45ef5e7a1d04b0f

Files changes

On info YML file: exchange_rate.info.yml  what I included was the compatibility with Drupal 9 plus I added a version definition.

The service now requires to you to register up in order to have access here, after that they will provide you a token to consume the service. So, I decided to create a settings form to store all those credentials. I've also storing the URL to consume the service (The last version I hardcoded it). 

bccr-webservice

That being said, I included a new link on admin menu exchange_rate.links.menu.yml to expose the new settings form.

admin menu

Of course, I created the new route for the new settings form here exchange_rate.routing.yml as well.

settings-form

By using this form the module is able to store the endpoint, email and token in order to consume the service. Now those are settings the exchange_rate.services.yml needs some updates in order to inject the configuration as dependency.

Another thing that I noticed there was not feedback when the webservice was not available, so I made few changes on the controller src/Controller/ExchangeRateController.php too.

The major changes were into src/ExchangeRateManager.php where two new properties was included:

  • $config It will have the configuration `exchange_rate.settings`.
  • $siteName It will have the system site name.

The parameters required by the webservice are named in a different way now on the top of that the method to consume the service is now by POST method.

At last but not least, I create the settings form class src/Form/ExchangeRateSettingsForm.php per se.

Easy peasy, isn't it? Well, it works again. The demo is still available:

demo-exchange-rate

https://www.keboca.com/exchange-rate/bccr


I hope it helps somebody somehow!

Happy coding!

Comments

Restricted HTML

  • Allowed HTML tags: <a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id>
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.