Call API [Call]

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]

Call edition

Up to the last time, I was able to make it so that if you call Facade, it will do all the API related processing. Let's call Facade

service.java


package service;

import exception.NantokaApiBadResultException;
import exception.NantokaApiUnknownException;
import facade.Facade;
import model.RequestBody;
import model.RequestDto;
import model.ResponseBody;
import model.ResponseDto;

public class Service {
	
	/** APIFacade */
	private static Facade facade = new Facade();
	
	public static void main(String[] args) {
		System.out.println(service(""));
	}

	public static String service(String str) {
		
		//logic
		
		RequestBody body = new RequestBody();
		body.setId("28");
		body.setName("Somehow");
		body.setInfo("I'm hungry");
		
		RequestDto request = new RequestDto();
		request.setRequest(body);
		
		try {
			//API execution
			ResponseDto response = facade.nantokaApiFacade(request);
			ResponseBody responseBody = response.getBody();
			return responseBody.getInfo() + responseBody.getComment();
			
		} catch (NantokaApiUnknownException e) {
			return e.getMessage();
			
		} catch (NantokaApiBadResultException e) {
			return e.getMessage();
		}
	}
}

The error is simple and nice It is possible to combine the case of Exception in Facade and the case where the processing result is null into one error. After that, I am taking an error when the result is not normal If you want to divide the processing according to the type of result, you can handle it by increasing Exception and branching.

Summary

★ Client (core layer) just hits the API ★ Make Exceptions spit out as you like in Facade (handling layer) ★ Implemented processing when branching in Service (call layer)

Isn't it pretty beautiful!

Recommended Posts

Call API [Call]
Call API [Handling]
Call API [Core Edition]
Rails API
Call TensorFlow Java API from Scala
Call GitHub's Rest API from Java's Socket API
Call GitHub API from Java Socket API part2
Call the Windows Notification API in Java
Stream API memo
ruby API problem
Chew about API
Java Stream API
[Ruby] Ruby API problem
ruby API problem
Stream API basics
Call Amazon Product Advertising API 5.0 (PA-API v5) in Java