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

com.mtnfog.idyl.e3.sdk.IdylE3ClientFactory Maven / Gradle / Ivy

There is a newer version: 2.3.1
Show newest version
/* Copyright 2016 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.e3.sdk;

import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;

import com.google.gson.Gson;

import retrofit.RequestInterceptor;
import retrofit.RestAdapter;
import retrofit.RestAdapter.Log;
import retrofit.converter.GsonConverter;

/**
 * Create Idyl E3 clients.
 * 
 * @author Mountain Fog, Inc.
 *
 */
public class IdylE3ClientFactory {

	private static final Logger LOGGER = LogManager.getLogger(IdylE3ClientFactory.class);
	
	private IdylE3ClientFactory() {
		
	}
	
	/**
	 * Gets an {@link IdylE3Client client}.
	 * @param endpoint The Idyl E3 API endpoint.
	 * @return A {@link IdylE3Client client}.
	 */
	public static IdylE3Client getIdylE3Client(String endpoint) {
	
		return getRestAdapter(endpoint).create(IdylE3Client.class);
		
	}
	
	public static IdylE3Client getIdylE3Client(String endpoint, String apiKey) {
		
		return getAuthenticatedRestAdapter(endpoint, apiKey).create(IdylE3Client.class);
		
	}
	
	private static RestAdapter getAuthenticatedRestAdapter(String endpoint, String apiKey) {
		
		RequestInterceptor requestInterceptor = new RequestInterceptor() {  
		    @Override
		    public void intercept(RequestFacade request) {
		        request.addHeader("Authorization", apiKey);
		        request.addHeader("Content-Type", "application/json");
		        request.addHeader("Accept", "application/json");
		    }
		};
		
		RestAdapter restAdapter = new RestAdapter.Builder()
				.setEndpoint(endpoint)			
				.setConverter(new GsonConverter(new Gson()))
				.setLogLevel(RestAdapter.LogLevel.FULL) 
				.setRequestInterceptor(requestInterceptor)
				.setLog(new Log() {

					@Override
					public void log(String message) {						
						LOGGER.debug(message);						
					}
					
				})
				.build();
		
		return restAdapter;
		
	}
	
	private static RestAdapter getRestAdapter(String endpoint) {
		
		RestAdapter restAdapter = new RestAdapter.Builder()
				.setEndpoint(endpoint)			
				.setConverter(new GsonConverter(new Gson()))
				.setLogLevel(RestAdapter.LogLevel.FULL) 
				.setLog(new Log() {

					@Override
					public void log(String message) {						
						LOGGER.debug(message);						
					}
					
				})
				.build();
		
		return restAdapter;
		
	}
	
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy