package de.simonscholz.retrofit.app.job;

import java.io.IOException;
import java.util.List;
import java.util.function.Consumer;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.e4.ui.di.UISynchronize;

import de.simonscholz.retrofit.model.Contributor;
import de.simonscholz.retrofit.model.GitHub;
import retrofit2.Call;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;



public class JsonContributorsJob extends Job {
	
	public static final String API_URL = "https://api.github.com";
	
	private Consumer<List<Contributor>> contributorConsumer;

	private UISynchronize uiSynchronize;

	public JsonContributorsJob(Consumer<List<Contributor>> contributorConsumer, UISynchronize uiSynchronize) {
		super("Fetch Eclipse Contributors from Eclipse Platform");
		this.contributorConsumer = contributorConsumer;
		this.uiSynchronize = uiSynchronize;
	}

	@Override
	protected IStatus run(IProgressMonitor monitor) {
		
		// Create a very simple REST adapter which points the GitHub API.
		Retrofit retrofit = new Retrofit.Builder().baseUrl(API_URL).addConverterFactory(GsonConverterFactory.create())
				.build();

		// Create an instance of our GitHub API interface.
		GitHub github = retrofit.create(GitHub.class);

		// Create a call instance for looking up Eclipse platform.ui contributors.
		Call<List<Contributor>> call = github.contributors("eclipse", "eclipse.platform.ui");

		try {
			List<Contributor> contributors = call.execute().body();
			uiSynchronize.asyncExec(new Runnable() {
				
				@Override
				public void run() {
					// pass the list of contributors to the consumer
					contributorConsumer.accept(contributors);
				}
			});
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return Status.OK_STATUS;
	}

}

@@ -338,16 +338,91 @@
 hronize;
+%0Aimport org.osgi.framework.Bundle;%0Aimport org.osgi.framework.FrameworkUtil;
 %0A%0Aimport
@@ -615,18 +615,16 @@
 ctory;%0A%0A
-%0A%0A
 public c
@@ -662,18 +662,17 @@
 s Job %7B%0A
-%09
 %0A
+
 %09public 
@@ -727,18 +727,17 @@
 b.com%22;%0A
-%09
 %0A
+
 %09private
@@ -1149,19 +1149,17 @@
 itor) %7B%0A
-%09%09
 %0A
+
 %09%09// Cre
@@ -1511,16 +1511,21 @@
 tform.ui
+%0A%09%09//
  contrib
@@ -1735,13 +1735,9 @@
 ) %7B%0A
-%09%09%09%09
 %0A
+
 %09%09%09%09
@@ -1919,35 +1919,147 @@
 %0A%09%09%09
-e.printStackTrace(
+Bundle bundle = FrameworkUtil.getBundle(getClass());%0A%09%09%09return new Status(IStatus.ERROR, bundle.getSymbolicName(), e.getMessage(), e
 );%0A%09%09%7D%0A
-%09%09
 %0A%09%09r

Return an error IStatus in case of an Exception