I have a class that is responsible for calling a service and returning a response.
The contract is like this
interface ServiceClient {
ServiceResponse process(ServiceSubmissionParams params);
}
ServiceSubmissionParams
and ServiceResponse
are simple POJOs that are part of the application calling the service (and not part of Service API).
ServiceSubmissionParams
has a bunch of data members that are needed to call an API of the service and ServiceResponse
has data that is interpreted from the service response.
The implementation class is like this
class ServiceClientImpl implements ServiceClient {
private Client client; //JAXRS client
ServiceResponse process(ServiceSubmissionParams params) {
//1
HttpHeaders headers = createHeaders(params);
//2
Response response = //call service with the above headers
//3 - Build reponse
if (response.getStatusInfo().getFamily() == Response.Status.Family.SUCCESSFUL) {
// Build ServiceResponse
}
//4
throw handleErrorResponse(response);
}
HttpHeaders createHeaders(ServiceSubmissionParams params) {
//Builds HttpHeaders from ServiceSubmissionParams fields
}
RuntimeException handleACAPIErrorResponse(Response response) {
/*
Interprets the response to throw a (custom) subclass of RuntimeException
if response == 503, return ServiceUnavailableException
if response == 408, return RequestTimeoutException
....
if response == 500, return ServerException
else return UnknownException
*/
}
}
The reason for throwing specific (or different) RuntimeExceptions is that based on the type some gets retried (ServiceUnavailable or RequestTimeout) while others don't.
My question:
I feel that this class does a lot of work. Prepare headers, call service, interpret response. So, does it violate SRP? Or does it do only job considering the level of abstraction of taking a ServiceSubmissionParams
and returning a ServiceResponse
.
More than a violation of SRP, I need to change this class whenever I need to handle an error response from the service i.e, I need to read the response body in case of 500 or 400 response code and interpret it to populate a ServiceResponse
. So, should I consider splitting this class into multiple classes?