/*
 * Copyright © 2014 Cask Data, 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 co.cask.cdap.client;

import co.cask.cdap.api.service.Service;
import co.cask.cdap.api.service.ServiceSpecification;
import co.cask.cdap.api.service.http.HttpServiceHandlerSpecification;
import co.cask.cdap.api.service.http.ServiceHttpEndpoint;
import co.cask.cdap.client.config.ClientConfig;
import co.cask.cdap.client.exception.UnAuthorizedAccessTokenException;
import co.cask.cdap.client.util.RESTClient;
import co.cask.cdap.common.http.HttpMethod;
import co.cask.cdap.common.http.HttpResponse;
import co.cask.cdap.common.http.ObjectResponse;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;

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

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

  private final RESTClient restClient;
  private final ClientConfig config;

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

  /**
   * Gets a {@link ServiceSpecification} for a {@link Service}.
   * 
   * @param appId ID of the application that the service belongs to
   * @param serviceId ID of the service
   * @return {@link ServiceSpecification} representing the service.
   * @throws IOException if a network error occurred
   * @throws UnAuthorizedAccessTokenException if the request is not authorized successfully in the gateway server
   */
  public ServiceSpecification get(String appId, String serviceId) throws IOException, UnAuthorizedAccessTokenException {
    URL url = config.resolveURL(String.format("apps/%s/services/%s", appId, serviceId));
    HttpResponse response = restClient.execute(HttpMethod.GET, url, config.getAccessToken());
    return ObjectResponse.fromJsonBody(response, ServiceSpecification.class).getResponseObject();
  }

  /**
   * Gets a list of {@link ServiceHttpEndpoint} that a {@link Service} exposes.
   * @param appId ID of the application that the service belongs to
   * @param serviceId ID of the service
   * @return A list of {@link ServiceHttpEndpoint}
   * @throws IOException if a network error occurred.
   * @throws UnAuthorizedAccessTokenException if the request is not authorized successfully in the gateway server
   */
  public List<ServiceHttpEndpoint> getEndpoints(String appId, String serviceId)
    throws IOException, UnAuthorizedAccessTokenException {

    ServiceSpecification specification = get(appId, serviceId);
    List<ServiceHttpEndpoint> endpoints = Lists.newArrayList();
    for (HttpServiceHandlerSpecification handlerSpecification : specification.getHandlers().values()) {
      endpoints.addAll(handlerSpecification.getEndpoints());
    }
    return ImmutableList.copyOf(endpoints);
  }
}

@@ -892,16 +892,72 @@
 Config;%0A
+import co.cask.cdap.client.exception.NotFoundException;%0A
 import c
@@ -1011,32 +1011,32 @@
 TokenException;%0A
-
 import co.cask.c
@@ -1314,24 +1314,59 @@
 OException;%0A
+import java.net.HttpURLConnection;%0A
 import java.
@@ -2221,16 +2221,20 @@
 rviceId)
+%0A   
  throws 
@@ -2278,16 +2278,35 @@
 xception
+, NotFoundException
  %7B%0A    U
@@ -2478,24 +2478,192 @@
 essToken());
+%0A%0A    if (response.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) %7B%0A      throw new NotFoundException(%22application or service %22, appId + %22/%22 + serviceId);%0A    %7D
 %0A    return 
@@ -3245,32 +3245,32 @@
 ring serviceId)%0A
-
     throws IOExc
@@ -3309,16 +3309,35 @@
 xception
+, NotFoundException
  %7B%0A%0A    

Handle 404s