package com.kodcu.rapid.path;

import com.kodcu.rapid.config.DockerClient;

import javax.json.JsonObject;
import javax.ws.rs.*;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Response;
import java.io.IOException;
import java.util.Objects;
import java.util.concurrent.ExecutionException;

/**
 * Created by Hakan on 2/12/2016.
 */
@Path("networks")
public class Network extends DockerClient {

    @GET
    public String getNetwork(
            @QueryParam("filters") String filters)
            throws IOException, ExecutionException, InterruptedException {

        WebTarget target = resource().path("networks");
        if (Objects.nonNull(filters))
            target = target.queryParam("filters", filters);

        Response response = getResponse(target);
        String entity = response.readEntity(String.class);
        response.close();
        return entity;
    }

    @GET
    @Path("{id}")
    public String inspectNetwork(
            @PathParam("id") String name)
            throws IOException, ExecutionException, InterruptedException {

        WebTarget target = resource().path("networks").path(name);

        Response response = getResponse(target);
        String entity = response.readEntity(String.class);
        response.close();
        return entity;
    }

    @DELETE
    @Path("{id}")
    public String deleteNetwork(
            @PathParam("id") String name)
            throws IOException, ExecutionException, InterruptedException {

        WebTarget target = resource().path("networks").path(name);

        Response response = deleteResponse(target);
        String entity = response.readEntity(String.class);
        response.close();
        return entity;
    }

    @POST
    @Path("{id}/connect")
    public String connect2Network(
            @PathParam("id") String id,
            JsonObject content)
            throws IOException, ExecutionException, InterruptedException {

        WebTarget target = resource().path("networks").path(id).path("connect");

        Response response = postResponse(target, content);
        String entity = response.readEntity(String.class);
        response.close();
        return entity;
    }

    @POST
    @Path("{id}/disconnect")
    public String disconnect2Network(
            @PathParam("id") String id,
            JsonObject content)
            throws IOException, ExecutionException, InterruptedException {

        WebTarget target = resource().path("networks").path(id).path("disconnect");

        Response response = postResponse(target, content);
        String entity = response.readEntity(String.class);
        response.close();
        return entity;
    }

    @POST
    @Path("create")
    public String createNetwork(JsonObject content)
            throws IOException, ExecutionException, InterruptedException {

        WebTarget target = resource().path("networks").path("create");

        Response response = postResponse(target, content);
        String entity = response.readEntity(String.class);
        response.close();
        return entity;
    }
}

@@ -118,17 +118,157 @@
 x.ws.rs.
-*
+DELETE;%0Aimport javax.ws.rs.GET;%0Aimport javax.ws.rs.POST;%0Aimport javax.ws.rs.Path;%0Aimport javax.ws.rs.PathParam;%0Aimport javax.ws.rs.QueryParam
 ;%0Aimport
@@ -1114,36 +1114,34 @@
 am(%22id%22) String 
-name
+id
 )%0A            th
@@ -1254,36 +1254,34 @@
 networks%22).path(
-name
+id
 );%0A%0A        Resp
@@ -1531,20 +1531,18 @@
  String 
-name
+id
 )%0A      
@@ -1675,12 +1675,10 @@
 ath(
-name
+id
 );%0A%0A
@@ -1836,32 +1836,528 @@
  entity;%0A    %7D%0A%0A
+    @POST%0A    @Path(%22prune%22)%0A    public String pruneNetwork(@QueryParam(%22filters%22) String filters)%0A            throws IOException, ExecutionException, InterruptedException %7B%0A%0A        WebTarget target = resource().path(%22networks%22);%0A%0A        if (Objects.nonNull(filters))%0A            target = target.queryParam(%22filters%22, filters);%0A%0A        Response response = postResponse(target);%0A        String entity = response.readEntity(String.class);%0A        response.close();%0A        return entity;%0A    %7D%0A%0A
     @POST%0A    @P
@@ -2397,25 +2397,26 @@
 ring connect
-2
+To
 Network(%0A   
@@ -2883,17 +2883,18 @@
 sconnect
-2
+To
 Network(

update network paths for v1.26