Skip to content

Class ZCL_LLM_HTTP_CLIENT_WRAPPER

AI Generated documentation.

Overview

The zcl_llm_http_client_wrapper class is designed to encapsulate the functionality of an HTTP client, providing a wrapper around the SAP HTTP client. This class is part of a larger system that handles HTTP communication, logging, and error handling. The class implements the zif_llm_http_client_wrapper interface, which defines the public methods for interacting with the HTTP client.

Public Methods

  1. Constructor:
  2. Purpose: Initializes the HTTP client wrapper with the provided client_config and provider_config.
  3. Parameters:
    • client_config: Configuration for the client.
    • provider_config: Configuration for the provider.
  4. Exceptions: Raises zcx_llm_validation if there is an error during initialization.

  5. get_client:

  6. Purpose: Returns a new instance of the HTTP client wrapper.
  7. Parameters: None.
  8. Returns: A new instance of zcl_llm_http_client_wrapper.

  9. set_header:

  10. Purpose: Sets a header on the HTTP request.
  11. Parameters:
    • name: The name of the header.
    • value: The value of the header.
  12. Returns: None.

  13. set_url:

  14. Purpose: Sets the URL for the HTTP request.
  15. Parameters:
    • url: The URL to be set.
  16. Returns: None.

  17. communicate:

  18. Purpose: Sends an HTTP request and handles the response.
  19. Parameters: None.
  20. Returns: None.
  21. Exceptions: Raises zcx_llm_http_error if there is an error during communication.

  22. close_client:

  23. Purpose: Closes the HTTP client.
  24. Parameters: None.
  25. Returns: None.

Dependencies

  • zif_llm_http_client_wrapper: Interface that defines the public methods for the HTTP client wrapper.
  • zcl_llm_common: Provides utility methods, such as get_llm_badi.
  • zcx_llm_validation: Exception class for validation errors.
  • zcx_llm_http_error: Exception class for HTTP communication errors.
  • cl_http_client: SAP HTTP client class.
  • cl_http_utility: Utility class for HTTP operations.
  • zif_llm_call_logger: Interface for logging HTTP calls.

Details

The zcl_llm_http_client_wrapper class is designed to handle HTTP communication in a structured and error-resistant manner. It uses the SAP HTTP client to send and receive HTTP requests and responses. The class also includes logging functionality to record the details of each HTTP call, which is crucial for debugging and monitoring.

Class Structure

The class is divided into three sections: public, protected, and private.

  • Public Section: Contains the interface methods and the constructor.
  • Protected Section: Contains data members for configuration, URL, client, and call logger.
  • Private Section: Contains the implementation details of the methods.

Key Interactions

  1. Initialization:
  2. The constructor initializes the client and provider configurations.
  3. It retrieves the call logger implementation using a Business Add-In (BADI).
  4. It creates an HTTP client using the destination specified in the provider configuration.
  5. If the destination is not found or there is an error, it raises a validation exception.

  6. Header and URL Setting:

  7. The set_header method sets a header on the HTTP request.
  8. The set_url method sets the URL for the HTTP request.

  9. Communication:

  10. The communicate method sends an HTTP POST request with a JSON content type.
  11. It sets the request URI if a URL is provided.
  12. It sends the request and receives the response.
  13. It logs the request and response details using the call logger.
  14. If there is an error during communication, it raises an HTTP error exception.

  15. Client Management:

  16. The get_client method returns a new instance of the HTTP client wrapper.
  17. The close_client method closes the HTTP client, ignoring any exceptions that occur during the process.

Logic Flow

The following Mermaid diagram illustrates the logic flow of the communicate method:

graph TD
    A[Start] --> B[Set HTTP Method to POST]
    B --> C[Set Content-Type Header]
    C --> D[Check if URL is set]
    D -->|Yes| E[Set Request URI]
    D -->|No| F[Skip Setting URI]
    E --> F
    F --> G[Set Request Data]
    G --> H[Send Request]
    H --> I[Check Response Code]
    I -->|Success| J[Receive Response]
    I -->|Failure| K[Get Last Error]
    J --> L[Log Request and Response]
    K --> M[Raise HTTP Error]
    L --> N[End]
    M --> N

This diagram shows the sequence of operations performed during an HTTP communication, including setting headers, sending the request, receiving the response, and handling errors.