package com.ctrip.zeus.client;

import com.ctrip.zeus.model.entity.GroupServerStatus;
import com.ctrip.zeus.model.entity.GroupStatus;
import com.ctrip.zeus.model.transform.DefaultJsonParser;
import jersey.repackaged.com.google.common.cache.CacheBuilder;
import jersey.repackaged.com.google.common.cache.CacheLoader;
import jersey.repackaged.com.google.common.cache.LoadingCache;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

/**
 * Created by fanqq on 2015/5/20.
 */
public class StatusClient extends AbstractRestClient {
    private static LoadingCache<String,StatusClient> cache = CacheBuilder.newBuilder().maximumSize(20)
            .expireAfterAccess(30, TimeUnit.MINUTES)
            .build(new CacheLoader<String, StatusClient>() {
                       @Override
                       public StatusClient load(String url) throws Exception {
                           return new StatusClient(url);
                       }
                   }
            );

    protected StatusClient(String url) {
        super(url);
    }

    public static StatusClient getClient(String url) throws ExecutionException {
        return cache.get(url);
    }

    public GroupServerStatus getGroupServerStatus(Long groupId , Long slbId , String sip)throws Exception
    {
        String responseStr = getTarget().path("/api/status/group/"+groupId+"/slb/"+slbId+"/server/"+sip)
                .request().headers(getDefaultHeaders()).get(String.class);
        return DefaultJsonParser.parse(GroupServerStatus.class, responseStr);
    }

    public GroupStatus getGroupStatus(Long groupId , Long slbId)throws Exception
    {
        String responseStr = getTarget().path("/api/status/group?groupId="+groupId+"&slbId="+slbId)
                .request().headers(getDefaultHeaders()).get(String.class);
        return DefaultJsonParser.parse(GroupStatus.class, responseStr);
    }
}

@@ -373,16 +373,50 @@
 Cache;%0A%0A
+import javax.ws.rs.core.Response;%0A
 import j
@@ -1682,32 +1682,198 @@
 Exception%0A    %7B%0A
+//        String res = getTarget().path(%22/api/status/group%22).queryParam(%22groupId%22,groupId).queryParam(%22slbId%22, slbId)%0A//                .request().get(String.class);%0A
         String r
@@ -1924,37 +1924,61 @@
 roup
-?
+%22).queryParam(%22
 groupId
-=%22+
+%22,
 groupId
-+%22&
+).queryParam(%22
 slbId
-=%22+
+%22, 
 slbI

update status client