All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.mtnfog.idyl.ami.sdk.IdylAmiClient Maven / Gradle / Ivy

The newest version!
/* Copyright 2014-2015 Mountain Fog, Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */

package com.mtnfog.idyl.ami.sdk;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.mtnfog.idyl.ami.sdk.model.Entity;

/**
 * The IdylClient supports sending entity extraction requests to the
 * Idyl AMI instances.
 * @author mtnfog
 */
public class IdylAmiClient {

	private static final Logger LOGGER = LogManager.getLogger(IdylAmiClient.class);
	
	private static final int HTTP_OK = 200;
		
	/**
	 * The Idyl AMI REST endpoint.
	 */
	private String endpoint;
	
	/**
	 * Used for deserializing the JSON response from the Idyl AMI.
	 */
	private Gson gson;
	
	/**
	 * The username for the Idyl AMI user.
	 */
	private String userName;
	
	/**
	 * The password for the Idyl AMI user.
	 */
	private String password;
	
	/**
	 * Initialize a new Idyl client with the endpoint.
	 * @param endpoint The Idyl AMI endpoint.
	 */
	public IdylAmiClient(String endpoint, String userName, String password) {
		
		this.endpoint = endpoint;
		this.userName = userName;
		this.password = password;
		
		this.gson = new Gson();
		
	}
	
	/**
	 * Causes any integrations with batches whose length is greater than
	 * zero to be immediately processed.
	 * @throws IOException
	 */
	public void flush() throws IOException {
		
		// Add the operation to the endpoint.
		String url = endpoint + "/flush";		
				
		// Send it as a POST to Idyl AMI.
		HttpResponse response = executeHttpPostRequest(url);
		
		// Get the response code.
		int responseCode = response.getStatusLine().getStatusCode();
		
		// Check the response code.
		if(responseCode == HTTP_OK) {
			
			// Nothing to do.
			
		} else {
			
			// Something went wrong that the entities could not be extracted.
			// Check the Idyl AMI log for details.
			LOGGER.warn("Unable to flush the integration batches. Check the Idyl AMI log for details.");
			
		}
		
	}
	
	/**
	 * Get the current count of items in the batches for the integrations
	 * that support batching.
	 * @return
	 * @throws IOException
	 */
	public Map getBatchSizes() throws IOException {
		
		Map batchSizes = new HashMap();
		
		// Add the operation to the endpoint.
		String url = endpoint + "/batchsize";		
				
		// Send it as a POST to Idyl AMI.
		HttpResponse response = executeHttpGetRequest(url);
		
		// Get the response code.
		int responseCode = response.getStatusLine().getStatusCode();
		
		// Check the response code.
		if(responseCode == HTTP_OK) {
			
			// Read the response.
			BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
			 
			// Read the response as a string.
			String json = IOUtils.toString(rd);
						
			// Close the reader.
			rd.close();
			
			// Convert the json to an oject.
			batchSizes = gson.fromJson(json, new TypeToken>(){}.getType());
			
		} else {
			
			// Something went wrong that the entities could not be extracted.
			// Check the Idyl AMI log for details.
			LOGGER.warn("Unable to retrieve the current batch sizes. Check the Idyl AMI log for details.");
			
		}
		
		return batchSizes;
		
	}
	
	/**
	 * Extract entities from text.
	 * @param text The text to extract entities from.
	 * @return A list of Entity objects, or null if an error occurred.
	 * @throws IOException
	 */
	public List extractEntities(String text) throws IOException {
		
		return extractEntities(text, 0, false);
		
	}

	/**
	 * Extract entities from text.
	 * @param text The text to extract entities from.
	 * @param confidence The confidence threshold. Valid values are 0 to 100. Entities with a confidence value less than
	 * the supplied value will not be returned.
	 * @param skipIntegrations Controls if any enabled integrations are disabled during the processing of this request.
	 * @return A list of Entity objects, or null if an error occurred.
	 * @throws IOException
	 */
	public List extractEntities(String text, int confidence, boolean skipIntegrations) throws IOException {
			
		// The list of entities that will be returned.
		List entities = null;
		
		// Add the operation to the endpoint.
		String completeEndpoint = endpoint + "/extract?text=%s&confidence=%s&skipIntegrations=%s";				
			
		// UrlEncode the input sentence.
		final String encodedSentence = URLEncoder.encode(text, "UTF-8");
		
		// Format the inputs into the endpoint URL.
		String url = String.format(completeEndpoint, encodedSentence, confidence, String.valueOf(skipIntegrations));
		
		// Send it as a POST to Idyl AMI.
		HttpResponse response = executeHttpPostRequest(url);
	 
		// Get the response code.
		int responseCode = response.getStatusLine().getStatusCode();
		
		// Check the response code.
		if(responseCode == HTTP_OK) {
			
			// Read the response.
			BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
			 
			// Read the response as a string.
			String result = IOUtils.toString(rd);
						
			// Close the reader.
			rd.close();
			
			// Get the entities from the list.
			entities = gson.fromJson(result.toString(), new TypeToken>(){}.getType());
			
		} else {
			
			// Something went wrong that the entities could not be extracted.
			// Check the Idyl AMI log for details.
			LOGGER.warn("Unable to extract entities. Check the Idyl AMI log for details.");
			
		}
		
		// Return the entities if successful, null or otherwise.
		return entities;
		
	}
	
	private String generateHttpAuthorizationHeaderValue() {
		
		return "Basic " + Base64.encodeBase64String((userName + ":" + password).getBytes());
		
	}
	
	/**
	 * Execute a Http POST request.
	 * @param url The url to request.
	 * @return An @HttpResponse from the request.
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	private HttpResponse executeHttpPostRequest(String url) throws IOException {
		
		HttpClient client = HttpClientBuilder.create().build();
		
		HttpPost request = new HttpPost(url);
		request.addHeader("Accept", "application/json");
		request.addHeader("Authorization", generateHttpAuthorizationHeaderValue());
		
		return client.execute(request);

	}
	
	/**
	 * Execute a Http GET request.
	 * @param url The url to request.
	 * @return An @HttpResponse from the request.
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	private HttpResponse executeHttpGetRequest(String url) throws IOException {
		
		HttpClient client = HttpClientBuilder.create().build();
		
		HttpGet request = new HttpGet(url);
		request.addHeader("Accept", "application/json");
		request.addHeader("Authorization", generateHttpAuthorizationHeaderValue());
		
		return client.execute(request);

	}
	
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy