Class RestApiClient
java.lang.Object
overit.geocallapp.wfm.orchestrator.planning.utilities.rest.RestApiClient
Client for making REST API calls with support for different authentication methods.
This class provides methods for making HTTP requests to a REST API.
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionRestApiClient(String baseUrl) Creates a new REST API client with the specified base URL and no authentication.RestApiClient(String baseUrl, AuthManager authManager) Creates a new REST API client with the specified base URL and authentication manager. -
Method Summary
Modifier and TypeMethodDescriptionbooleanMakes a DELETE request to the specified endpoint.booleanMakes a DELETE request to the specified endpoint with query parameters.<T> Optional<T> Makes a GET request to the specified endpoint.<T> Optional<T> Makes a GET request to the specified endpoint with query parameters.Gets the authentication manager used by this client.<T,R> Optional <R> Makes a PATCH request to the specified endpoint with a request body.<R> Optional<R> Makes a POST request to the specified endpoint with no request body.<T,R> Optional <R> Makes a POST request to the specified endpoint with a request body.<T,R> Optional <R> Makes a PUT request to the specified endpoint with a request body.static RestApiClientwithApiToken(String baseUrl, String token) Creates a new REST API client with the specified base URL and API token.static RestApiClientwithBasicAuth(String baseUrl, String username, String password) Creates a new REST API client with the specified base URL and HTTP Basic Authentication.static RestApiClientwithUsernamePassword(String baseUrl, String authUrl, String username, String password) Creates a new REST API client with the specified base URL and username/password authentication with JWT token.
-
Field Details
-
LOG
-
-
Constructor Details
-
RestApiClient
Creates a new REST API client with the specified base URL and no authentication.- Parameters:
baseUrl- The base URL of the API
-
RestApiClient
Creates a new REST API client with the specified base URL and authentication manager.- Parameters:
baseUrl- The base URL of the APIauthManager- The authentication manager to use (can be null for unauthenticated requests)
-
-
Method Details
-
withApiToken
Creates a new REST API client with the specified base URL and API token. This is a convenience method that creates an API token authentication manager.- Parameters:
baseUrl- The base URL of the APItoken- The API token to use for authentication- Returns:
- A new REST API client
-
withUsernamePassword
public static RestApiClient withUsernamePassword(String baseUrl, String authUrl, String username, String password) Creates a new REST API client with the specified base URL and username/password authentication with JWT token. This is a convenience method that creates a username/password authentication manager.- Parameters:
baseUrl- The base URL of the APIauthUrl- The URL to use for authenticationusername- The username to use for authenticationpassword- The password to use for authentication- Returns:
- A new REST API client
-
withBasicAuth
Creates a new REST API client with the specified base URL and HTTP Basic Authentication. This is a convenience method that creates a basic auth authentication manager.- Parameters:
baseUrl- The base URL of the APIusername- The username to use for authenticationpassword- The password to use for authentication- Returns:
- A new REST API client
-
get
public <T> Optional<T> get(String endpoint, Class<T> responseType) throws DAValidateException, org.springframework.web.client.RestClientException Makes a GET request to the specified endpoint.- Type Parameters:
T- The type of the response- Parameters:
endpoint- The API endpointresponseType- The type of the response- Returns:
- An Optional containing the response
- Throws:
DAValidateException- if authentication failsorg.springframework.web.client.RestClientException- if the request fails
-
get
public <T> Optional<T> get(String endpoint, Map<String, Object> queryParams, Class<T> responseType) throws DAValidateException, org.springframework.web.client.RestClientExceptionMakes a GET request to the specified endpoint with query parameters.- Type Parameters:
T- The type of the response- Parameters:
endpoint- The API endpointqueryParams- The query parameters to add to the URLresponseType- The type of the response- Returns:
- An Optional containing the response
- Throws:
DAValidateException- if authentication failsorg.springframework.web.client.RestClientException- if the request fails
-
post
public <R> Optional<R> post(String endpoint, Class<R> responseType) throws DAValidateException, org.springframework.web.client.RestClientException Makes a POST request to the specified endpoint with no request body.- Type Parameters:
R- The type of the response- Parameters:
endpoint- The API endpointresponseType- The type of the response- Returns:
- An Optional containing the response
- Throws:
DAValidateException- if authentication failsorg.springframework.web.client.RestClientException- if the request fails
-
post
public <T,R> Optional<R> post(String endpoint, T requestBody, Class<R> responseType) throws DAValidateException, org.springframework.web.client.RestClientException Makes a POST request to the specified endpoint with a request body.- Type Parameters:
T- The type of the request bodyR- The type of the response- Parameters:
endpoint- The API endpointrequestBody- The request bodyresponseType- The type of the response- Returns:
- An Optional containing the response
- Throws:
DAValidateException- if authentication failsorg.springframework.web.client.RestClientException- if the request fails
-
put
public <T,R> Optional<R> put(String endpoint, T requestBody, Class<R> responseType) throws DAValidateException, org.springframework.web.client.RestClientException Makes a PUT request to the specified endpoint with a request body.- Type Parameters:
T- The type of the request bodyR- The type of the response- Parameters:
endpoint- The API endpointrequestBody- The request bodyresponseType- The type of the response- Returns:
- An Optional containing the response
- Throws:
DAValidateException- if authentication failsorg.springframework.web.client.RestClientException- if the request fails
-
patch
public <T,R> Optional<R> patch(String endpoint, T requestBody, Class<R> responseType) throws DAValidateException, org.springframework.web.client.RestClientException Makes a PATCH request to the specified endpoint with a request body.- Type Parameters:
T- The type of the request bodyR- The type of the response- Parameters:
endpoint- The API endpointrequestBody- The request bodyresponseType- The type of the response- Returns:
- An Optional containing the response
- Throws:
DAValidateException- if authentication failsorg.springframework.web.client.RestClientException- if the request fails
-
delete
public boolean delete(String endpoint) throws DAValidateException, org.springframework.web.client.RestClientException Makes a DELETE request to the specified endpoint.- Parameters:
endpoint- The API endpoint- Returns:
- true if the request was successful
- Throws:
DAValidateException- if authentication failsorg.springframework.web.client.RestClientException- if the request fails
-
delete
public boolean delete(String endpoint, Map<String, Object> queryParams) throws DAValidateException, org.springframework.web.client.RestClientExceptionMakes a DELETE request to the specified endpoint with query parameters.- Parameters:
endpoint- The API endpointqueryParams- The query parameters to add to the URL- Returns:
- true if the request was successful
- Throws:
DAValidateException- if authentication failsorg.springframework.web.client.RestClientException- if the request fails
-
getAuthManager
Gets the authentication manager used by this client.- Returns:
- The authentication manager
-