/*******************************************************************************
 * Copyright (c) 2015 University of York.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Antonio Garcia-Dominguez - initial API and implementation
 *******************************************************************************/
package uk.ac.york.mondo.integration.api.utils;

import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.thrift.protocol.TTupleProtocol;
import org.apache.thrift.transport.THttpClient;
import org.apache.thrift.transport.TTransportException;

import uk.ac.york.mondo.integration.api.Hawk;

/**
 * Utility methods for connecting to the MONDO APIs. These use the optional
 * dependency on Apache HTTP Components.
 */
public class APIUtils {
	private APIUtils() {
	}

	public static Hawk.Client connectToHawk(String url) throws TTransportException {
		final HttpClient httpClient = APIUtils. createGZipAwareHttpClient();
		final THttpClient transport = new THttpClient(url, httpClient);
		return new Hawk.Client(new TTupleProtocol(transport));
	}

	@SuppressWarnings("restriction")
	private static HttpClient createGZipAwareHttpClient() {
		return HttpClientBuilder.create()
				.addInterceptorFirst(new GZipRequestInterceptor())
				.addInterceptorFirst(new GZipResponseInterceptor()).build();
	}

}

@@ -577,16 +577,107 @@
 utils;%0A%0A
+import java.lang.reflect.Constructor;%0Aimport java.lang.reflect.InvocationTargetException;%0A%0A
 import o
@@ -702,32 +702,32 @@
 ent.HttpClient;%0A
-
 import org.apach
@@ -764,16 +764,57 @@
 uilder;%0A
+import org.apache.thrift.TServiceClient;%0A
 import o
@@ -1264,16 +1264,185 @@
 ption %7B%0A
+%09%09return connectTo(Hawk.Client.class, url);%0A%09%7D%0A%09%0A%09public static %3CT extends TServiceClient%3E T connectTo(Class%3CT%3E clazz, String url) throws TTransportException %7B%0A%09%09try %7B%0A%09
 %09%09final 
@@ -1506,16 +1506,17 @@
 nt();%0A%09%09
+%09
 final TH
@@ -1577,62 +1577,405 @@
 ;%0A%09%09
-return new Hawk.Client(new TTupleProtocol(transport));
+%09Constructor%3CT%3E constructor = clazz.getDeclaredConstructor(org.apache.thrift.protocol.TProtocol.class);%0A%09%09%09return constructor.newInstance(new TTupleProtocol(transport));%0A%09%09%7D catch (InstantiationException %0A%09%09%09%09%7C IllegalAccessException %0A%09%09%09%09%7C IllegalArgumentException %0A%09%09%09%09%7C InvocationTargetException %0A%09%09%09%09%7C NoSuchMethodException%0A%09%09%09%09%7C SecurityException e) %7B%0A%09%09%09throw new TTransportException(e);%0A%09%09%7D
 %0A%09%7D%0A

convert connectToHawk to a generic method