Package com.oorian.apis
Class ApiHttpRequest
java.lang.Object
com.oorian.apis.ApiHttpRequest
- Direct Known Subclasses:
DeleteRequest,GetRequest,MultipartRequest,PutRequest,RawDataRequest,UrlEncodedRequest
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 Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionApiHttpRequest(int method) Creates a new request with the specified HTTP method.ApiHttpRequest(int method, String url) Creates a new request with the specified HTTP method and URL.ApiHttpRequest(int method, String url, String contentType) Creates a new request with the specified HTTP method, URL, and content type. -
Method Summary
Modifier and TypeMethodDescriptionvoidAdds a custom header to this request.protected voidaddHeaders(HttpRequest.Builder builder) Returns the character encoding for this request.protected abstract HttpRequest.BodyPublishergetData()Returns the list of custom headers added to this request.protected StringgetUrl()Returns the target URL for this request.protected HttpURLConnectionsend()Sends the HTTP request and returns the response body as a string.protected Stringsend(HttpClient client, HttpRequest request) voidsetCharset(String charset) Sets the character encoding for this request.voidsetConnectTimeout(int connectTimeout) Sets the connection timeout in milliseconds.voidsetContentType(String contentType) Sets the Content-Type header for this request.voidsetReadTimeout(int readTimeout) Sets the read timeout in milliseconds.voidSets the target URL for this request.
-
Field Details
-
GET
public static final int GET- See Also:
-
PUT
public static final int PUT- See Also:
-
POST
public static final int POST- See Also:
-
DELETE
public static final int DELETE- See Also:
-
-
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
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
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
Sets the target URL for this request.- Parameters:
url- the target URL.
-
setContentType
Sets the Content-Type header for this request.- Parameters:
contentType- the Content-Type header value.
-
addHeader
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
Sets the character encoding for this request.- Parameters:
charset- the character encoding (default is "UTF-8").
-
getUrl
Returns the target URL for this request.- Returns:
- the target URL.
-
getHeaders
Returns the list of custom headers added to this request.- Returns:
- the list of request headers.
-
getCharset
Returns the character encoding for this request.- Returns:
- the character encoding.
-
send
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
- Throws:
HttpRequestException
-
send
- Throws:
HttpRequestException
-
getUrlConnection
- Throws:
HttpRequestException
-
addHeaders
-
getResponse
- Throws:
HttpRequestException
-