Call API [Handling]

Call API

It is divided into the following four parts. (Forgive me for the XX edition) Call API [Preparation] Call API [Core] Call API [Handling] Call API [Call]

Handling edition

Since I actually hit the API at the core layer, In this hierarchy, we will handle the errors that appear.

The error here is not only Exception It also includes the case where the processing result is invalid.

Facade.java


package facade;

import client.Client;
import exception.NantokaApiBadResultException;
import exception.NantokaApiUnknownException;
import model.RequestDto;
import model.ResponseDto;

public class Facade {
	
	/**Processing result code normal*/
	private static final String SUCCESS = "0";
	
	/**API client*/
	private Client client = new Client();

	/**
	 *Get the result of executing the API
	 * 
	 * @param request request DTO
	 * @return response DTO
	 * @throws NantokaApiUnknownException When a communication error or result does not exist
	 * @throws NantokaApiBadResultException When processing result is invalid
	 */
	public ResponseDto nantokaApiFacade(RequestDto request)
			throws NantokaApiUnknownException, NantokaApiBadResultException {

		ResponseDto response = new ResponseDto();

		try {
			response = client.nantokaApiClient(request);

		} catch (Exception e) {
			//In case of communication error
			throw new NantokaApiUnknownException(e.getMessage());
		}

		if (response == null) {
			throw new NantokaApiUnknownException("I didn't get anything back");
		}
		
		if (!SUCCESS.equals(response.getResultCode())) {
			throw new NantokaApiBadResultException("Illegal processing result");
		}

		return response;
	}
}

Even in the case of communication error type Exception, even if the processing result is null for some reason I also tried to return with ʻUnknownException`

Well, I don't really understand this feeling, so I'd like to summarize it. Even if the processing result is invalid, it is like an error from here, so I returned it as an Exception

At this point, it can be said that the function to call the API is completed. All you have to do is call this one, so let's actually use it next time.

Recommended Posts

Call API [Handling]
Call API [Call]
Call API [Preparation]
Call API [Core Edition]
Call TensorFlow Java API from Scala
Rails API
Call GitHub's Rest API from Java's Socket API
Call GitHub API from Java Socket API part2
Exception handling
Exception handling Exception
I want to separate the handling of call results according to the API caller (call trigger)