Consuming a remote API from the BO (SCM, SCM2, HTTP client)

A behavior pool can call a remote API with the usual ABAP Cloud code: the HTTP client (cl_web_http_client_manager) for a REST API, the OData client proxy of a service consumption model for an OData V2 or V4 service. The generated application runs this code as is; only the destination is configured differently from SAP (decision D31).

1. What to export with abapGit

On SAPIn the abapGit repositoryWhat Open RAP Runtime does with it
Service consumption model 2 (OData V4)the generated consumption model class (INHERITING FROM /iwbep/cl_v4_abs_pm_model_prov)keeps its types (tys_…, tyt_…) and constants (gcs_entity_set…); the define method is removed
Service consumption model (OData V2/V4, first generation)the generated abstract entities and the service definition of the proxythe abstract entities type the data; the service definition without a binding is not built (info SRVD007)
REST APInothing

The .srvc file of the service consumption model itself is not needed: the proxy reads the $metadata of the remote service on its first call.

2. The code of the behavior pool

The same as on SAP, for example in an action:

METHOD importRemote.
  DATA remote_travels TYPE zscm_rt_travel=>tyt_travel_type.
  DATA travel_ids     TYPE RANGE OF zscm_rt_travel=>tys_travel_type-travel_id.

  READ ENTITIES OF zrt_r_travel IN LOCAL MODE
    ENTITY Travel FIELDS ( TravelID ) WITH CORRESPONDING #( keys )
    RESULT DATA(travels).
  travel_ids = VALUE #( FOR t IN travels ( sign = 'I' option = 'EQ' low = t-TravelID ) ).

  TRY.
      DATA(destination) = cl_http_destination_provider=>create_by_comm_arrangement(
                            comm_scenario = 'ZRT_TRAVEL_REMOTE'
                            service_id    = 'ZRT_TRAVEL_REMOTE_REST' ).
      DATA(proxy) = /iwbep/cl_cp_factory_remote=>create_v4_remote_proxy(
                      is_proxy_model_key       = VALUE #( repository_id       = 'DEFAULT'
                                                          proxy_model_id      = 'ZSCM_RT_TRAVEL'
                                                          proxy_model_version = '0001' )
                      io_http_client           = cl_web_http_client_manager=>create_by_http_destination( destination )
                      iv_relative_service_root = '/sap/opu/odata4/sap/zrt_ui_travel_o4/srvd/sap/zrt_ui_travel/0001/' ).

      DATA(request) = proxy->create_resource_for_entity_set( zscm_rt_travel=>gcs_entity_set-travel )->create_request_for_read( ).
      DATA(filter_factory) = request->create_filter_factory( ).
      request->set_filter( filter_factory->create_by_range( iv_property_path = 'TRAVEL_ID' it_range = travel_ids ) ).
      request->set_top( 50 ).
      DATA(response) = request->execute( ).
      response->get_business_data( IMPORTING et_business_data = remote_travels ).

    CATCH /iwbep/cx_cp_remote INTO DATA(remote_error).
      " remote_error->http_status_code, remote_error->get_text( )
    CATCH /iwbep/cx_gateway cx_web_http_client_error cx_http_dest_provider_error INTO DATA(error).
      " error->get_text( )
  ENDTRY.
ENDMETHOD.

The internal names of the class (TRAVEL_ID, 'TRAVEL') are matched with the names of the service (TravelID, Travel) ignoring case and _. Supported: list read (create_by_range, and/or/not, set_select_properties, set_orderby, set_top, set_skip, request_count, request_no_business_data), read by key (navigate_with_key), create, update (PATCH or PUT, it_provided_property, set_if_match), delete. The CSRF token and the session cookies of SAP Gateway are handled by the proxy.

A REST API:

DATA(client) = cl_web_http_client_manager=>create_by_http_destination(
                 cl_http_destination_provider=>create_by_cloud_destination( i_name       = 'RATINGS'
                                                                            i_authn_mode = if_a4c_cp_service=>service_specific ) ).
DATA(request) = client->get_http_request( ).
request->set_uri_path( |/api/ratings/{ travel-TravelID }| ).
request->set_header_field( i_name = 'Accept' i_value = 'application/json' ).
DATA(response) = client->execute( if_web_http_client=>get ).
DATA(status) = response->get_status( ).
IF status-code = 200.
  DATA(json) = response->get_text( ).
ENDIF.

3. The destination

On SAP, the URL and the credentials come from a communication arrangement or from the destination service of BTP. In the generated application, they are environment variables:

VariableContent
DESTINATION_<NAME>_URLbase URL (mandatory)
DESTINATION_<NAME>_USER, DESTINATION_<NAME>_PASSWORDbasic authentication
DESTINATION_<NAME>_TOKENbearer token (Authorization: Bearer)
DESTINATION_<NAME>_HEADERSother headers, JSON object: {"APIKey":"…"}
DESTINATION_<NAME>_TOKEN_URLtoken endpoint of an OAuth 2.0 authorization server: the client credentials grant (D122)
DESTINATION_<NAME>_CLIENT_ID, DESTINATION_<NAME>_CLIENT_SECRETthe OAuth client (the ID is mandatory with _TOKEN_URL)
DESTINATION_<NAME>_CLIENT_AUTHhow the client authenticates: basic (HTTP basic, client_secret_basic, the default), post (in the body, client_secret_post), none (its ID only), mtls (its certificate, D124) or jwt (a JWT signed with its key, D124)
DESTINATION_<NAME>_SCOPEscopes of the token request, separated by spaces
DESTINATION_<NAME>_TOKEN_PARAMSfurther parameters of the token request, JSON object: {"audience":"…"}, {"resource":"…"}
DESTINATION_<NAME>_REFRESH_TOKENa refresh token: the refresh token grant instead of client credentials (D123)
DESTINATION_<NAME>_CLIENT_CERT, DESTINATION_<NAME>_CLIENT_KEY, DESTINATION_<NAME>_CLIENT_KEY_PASSPHRASEX.509 client certificate and its private key (D124), PEM: the PEM itself or the path of a file
DESTINATION_<NAME>_CAcertificate authorities trusted for the server of the destination, besides the ones of Node.js (PEM or file)
DESTINATION_<NAME>_CLIENT_KEY_IDkid of the client assertion of _CLIENT_AUTH=jwt
DESTINATION_<NAME>_OAUTH1_CONSUMER_KEY, _OAUTH1_CONSUMER_SECRET, _OAUTH1_TOKEN, _OAUTH1_TOKEN_SECRET, _OAUTH1_SIGNATURE_METHOD, _OAUTH1_REALMOAuth 1.0 (D124)

<NAME> is the name of create_by_cloud_destination (RATINGS), or for create_by_comm_arrangement: <SCENARIO>_<SERVICE_ID>, otherwise <SCENARIO> (ZRT_TRAVEL_REMOTE_ZRT_TRAVEL_REMOTE_REST, then ZRT_TRAVEL_REMOTE), in upper case, the characters other than letters and digits replaced by _. rapt build lists the destinations written as literals in the .env.example and the README of the application. The credentials stay on the server: the ABAP only sees the URL.

OAuth 2.0 client credentials

With _TOKEN_URL, the application asks the authorization server for an access token (grant_type=client_credentials, RFC 6749 section 4.4) and sends it as Authorization: Bearer; it replaces the authorization of _USER/_PASSWORD and _TOKEN. This is the OAuth 2.0 Client Credentials authentication of an outbound communication user on SAP, and of the destinations of type OAuth2ClientCredentials of SAP BTP (for SAP BTP: the url of the XSUAA or IAS instance followed by /oauth/token, its clientid and clientsecret).

DESTINATION_ENERGY_URL=https://api.energy.example.com
DESTINATION_ENERGY_TOKEN_URL=https://auth.energy.example.com/oauth/token
DESTINATION_ENERGY_CLIENT_ID=my-client
DESTINATION_ENERGY_CLIENT_SECRET=...
DESTINATION_ENERGY_SCOPE=meter.read

OAuth 2.0 refresh token

Many APIs (energy, home automation, the consumer APIs of the cloud) give no client credentials: the owner of the account consents once (authorization code grant, in a browser), and the application gets a refresh token, from which it asks for access tokens. With _REFRESH_TOKEN, the application uses the refresh token grant (grant_type=refresh_token, RFC 6749 section 6) with the same _TOKEN_URL, _CLIENT_ID, _CLIENT_SECRET (none for a public client: its ID is sent in the body), _CLIENT_AUTH, _SCOPE and _TOKEN_PARAMS:

DESTINATION_ENERGY_URL=https://api.energy.example.com
DESTINATION_ENERGY_TOKEN_URL=https://auth.energy.example.com/oauth/token
DESTINATION_ENERGY_CLIENT_ID=my-client
DESTINATION_ENERGY_CLIENT_SECRET=...
DESTINATION_ENERGY_REFRESH_TOKEN=...      # from the consent of the owner of the account

The other authentication methods of ABAP Cloud (D124)

The users of outbound communication of a communication system of ABAP Cloud authenticate with a user and a password, an X.509 client certificate, OAuth 1.0 or OAuth 2.0, whose client authenticates with a secret (Basic), mTLS, a JWT or nothing (None). All of them are destinations of the environment:

ABAP CloudVariables
User name and password_USER, _PASSWORD
SSL client certificate_CLIENT_CERT, _CLIENT_KEY (_CLIENT_KEY_PASSPHRASE)
OAuth 2.0, client authentication Basic_TOKEN_URL, _CLIENT_ID, _CLIENT_SECRET (_CLIENT_AUTH=basic or post)
OAuth 2.0, client authentication mTLS_TOKEN_URL (the mTLS endpoint: certurl + /oauth/token for XSUAA), _CLIENT_ID, _CLIENT_AUTH=mtls, _CLIENT_CERT, _CLIENT_KEY
OAuth 2.0, client authentication JWT_TOKEN_URL, _CLIENT_ID, _CLIENT_AUTH=jwt, _CLIENT_KEY, _CLIENT_CERT (the certificate registered at the authorization server) or _CLIENT_KEY_ID
OAuth 2.0, client authentication None_TOKEN_URL, _CLIENT_ID, _CLIENT_AUTH=none
OAuth 1.0_OAUTH1_CONSUMER_KEY, _OAUTH1_CONSUMER_SECRET, _OAUTH1_TOKEN, _OAUTH1_TOKEN_SECRET (_OAUTH1_SIGNATURE_METHOD, _OAUTH1_REALM)

Authorization code

The authorization code grant (with PKCE) needs a user in a browser: it has no place in a call of the ABAP code. It gives the first refresh token: do the consent once with the tools of the provider (its developer portal, an OAuth client such as oauth2c or Postman) and set the refresh token it gives in _REFRESH_TOKEN. Not supported: the password grant (deprecated by the OAuth 2.0 Security BCP), user propagation (SAML bearer, token exchange, a token per user).

With docker compose, in the app service:

    environment:
      DESTINATION_ZRT_TRAVEL_REMOTE_URL: https://my-system.example.com
      DESTINATION_ZRT_TRAVEL_REMOTE_USER: ${REMOTE_USER}
      DESTINATION_ZRT_TRAVEL_REMOTE_PASSWORD: ${REMOTE_PASSWORD}

Behind an outgoing proxy: HTTPS_PROXY=… and NODE_USE_ENV_PROXY=1.

4. Limits

See KNOWN_GAPS.md: no $batch, $expand, deep insert, navigation by association nor actions of the remote service in the client proxy; value controls not filled; no user propagation, no OAuth grant other than client credentials and refresh token; no communication targets, SOAP nor RFC. As on SAP, a remote call made during the interaction is not undone by a rollback of the LUW.

Complete tested example: packages/runtime/test/variants.ts (remoteVariant) and packages/runtime/test/rap-variant-remote.test.ts.