package de.retest.recheck.persistence;

import static de.retest.recheck.XmlTransformerUtil.getXmlTransformer;

import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.nio.file.Paths;

import de.retest.recheck.Properties;
import de.retest.recheck.auth.RetestAuthentication;
import de.retest.recheck.persistence.bin.KryoPersistence;
import de.retest.recheck.persistence.xml.XmlFolderPersistence;
import lombok.extern.slf4j.Slf4j;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

@Slf4j
public class CloudPersistence<T extends Persistable> implements Persistence<T> {
	private static final String SERVICE_ENDPOINT = "https://marvin.prod.cloud.retest.org/api/report";
	private final KryoPersistence<T> kryoPersistence = new KryoPersistence<>();
	private final XmlFolderPersistence<T> folderPersistence = new XmlFolderPersistence<>( getXmlTransformer() );
	private final OkHttpClient client = new OkHttpClient();

	public static final String RECHECK_API_KEY = "RECHECK_API_KEY";

	@Override
	public void save( final URI identifier, final T element ) throws IOException {
		kryoPersistence.save( identifier, element );
		if ( identifier.getPath().endsWith( Properties.AGGREGATED_TEST_REPORT_FILE_NAME ) ) {
			saveToCloud( identifier );
		}
	}

	private void saveToCloud( final URI identifier ) throws IOException {
		final Response uploadUrlResponse = getUploadUrl();

		if ( uploadUrlResponse.isSuccessful() ) {
			uploadReport( identifier, uploadUrlResponse.body().string() );
		}
	}

	private void uploadReport( final URI identifier, final String url ) throws IOException {
		final Request uploadRequest = new Request.Builder() //
				.url( url ) //
				.put( RequestBody.create( null, new File( identifier ) ) ) //
				.addHeader( "x-amz-meta-report-name", Paths.get( identifier ).getFileName().toString() ) //
				.build();

		final Response uploadResponse = client.newCall( uploadRequest ).execute();

		if ( uploadResponse.isSuccessful() ) {
			log.info( "Successfully uploaded report to rehub" );
		}
	}

	private Response getUploadUrl() throws IOException {
		final RetestAuthentication auth = RetestAuthentication.getInstance();
		final String token = String.format( "Bearer %s", auth.getAccessTokenString() );

		final Request uploadUrlRequest = new Request.Builder() //
				.url( SERVICE_ENDPOINT ) //
				.post( RequestBody.create( null, "" ) ) //
				.addHeader( "Authorization", token ) //
				.build();

		return client.newCall( uploadUrlRequest ).execute();
	}

	@Override
	public T load( final URI identifier ) throws IOException {
		if ( Paths.get( identifier ).toFile().isDirectory() ) {
			return folderPersistence.load( identifier );
		}
		return kryoPersistence.load( identifier );
	}

}

@@ -424,139 +424,96 @@
 ort 
-lombok.extern.slf4j.Slf4j;%0Aimport okhttp3.OkHttpClient;%0Aimport okhttp3.Request;%0Aimport okhttp3.RequestBody;%0Aimport okhttp3.Response
+kong.unirest.HttpResponse;%0Aimport kong.unirest.Unirest;%0Aimport lombok.extern.slf4j.Slf4j
 ;%0A%0A@
@@ -888,65 +888,8 @@
 ) );
-%0A%09private final OkHttpClient client = new OkHttpClient();
 %0A%0A%09p
@@ -1291,32 +1291,36 @@
 n %7B%0A%09%09final 
+Http
 Response
  uploadUrlRe
@@ -1303,24 +1303,32 @@
 HttpResponse
+%3CString%3E
  uploadUrlRe
@@ -1379,35 +1379,32 @@
 sponse.isSuccess
-ful
 () ) %7B%0A%09%09%09upload
@@ -1445,23 +1445,17 @@
 nse.
-b
+getB
 ody()
-.string()
  );%0A
@@ -1564,136 +1564,57 @@
 nal 
-Request uploadRequest = new Request.Builder() //%0A%09%09%09%09.url( url ) //%0A%09%09%09%09.put( RequestBody.create( null, new File( identifier ) )
+HttpResponse%3C?%3E uploadResponse = Unirest.put( url
  ) /
@@ -1612,36 +1612,33 @@
 ( url ) //%0A%09%09%09%09.
-addH
+h
 eader( %22x-amz-me
@@ -1708,100 +1708,71 @@
 () )
- //
 %0A%09%09%09%09.
-build();%0A%0A%09%09final Response uploadResponse = client.newCall( uploadRequest ).execute
+field( %22upload%22, new File( identifier ) ) //%0A%09%09%09%09.asEmpty
 ();%0A
@@ -1803,19 +1803,16 @@
 sSuccess
-ful
 () ) %7B%0A%09
@@ -1883,24 +1883,28 @@
 private 
+Http
 Response
  getUplo
@@ -1895,16 +1895,24 @@
 Response
+%3CString%3E
  getUplo
@@ -2101,154 +2101,57 @@
 %0A%0A%09%09
-final Request uploadUrlRequest = new Request.Builder() //%0A%09%09%09%09.url( SERVICE_ENDPOINT ) //%0A%09%09%09%09.post( RequestBody.create( null, %22%22 ) ) //%0A%09%09%09%09.addH
+return Unirest.post( SERVICE_ENDPOINT ) //%0A%09%09%09%09.h
 eade
@@ -2177,25 +2177,24 @@
  token )
- 
 //%0A%09%09%09%09.
 build();
@@ -2189,69 +2189,16 @@
 %09%09%09.
-build();%0A%0A%09%09return client.newCall( uploadUrlRequest ).execute
+asString
 ();%0A

Replace okhttp with unirest