package com.ibm.streamsx.rest.build;

// TODO pare down these imports
import static com.ibm.streamsx.rest.build.StreamsRestUtils.requestGsonResponse;

import java.io.File;
import java.io.IOException;

import java.util.Collections;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AUTH;
import org.apache.http.client.fluent.Executor;
import org.apache.http.client.fluent.Request;
import org.apache.http.client.fluent.Response;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.FileEntity;
import org.apache.http.entity.StringEntity;
import org.apache.http.util.EntityUtils;

import com.google.gson.JsonObject;

import com.ibm.streamsx.rest.RESTException;
import com.ibm.streamsx.rest.internal.DirectoryZipInputStream;
import com.ibm.streamsx.topology.internal.context.remote.SubmissionResultsKeys;

class StreamsRestActions {
	

  static boolean deleteToolkit(Toolkit toolkit) throws IOException
  {
    Request deleteToolkit = Request.Delete(toolkit.self());
    Response response = toolkit.connection().getExecutor().execute(deleteToolkit);
    HttpResponse httpResponse = response.returnResponse();
    int statusCode = httpResponse.getStatusLine().getStatusCode();
    if (statusCode == 204)
      return true;
    if (statusCode == 404)
      return false;
    String message = EntityUtils.toString(httpResponse.getEntity());
    throw RESTException.create(statusCode, message);
  }

  static Toolkit uploadToolkit(StreamsBuildService connection, File path) throws IOException {
    // Make sure it is a directory
    if (! path.isDirectory()) {
      throw new IllegalArgumentException("The specified toolkit path '" + path.toString() + "' is not a directory.");
    }

    // Make sure it contains toolkit.xml
    File toolkit = new File(path, "toolkit.xml");
    if (! toolkit.isFile()) {
      throw new IllegalArgumentException("The specified toolkit path '" + path.toString() + "' is not a toolkit.");
    }

    String toolkitsURL = connection.getToolkitsURL();
    Request post = Request.Post(toolkitsURL);
    post.addHeader(AUTH.WWW_AUTH_RESP, connection.getAuthorization());
    post.bodyStream(DirectoryZipInputStream.fromPath(path.toPath()), ContentType.create("application/zip"));

    JsonObject response = requestGsonResponse(connection.getExecutor(), post);
    List<Toolkit> toolkitList = Toolkit.createToolkitList(connection, response);

    // We expect a list of zero or one element.
    if (toolkitList.size() == 0) {
      return null;
    }
    return toolkitList.get(0);
  }
}

@@ -35,121 +35,29 @@
 d;%0A%0A
-// TODO pare down these imports%0Aimport static com.ibm.streamsx.rest.build.StreamsRestUtils.requestGsonRespons
+import java.io.Fil
 e;%0A
-%0A
 impo
@@ -59,36 +59,43 @@
 %0Aimport java.io.
-File
+IOException
 ;%0Aimport java.io
@@ -100,50 +100,49 @@
 io.I
-OException;%0A%0Aimport java.util.Collections;
+nputStreamReader;%0Aimport java.io.Reader;%0A
 %0Aimp
@@ -226,32 +226,67 @@
 p.HttpResponse;%0A
+import org.apache.http.HttpStatus;%0A
 import org.apach
@@ -490,94 +490,8 @@
 pe;%0A
-import org.apache.http.entity.FileEntity;%0Aimport org.apache.http.entity.StringEntity;%0A
 impo
@@ -528,16 +528,45 @@
 Utils;%0A%0A
+import com.google.gson.Gson;%0A
 import c
@@ -703,88 +703,8 @@
 eam;
-%0Aimport com.ibm.streamsx.topology.internal.context.remote.SubmissionResultsKeys;
 %0A%0Acl
@@ -1097,11 +1097,32 @@
  == 
-204
+HttpStatus.SC_NO_CONTENT
 )%0A  
@@ -1164,11 +1164,31 @@
  == 
-404
+HttpStatus.SC_NOT_FOUND
 )%0A  
@@ -2148,26 +2148,24 @@
 );%0A%0A    
-JsonObject
+Response
  respons
@@ -2172,61 +2172,766 @@
 e = 
-requestGsonResponse(connection.getExecutor(), post
+connection.getExecutor().execute(post);%0A    HttpResponse httpResponse = response.returnResponse();%0A    int statusCode = httpResponse.getStatusLine().getStatusCode();%0A    // TODO The API is supposed to return CREATED, but there is a bug and it%0A    // returns OK.  When the bug is fixed, change this to accept only OK%0A    if (statusCode != HttpStatus.SC_CREATED && statusCode != HttpStatus.SC_OK) %7B%0A      String message = EntityUtils.toString(httpResponse.getEntity());%0A      throw RESTException.create(statusCode, message);%0A    %7D%0A    %0A    HttpEntity entity = httpResponse.getEntity();%0A    try (Reader r = new InputStreamReader(entity.getContent())) %7B%0A      JsonObject jresponse = new Gson().fromJson(r, JsonObject.class);%0A      EntityUtils.consume(entity
 );%0A
+  
     
@@ -2996,16 +2996,17 @@
 ection, 
+j
 response
@@ -3005,24 +3005,26 @@
 response);%0A%0A
+  
     // We ex
@@ -3059,24 +3059,26 @@
 lement.%0A    
+  
 if (toolkitL
@@ -3098,24 +3098,26 @@
  0) %7B%0A      
+ 
+ 
 return null;
@@ -3117,26 +3117,30 @@
 n null;%0A    
-%7D%0A
+  %7D%0A  
     return t
@@ -3158,14 +3158,20 @@
 get(0);%0A
+    %7D%0A
   %7D%0A%7D%0A

Check return status when posting toollkit.