CLASS zcl_em_messaging DEFINITION PUBLIC.
* Generated by abap-openapi-client
* SAP Event Mesh - REST APIs Messaging, 0.1.0
  PUBLIC SECTION.
    INTERFACES zif_em_messaging.
    METHODS constructor IMPORTING ii_client TYPE REF TO if_http_client.
  PROTECTED SECTION.
    DATA mi_client TYPE REF TO if_http_client.
    DATA mo_json TYPE REF TO zcl_oapi_json.
    METHODS send_receive RETURNING VALUE(rv_code) TYPE i.
    METHODS parse_errormessage
      IMPORTING iv_prefix TYPE string
      RETURNING VALUE(errormessage) TYPE zif_em_messaging=>errormessage
      RAISING cx_static_check.
    METHODS parse_qos
      IMPORTING iv_prefix TYPE string
      RETURNING VALUE(qos) TYPE zif_em_messaging=>qos
      RAISING cx_static_check.
    METHODS json_create_subscription
      IMPORTING data TYPE zif_em_messaging=>bodycreate_subscription
      RETURNING VALUE(json) TYPE string
      RAISING cx_static_check.
    METHODS parse_create_subscription
      IMPORTING iv_prefix TYPE string
      RETURNING VALUE(response_create_subscription) TYPE zif_em_messaging=>response_create_subscription
      RAISING cx_static_check.
ENDCLASS.

CLASS zcl_em_messaging IMPLEMENTATION.
  METHOD constructor.
    mi_client = ii_client.
  ENDMETHOD.

  METHOD send_receive.
    mi_client->send( ).
    mi_client->receive( ).
    mi_client->response->get_status( IMPORTING code = rv_code ).
  ENDMETHOD.

  METHOD parse_errormessage.
    errormessage-message = mo_json->value_string( iv_prefix && '/message' ).
  ENDMETHOD.

  METHOD parse_qos.
    qos = mo_json->value_integer( iv_prefix && '/' ).
  ENDMETHOD.

  METHOD parse_create_subscription.
    response_create_subscription-href = mo_json->value_string( iv_prefix && '/href' ).
  ENDMETHOD.

  METHOD json_create_subscription.
    json = json && '{'.
    json = json && |"name": "{ data-name }",|.
    json = json && |"address": "{ data-address }",|.
*  json = json && '"qos":' not simple
*  json = json && '"pushConfig":' not simple
    json = substring( val = json off = 0 len = strlen( json ) - 1 ).
    json = json && '}'.
  ENDMETHOD.

  METHOD zif_em_messaging~publish_message_to_queue.
    DATA lv_code TYPE i.
    DATA lv_temp TYPE string.
    DATA lv_uri TYPE string VALUE '/messagingrest/v1/queues/{queue-name}/messages'.
    lv_temp = queue_name.
    lv_temp = cl_http_utility=>escape_url( condense( lv_temp ) ).
    REPLACE ALL OCCURRENCES OF '{queue-name}' IN lv_uri WITH lv_temp.
    mi_client->request->set_method( 'POST' ).
    mi_client->request->set_header_field( name = '~request_uri' value = lv_uri ).
    mi_client->request->set_header_field( name = 'x-qos' value = x_qos ).
    mi_client->request->set_header_field( name = 'x-message-expiration' value = x_message_expiration ).
    mi_client->request->set_cdata( body ).
    lv_code = send_receive( ).
    WRITE / lv_code.
    CASE lv_code.
      WHEN 204. " No Content, message published successfully
      WHEN 400. " Bad Request
" application/json,#/components/schemas/ErrorMessage
        CREATE OBJECT mo_json EXPORTING iv_json = mi_client->response->get_cdata( ).
        parse_errormessage( '' ).
" todo, raise
      WHEN 404. " Not Found
" application/json,#/components/schemas/ErrorMessage
        CREATE OBJECT mo_json EXPORTING iv_json = mi_client->response->get_cdata( ).
        parse_errormessage( '' ).
" todo, raise
    ENDCASE.
  ENDMETHOD.

  METHOD zif_em_messaging~publish_message_to_topic.
    DATA lv_code TYPE i.
    DATA lv_temp TYPE string.
    DATA lv_uri TYPE string VALUE '/messagingrest/v1/topics/{topic-name}/messages'.
    lv_temp = topic_name.
    lv_temp = cl_http_utility=>escape_url( condense( lv_temp ) ).
    REPLACE ALL OCCURRENCES OF '{topic-name}' IN lv_uri WITH lv_temp.
    mi_client->request->set_method( 'POST' ).
    mi_client->request->set_header_field( name = '~request_uri' value = lv_uri ).
    mi_client->request->set_header_field( name = 'x-qos' value = x_qos ).
    mi_client->request->set_header_field( name = 'x-message-expiration' value = x_message_expiration ).
    mi_client->request->set_cdata( body ).
    lv_code = send_receive( ).
    WRITE / lv_code.
    CASE lv_code.
      WHEN 204. " No Content, message published successfully
      WHEN 400. " Bad Request
" application/json,#/components/schemas/ErrorMessage
        CREATE OBJECT mo_json EXPORTING iv_json = mi_client->response->get_cdata( ).
        parse_errormessage( '' ).
" todo, raise
    ENDCASE.
  ENDMETHOD.

  METHOD zif_em_messaging~consume_from_queue.
    DATA lv_code TYPE i.
    DATA lv_temp TYPE string.
    DATA lv_uri TYPE string VALUE '/messagingrest/v1/queues/{queue-name}/messages/consumption'.
    lv_temp = queue_name.
    lv_temp = cl_http_utility=>escape_url( condense( lv_temp ) ).
    REPLACE ALL OCCURRENCES OF '{queue-name}' IN lv_uri WITH lv_temp.
    mi_client->request->set_method( 'POST' ).
    mi_client->request->set_header_field( name = '~request_uri' value = lv_uri ).
    mi_client->request->set_header_field( name = 'x-qos' value = x_qos ).
    lv_code = send_receive( ).
    WRITE / lv_code.
    CASE lv_code.
      WHEN 200. " Ok
" application/json,
      WHEN 204. " No Content, queue is empty
      WHEN 404. " Not Found
" application/json,#/components/schemas/ErrorMessage
        CREATE OBJECT mo_json EXPORTING iv_json = mi_client->response->get_cdata( ).
        parse_errormessage( '' ).
" todo, raise
    ENDCASE.
  ENDMETHOD.

  METHOD zif_em_messaging~acknowledge_message.
    DATA lv_code TYPE i.
    DATA lv_temp TYPE string.
    DATA lv_uri TYPE string VALUE '/messagingrest/v1/queues/{queue-name}/messages/{message-id}/acknowledgement'.
    lv_temp = queue_name.
    lv_temp = cl_http_utility=>escape_url( condense( lv_temp ) ).
    REPLACE ALL OCCURRENCES OF '{queue-name}' IN lv_uri WITH lv_temp.
    lv_temp = message_id.
    lv_temp = cl_http_utility=>escape_url( condense( lv_temp ) ).
    REPLACE ALL OCCURRENCES OF '{message-id}' IN lv_uri WITH lv_temp.
    mi_client->request->set_method( 'POST' ).
    mi_client->request->set_header_field( name = '~request_uri' value = lv_uri ).
    lv_code = send_receive( ).
    WRITE / lv_code.
    CASE lv_code.
      WHEN 202. " Accepted
" todo, raise
      WHEN 400. " Bad Request
" application/json,#/components/schemas/ErrorMessage
        CREATE OBJECT mo_json EXPORTING iv_json = mi_client->response->get_cdata( ).
        parse_errormessage( '' ).
" todo, raise
    ENDCASE.
  ENDMETHOD.

  METHOD zif_em_messaging~create_subscription.
    DATA lv_code TYPE i.
    DATA lv_temp TYPE string.
    DATA lv_uri TYPE string VALUE '/messagingrest/v1/subscriptions'.
    mi_client->request->set_method( 'POST' ).
    mi_client->request->set_header_field( name = '~request_uri' value = lv_uri ).
    mi_client->request->set_cdata( json_create_subscription( body ) ).
    lv_code = send_receive( ).
    WRITE / lv_code.
    CASE lv_code.
      WHEN 202. " Accepted
" application/json,#/components/schemas/response_create_subscription
        CREATE OBJECT mo_json EXPORTING iv_json = mi_client->response->get_cdata( ).
        parse_create_subscription( '' ).
" todo, raise
      WHEN 400. " Bad Request
" application/json,#/components/schemas/ErrorMessage
        CREATE OBJECT mo_json EXPORTING iv_json = mi_client->response->get_cdata( ).
        parse_errormessage( '' ).
" todo, raise
      WHEN 409. " Conflict
" application/json,#/components/schemas/ErrorMessage
        CREATE OBJECT mo_json EXPORTING iv_json = mi_client->response->get_cdata( ).
        parse_errormessage( '' ).
" todo, raise
    ENDCASE.
  ENDMETHOD.

ENDCLASS.
