/*
 * Copyright 2012-2014 Continuuity, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package com.continuuity.client;

import com.continuuity.client.config.ReactorClientConfig;
import com.continuuity.client.util.RESTClient;
import com.continuuity.common.http.HttpMethod;
import com.continuuity.common.http.HttpResponse;
import com.continuuity.common.http.ObjectResponse;
import com.continuuity.proto.ServiceMeta;

import java.io.IOException;
import java.net.URL;
import javax.inject.Inject;

/**
 * Provides ways to interact with Reactor User Services.
 */
public class ServiceClient {

  private final RESTClient restClient;
  private final ReactorClientConfig config;

  @Inject
  public ServiceClient(ReactorClientConfig config) {
    this.config = config;
    this.restClient = RESTClient.create(config);
  }

  /**
   * Gets information about a service.
   * 
   * @param appId ID of the application that the service belongs to
   * @param serviceId ID of the service
   * @return {@link ServiceMeta} representing the service.
   * @throws IOException if a network error occurred
   */
  public ServiceMeta get(String appId, String serviceId) throws IOException {
    URL url = config.resolveURL(String.format("apps/%s/services/%s", appId, serviceId));
    HttpResponse response = restClient.execute(HttpMethod.GET, url);
    return ObjectResponse.fromJsonBody(response, ServiceMeta.class).getResponseObject();
  }
}

@@ -927,56 +927,330 @@
 ta;%0A
-%0Aimport java.io.IOException;%0Aimport java.net.URL
+import com.google.gson.JsonArray;%0Aimport com.google.gson.JsonElement;%0Aimport com.google.gson.JsonObject;%0Aimport com.google.gson.JsonParser;%0Aimport org.apache.twill.discovery.Discoverable;%0A%0Aimport java.io.IOException;%0Aimport java.net.InetSocketAddress;%0Aimport java.net.URL;%0Aimport java.util.ArrayList;%0Aimport java.util.List
 ;%0Aim
@@ -2203,11 +2203,986 @@
 t();%0A  %7D
+%0A%0A  public List%3CDiscoverable%3E discover(String appId, String serviceId, final String discoverableId) throws IOException %7B%0A    URL url = config.resolveURL(String.format(%22apps/%25s/services/%25s/discover/%25s%22, appId, serviceId, discoverableId));%0A    HttpResponse response = restClient.execute(HttpMethod.GET, url);%0A    List%3CDiscoverable%3E discoverables = new ArrayList%3CDiscoverable%3E();%0A    JsonParser parser = new JsonParser();%0A    JsonArray array = (JsonArray) parser.parse(response.getResponseBodyAsString());%0A%0A    for (JsonElement element: array) %7B%0A      final JsonObject object = element.getAsJsonObject();%0A      discoverables.add(new Discoverable() %7B%0A        @Override%0A        public String getName() %7B%0A          return discoverableId;%0A        %7D%0A%0A        @Override%0A        public InetSocketAddress getSocketAddress() %7B%0A          return new InetSocketAddress(object.get(%22host%22).getAsString(), object.get(%22port%22).getAsInt());%0A        %7D%0A      %7D);%0A    %7D%0A    return discoverables;%0A  %7D
 %0A%7D%0A

Add discover method to serviceClient