Real Time Exchange Rate with Real Time Data Using Yahoo Finance API

In the earlier posts Google Map API in SAP and GPS like tool in SAP we saw how we can leverage the Google Map APIs and consume them in SAP. In this post, we extend the usage of similar APIs to track the exchange rate real time. By the real time we mean, it would be refreshed automatically at a fixed interval of time which we set and it would show the actual rate at that fraction of the time. Why always Google, this time we would use Yahoo’s API.

Yahoo Finance API: http://finance.yahoo.com/webservice/v1/symbols/allcurrencies/quote

This, Yahoo’s Finance API has the below structure.

We need to consume it in SAP to display the exchange rate real time and also refresh it automatically without the need to hit the REFRESH button on the screen. Won’t it be cool, if you are in a Finance Organization and you project the exchange rate or stock value on a huge wall from SAP and it gets refreshed real time for every passerby to view?

Let us see what our Application can do.

Provide the time interval at which you want the results to be refreshed.

The output is displayed for a particular time.

Now, it is refreshed automatically. The message shows the time at which the data was refreshed.

Please note, there is no refresh button on the screen. Our Application handles the refresh.

There is no rocket science. Class Method CL_GUI_ALV_GRID->REFRESH_TABLE_DISPLAY is doing our job.

A special mention of the Class/Interface CL_GUI_TIMER and Event FINISHED is also needed.

Check the METHOD TIMER_EVENT in the Class CL_AUTO in the code.

METHODS :display_report,
* Define event handling method for event FINISHED of class CL_GUI_TIMER.
timer_event FOR EVENT finished OF cl_gui_timer.

Conceptually, what is happening?

Event FINISHED of class CL_GUI_TIMER is raised after the Timer has waited for the Interval provided in the selection screen. A listener method for FINISHED event is created and the REFRESH logic is implemented.

Another important step is to call the RUN method of CL_GUI_TIMER after the REFRESH to activate the time again.

METHOD timer_event .
 
DATA: lv_time TYPE char10.
 
* Get Data
me->get_data( ) .
 
IF me->ob_grid IS INITIAL .
 
CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR'
IMPORTING
e_grid = ob_grid.
ELSE.
 
* Refresh the ALV
CALL METHOD ob_grid->refresh_table_display.
 
ENDIF.
 
WRITE sy-uzeit TO lv_time USING EDIT MASK '__:__:__'.
 
CONCATENATE 'Screen refreshed at' lv_time INTO DATA(lv_msg) SEPARATED BY space.
 
MESSAGE lv_msg TYPE 'S' . " Get the cuuent timE as message
 
* Call RUN method of CL_GUI_TIMER again to activate timer
me->ob_timer->run( ) .
 
ENDMETHOD.

This article is an outcome of research done to find a way to track and trace the vehicle of service providers like Ola or Uber etc. The idea was to make use of the interface/program which we created earlier where we consumed Google Map APIs. But unfortunately, we could not figure out any free APIs which would return the exact position of the vehicles real time. But we ended up finding this API which returned real time stock and exchange rate.

Therefore, we have not described them again here. If you have confusion, please refer to our earlier articles or just put breakpoints in the code and start debugging. The logic is straightforward.

We have defined a structure ‘ZAUTO_EXC’ for the below application. You might need this as well.

The complete working code snippet for this topic. Please replicate it in your SAP environment and have fun.

**---------------------------------------------------------------------*
** Date : 12/11/2016 *
** Author : SAPSPOT *
** Title : Auto Refresh the Screen Output *
**---------------------------------------------------------------------*
REPORT zsapspot MESSAGE-ID zma.
 
*&---------------------------------------------------------------------*
*& CLASS DEFINATION FOR ADDTING AUTO REFRESH
*&---------------------------------------------------------------------*
CLASS cl_auto DEFINITION .
 
PUBLIC SECTION .
*&---------------------------------------------------------------------*
*& TYPE-POOLS
*&---------------------------------------------------------------------*
TYPE-POOLS: truxs, slis.
 
DATA : ob_timer TYPE REF TO cl_gui_timer .
 
METHODS :display_report,
* Define event handling method for event FINISHED of class CL_GUI_TIMER.
timer_event FOR EVENT finished OF cl_gui_timer.
PRIVATE SECTION .
 
*&---------------------------------------------------------------------*
*& Types and Data
*&---------------------------------------------------------------------*
TYPES: BEGIN OF ty_dest,
time_date TYPE zauto_exc-time_date,
ex_type TYPE zauto_exc-ex_type,
rate TYPE zauto_exc-rate,
time_stamp TYPE zauto_exc-time_stamp,
END OF ty_dest.
 
DATA: gt_dest TYPE STANDARD TABLE OF ty_dest.
DATA : ob_grid TYPE REF TO cl_gui_alv_grid . "DISPLAY DATA
DATA : gt_dest1 TYPE STANDARD TABLE OF ty_dest.
 
*&---------------------------------------------------------------------*
*& Methods
*&---------------------------------------------------------------------*
METHODS :
get_data,
 
create_http_client IMPORTING ip_url TYPE string
EXPORTING ex_http_client TYPE REF TO if_http_client,
 
http_client_request_get_method
IMPORTING ip_http_client TYPE REF TO if_http_client,
 
http_client_send IMPORTING ip_http_client TYPE REF TO if_http_client,
 
http_client_receive IMPORTING ip_http_client TYPE REF TO if_http_client
EXPORTING ex_content TYPE string,
 
get_data_ex IMPORTING ip_content TYPE string.
 
ENDCLASS.
 
CLASS cl_auto IMPLEMENTATION.
 
* Method 1
METHOD create_http_client.
 
* Get client from url
CALL METHOD cl_http_client=>create_by_url
EXPORTING
url = ip_url " 'http://finance.yahoo.com/webservice/v1/symbols/allcurrencies/quote'.
IMPORTING
client = ex_http_client
EXCEPTIONS
argument_not_found = 1
plugin_not_active = 2
internal_error = 3
OTHERS = 4.
 
ENDMETHOD.
 
* Method 2
METHOD http_client_request_get_method.
* Request and Get
ip_http_client->request->set_header_field( name = '~request_method' value = 'GET' ).
ENDMETHOD.
 
* Method 3
METHOD http_client_send.
* Send the request
ip_http_client->send( ).
ENDMETHOD.
 
* Method 4
METHOD http_client_receive.
* Reterive the result
CALL METHOD ip_http_client->receive
EXCEPTIONS
http_communication_failure = 1
http_invalid_state = 2
http_processing_failed = 3
OTHERS = 4.
ex_content = ip_http_client->response->get_cdata( ).
 
ENDMETHOD.
 
* Method 5
METHOD get_data_ex.
 
* Local data declaration
DATA: lv_url TYPE c LENGTH 255,
ls_dest TYPE ty_dest,
moff TYPE syst-tabix,
moff1 TYPE syst-tabix,
lv_len TYPE syst-tabix,
lv_ex_type TYPE c LENGTH 20,
lv_rate TYPE c LENGTH 20,
lv_time_date TYPE c LENGTH 40.
 
*&---------------------------------------------------------------------*
*& Find exchange
*&---------------------------------------------------------------------*
DO .
* Find <location> text in the content string
FIND '<field name="name">' IN SECTION OFFSET moff OF ip_content IGNORING CASE MATCH OFFSET moff .
 
IF sy-subrc = 0 .
 
moff = moff + 19 .
 
FIND '</field>' IN SECTION OFFSET moff OF ip_content IGNORING CASE MATCH OFFSET moff1 .
 
lv_len = moff1 - moff .
 
lv_ex_type = ip_content+moff(lv_len) .
 
ls_dest-ex_type = lv_ex_type.
*--------------------------------------------------------------------*
* ---------------Find rate
*--------------------------------------------------------------------*
FIND '<field name="price">' IN SECTION OFFSET moff OF ip_content IGNORING CASE MATCH OFFSET moff .
 
IF sy-subrc = 0 .
 
moff = moff + 20 .
 
FIND '</field>' IN SECTION OFFSET moff OF ip_content IGNORING CASE MATCH OFFSET moff1 .
 
lv_len = moff1 - moff .
 
lv_rate = ip_content+moff(lv_len) .
 
ls_dest-rate = lv_rate.
 
ENDIF.
 
*==============================================================
* Find date and time stamp from api
*==============================================================
 
FIND '<field name="utctime">' IN SECTION OFFSET moff OF ip_content IGNORING CASE MATCH OFFSET moff .
IF sy-subrc = 0 .
 
moff = moff + 22 .
FIND '</field>' IN SECTION OFFSET moff OF ip_content IGNORING CASE MATCH OFFSET moff1 .
lv_len = moff1 - moff .
lv_time_date = ip_content+moff(lv_len) .
ls_dest-time_date = lv_time_date.
 
ENDIF.
 
* GET TIME form system for verfication .
ls_dest-time_stamp = sy-uzeit.
 
APPEND ls_dest TO gt_dest.
 
ELSE.
 
EXIT.
 
ENDIF.
 
ENDDO .
 
ENDMETHOD.
 
METHOD get_data .
 
CLEAR: gt_dest.
 
DATA: lv_http_client TYPE REF TO if_http_client,
lv_content TYPE string,
lv_url TYPE string.
 
* Prepare the url of the address
lv_url = 'http://finance.yahoo.com/webservice/v1/symbols/allcurrencies/quote'.
 
create_http_client( EXPORTING ip_url = lv_url
IMPORTING ex_http_client = lv_http_client ).
 
http_client_request_get_method( EXPORTING ip_http_client = lv_http_client ).
 
http_client_send( EXPORTING ip_http_client = lv_http_client ).
 
http_client_receive( EXPORTING ip_http_client = lv_http_client
IMPORTING ex_content = lv_content ).
 
get_data_ex( EXPORTING ip_content = lv_content ).
 
* Updating data every time
CLEAR:gt_dest1.
 
me->gt_dest1 = gt_dest.
 
ENDMETHOD.
 
METHOD timer_event .
 
DATA: lv_time TYPE char10.
 
me->get_data( ) .
 
IF me->ob_grid IS INITIAL .
 
CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR'
IMPORTING
e_grid = ob_grid.
ELSE.
 
CALL METHOD ob_grid->refresh_table_display.
 
ENDIF.
 
WRITE sy-uzeit TO lv_time USING EDIT MASK '__:__:__'.
 
CONCATENATE 'Screen refreshed at' lv_time INTO DATA(lv_msg) SEPARATED BY space.
 
MESSAGE lv_msg TYPE 'S' . " Get the cuuent timE as message
 
* Call RUN method of CL_GUI_TIMER again to activate timer
me->ob_timer->run( ) .
 
ENDMETHOD.
 
METHOD display_report . " Only call for first display
 
me->get_data( ) .
 
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'
EXPORTING
i_structure_name = 'ZAUTO_EXC'
TABLES
t_outtab = me->gt_dest1.
ENDMETHOD.
 
ENDCLASS.
 
*--------------------------------------------------------------------*
* Start of Selection
*--------------------------------------------------------------------*
DATA : ob_auto TYPE REF TO cl_auto,
ob_timer TYPE REF TO cl_gui_timer.
 
*--------------------------------------------------------------------*
* Selection Screen Parameters
*--------------------------------------------------------------------*
PARAMETERS : p_refres TYPE char01 AS CHECKBOX,
p_int TYPE i.
 
*--------------------------------------------------------------------*
* Start of Selection
*--------------------------------------------------------------------*
START-OF-SELECTION.
 
* Create main object
CREATE OBJECT ob_auto .
 
IF p_refres IS NOT INITIAL .
 
CREATE OBJECT ob_auto->ob_timer .
SET HANDLER ob_auto->timer_event FOR ob_auto->ob_timer .
 
* Set interval for timer
ob_auto->ob_timer->interval = p_int .
 
* Call method RUN of CL_GUI_TIMER.
ob_auto->ob_timer->run( ) .
 
ENDIF.
 
*--------------------------------------------------------------------*
* End of Selection
*--------------------------------------------------------------------*
END-OF-SELECTION.
 
* display dat first time
ob_auto->display_report( ) .