Class ApiHttpRequest

java.lang.Object
com.oorian.apis.ApiHttpRequest
Direct Known Subclasses:
DeleteRequest, GetRequest, MultipartRequest, PutRequest, RawDataRequest, UrlEncodedRequest

public abstract class ApiHttpRequest extends Object
Abstract base class for making HTTP requests to external APIs.

Provides common functionality for GET, PUT, POST, and DELETE requests including header management, timeout configuration, and response handling. Concrete subclasses define how request data is serialized and sent.

  • Field Details

  • Constructor Details

    • ApiHttpRequest

      public ApiHttpRequest(int method)
      Creates a new request with the specified HTTP method.
      Parameters:
      method - the HTTP method constant (GET, PUT, POST, or DELETE).
    • ApiHttpRequest

      public ApiHttpRequest(int method, String url)
      Creates a new request with the specified HTTP method and URL.
      Parameters:
      method - the HTTP method constant (GET, PUT, POST, or DELETE).
      url - the target URL.
    • ApiHttpRequest

      public ApiHttpRequest(int method, String url, String contentType)
      Creates a new request with the specified HTTP method, URL, and content type.
      Parameters:
      method - the HTTP method constant (GET, PUT, POST, or DELETE).
      url - the target URL.
      contentType - the Content-Type header value.
  • Method Details

    • setUrl

      public void setUrl(String url)
      Sets the target URL for this request.
      Parameters:
      url - the target URL.
    • setContentType

      public void setContentType(String contentType)
      Sets the Content-Type header for this request.
      Parameters:
      contentType - the Content-Type header value.
    • addHeader

      public void addHeader(String name, String value)
      Adds a custom header to this request.
      Parameters:
      name - the header name.
      value - the header value.
    • setConnectTimeout

      public void setConnectTimeout(int connectTimeout)
      Sets the connection timeout in milliseconds.
      Parameters:
      connectTimeout - the timeout in milliseconds.
    • setReadTimeout

      public void setReadTimeout(int readTimeout)
      Sets the read timeout in milliseconds.
      Parameters:
      readTimeout - the timeout in milliseconds.
    • setCharset

      public void setCharset(String charset)
      Sets the character encoding for this request.
      Parameters:
      charset - the character encoding (default is "UTF-8").
    • getUrl

      public String getUrl()
      Returns the target URL for this request.
      Returns:
      the target URL.
    • getHeaders

      public List<RequestHeader> getHeaders()
      Returns the list of custom headers added to this request.
      Returns:
      the list of request headers.
    • getCharset

      public String getCharset()
      Returns the character encoding for this request.
      Returns:
      the character encoding.
    • send

      public String send() throws HttpRequestException
      Sends the HTTP request and returns the response body as a string.
      Returns:
      the response body.
      Throws:
      HttpRequestException - if the request fails or the server returns a non-2xx status.
    • getData

      protected abstract HttpRequest.BodyPublisher getData() throws HttpRequestException
      Throws:
      HttpRequestException
    • send

      protected String send(HttpClient client, HttpRequest request) throws HttpRequestException
      Throws:
      HttpRequestException
    • getUrlConnection

      protected HttpURLConnection getUrlConnection() throws HttpRequestException
      Throws:
      HttpRequestException
    • addHeaders

      protected void addHeaders(HttpRequest.Builder builder)
    • getResponse

      protected String getResponse() throws HttpRequestException
      Throws:
      HttpRequestException