RESTful API 호출하는 예제 프로그램.
*&---------------------------------------------------------------------*
*& Report ZRESTFUL_API
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT zrestful_api.
PARAMETERS: p_string TYPE string.
TYPES: BEGIN OF ts_req,
BEGIN OF argument,
type TYPE string,
question TYPE string,
END OF argument,
END OF ts_req,
BEGIN OF ts_res_irinfo,
wiki_title TYPE string,
sent TYPE string,
url TYPE string,
END OF ts_res_irinfo,
BEGIN OF ts_res_answerinfo,
rank TYPE i,
answer TYPE string,
confidence TYPE i,
url TYPE STANDARD TABLE OF string WITH DEFAULT KEY,
END OF ts_res_answerinfo,
BEGIN OF ts_res,
result TYPE string,
BEGIN OF return_object,
BEGIN OF wikiinfo,
irinfo TYPE STANDARD TABLE OF ts_res_irinfo WITH DEFAULT KEY,
answerinfo TYPE STANDARD TABLE OF ts_res_answerinfo WITH DEFAULT KEY,
END OF wikiinfo,
END OF return_object,
END OF ts_res.
DATA: lv_url TYPE string,
ls_req TYPE ts_req,
lv_req_json TYPE string,
lo_client TYPE REF TO if_http_client,
lv_message TYPE string,
lv_res_json TYPE string,
ls_res TYPE ts_res.
* 위키백과 QA API
* https://aiopen.etri.re.kr/guide/WikiQA
lv_url = |http://aiopen.etri.re.kr:8000/WikiQA|.
ls_req-argument-type = 'hybridqa'.
ls_req-argument-question = p_string.
"REQ to json
/ui2/cl_json=>serialize(
EXPORTING
data = ls_req " Data to serialize
pretty_name = /ui2/cl_json=>pretty_mode-low_case " Pretty Print property names
RECEIVING
r_json = lv_req_json " JSON string
).
"HTTP create
cl_http_client=>create_by_url(
EXPORTING
url = lv_url " URL
IMPORTING
client = lo_client " HTTP Client Abstraction
EXCEPTIONS
argument_not_found = 1 " Communication parameter (host or service) not available
plugin_not_active = 2 " HTTP/HTTPS communication not available
internal_error = 3 " Internal error (e.g. name too long)
pse_not_found = 4 " PSE not found
pse_not_distrib = 5 " PSE not distributed
pse_errors = 6 " General PSE error
OTHERS = 7
).
IF sy-subrc <> 0.
MESSAGE 'HTTP error' TYPE 'E'.
RETURN.
ENDIF.
" HTTP request
lo_client->request->set_method(
method = if_http_request=>co_request_method_post
).
lo_client->request->set_header_field(
EXPORTING
name = 'Content-Type'
value = 'application/json'
).
lo_client->request->set_header_field(
EXPORTING
name = 'Authorization'
value = 'cfd006b6-7766-42ca-88fc-3cd1e1c357f7'
).
lo_client->request->set_cdata(
EXPORTING
data = lv_req_json
).
"HTTP send
lo_client->send(
EXCEPTIONS
http_communication_failure = 1 " Communication Error
http_invalid_state = 2 " Invalid state
http_processing_failed = 3 " Error when processing method
http_invalid_timeout = 4 " Invalid Time Entry
OTHERS = 5
).
IF sy-subrc <> 0.
lo_client->get_last_error(
IMPORTING
message = lv_message
).
MESSAGE lv_message TYPE 'E'.
RETURN.
ENDIF.
"HTTP receive
lo_client->receive(
EXCEPTIONS
http_communication_failure = 1 " Communication Error
http_invalid_state = 2 " Invalid state
http_processing_failed = 3 " Error when processing method
OTHERS = 4
).
IF sy-subrc <> 0.
lo_client->get_last_error(
IMPORTING
message = lv_message
).
MESSAGE lv_message TYPE 'E'.
RETURN.
ENDIF.
"HTTP response
IF lo_client->response IS NOT INITIAL.
lv_res_json = lo_client->response->get_cdata( ).
ENDIF.
"json to RES
/ui2/cl_json=>deserialize(
EXPORTING
json = lv_res_json " JSON string
CHANGING
data = ls_res " Data to serialize
).
*BREAK-POINT.
IF ls_res-return_object-wikiinfo-answerinfo IS NOT INITIAL.
cl_demo_output=>display( ls_res-return_object-wikiinfo-answerinfo[ 1 ]-answer ).
ENDIF.
'ABAP' 카테고리의 다른 글
SAP ABAP Currency 총정리 (0) | 2024.11.15 |
---|---|
SY-DATUM 과 SY-DATLO 차이 (0) | 2024.01.04 |
abap으로 ZIP 압축하기 (0) | 2023.10.17 |
search help exit 총정리 (0) | 2023.04.25 |
FUNCTION ZADD_1_ALPHANUM (0) | 2022.12.20 |