[JAVA] Call API [Core Edition]

Call API

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

Core edition

Implement the core part that actually connects POST transmission to API

client.java


package client;

import javax.xml.ws.http.HTTPException;

import json.JsonUtil;
import model.RequestDto;
import model.ResponseDto;

public class Client {
	
	/**
	 *Run the API
	 * 
	 * @param request request DTO
	 * @return response DTO
	 */
	public ResponseDto nantokaApiClient(RequestDto request) {
		
		String requestJson = JsonUtil.requestDtoToJson(request);
		
		String response = post(requestJson, "http://nantoka.com");
		
		return JsonUtil.responseJsonToDto(response);
	}
	
	/**
	 *POST processing
	 * 
	 * @param requestJson Json Translated request
	 * @param uri destination URI
	 * @return response Json
	 * @throws communication error system
	 */
	private final String post(String requestJson, String uri) {
		
		try {
			//Something like a library for POST
			return "ClientBuilder.newClient().path(uri).request().post(requestJson);";
		} catch (HTTPException e) {
			throw e;
		}
	}
}

The actual ClientBuilder library inside the post method is omitted this time Because I want to handle more by processing the hierarchy of the surface part In the core layer like this time, all communication Exceptions are thrown as they are

All you're doing is throwing a request and receiving a response, that's it. I'm just communicating and using external methods When communicating, it is converted to Json format and sent, and the returned Json result is converted to DTO and returned.

Recommended Posts

Call API [Core Edition]
Call API [Call]
Call API [Preparation]
Call API [Handling]
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