001package com.astrolabsoftware.FinkBrowser.FinkPortalClient;
002
003import com.Lomikel.Utils.SmallHttpClient;
004import com.Lomikel.Utils.LomikelException;
005
006// org.json
007import org.json.JSONArray;
008import org.json.JSONObject;
009
010// Log4J
011import org.apache.logging.log4j.Logger;
012import org.apache.logging.log4j.LogManager;
013
014/** <code>FPC</code> is a client for <a href="https://api.ztf.fink-portal.org/api">Fink Science Portal</a>.
015  * @opt attributes
016  * @opt operations
017  * @opt types
018  * @opt visibility
019  * @author <a href="mailto:Julius.Hrivnac@cern.ch">J.Hrivnac</a> */
020// BUG: make everything for ZTF/LSST
021public class FPC {
022  
023  /** Create for a survey.
024    * @param survey The survey: <em>ZTF</em> or <em>LSST</em>. */
025  public FPC(String survey) throws LomikelException {
026    switch (survey) {
027      case "ZTF":
028        _restUrl = "https://api.ztf.fink-portal.org/api/v1";
029        break;
030      case "LSST":
031        _restUrl = "https://api.lsst.fink-portal.org/api/v1";
032        break;
033      default:
034        throw new LomikelException("Unknown Classifier survey " + survey);
035      }  
036    }
037    
038  /** Call <em>Fink Science Portal <b>objects</b></em> Web Service.
039    * <a href="https://api.ztf.fink-portal.org/api">https://api.ztf.fink-portal.org/api/v1</a>.
040    * @param request  The requested formulated as {@link JSONObject}.
041    * @param endpoint The service endpoint.
042    * @return         The answer formulated as {@link JSONArray}.
043    * @throws LomikelException If call fails. */
044  public JSONArray objects(JSONObject request) throws LomikelException {
045    return call(request, OBJECTS_WS);
046    }
047     
048  /** Call <em>Fink Science Portal <b>latests</b></em> Web Service.
049    * <a href="https://api.ztf.fink-portal.org/api/v1/latests">https://api.ztf.fink-portal.org/api/v1/latests</a>.
050    * @param request  The requested formulated as {@link JSONObject}.
051    * @param endpoint The service endpoint.
052    * @return         The answer formulated as {@link JSONArray}.
053    * @throws LomikelException If call fails. */
054  public JSONArray latests(JSONObject request) throws LomikelException {
055    return call(request, LATESTS_WS);
056    }
057    
058  /** Call <em>Fink Science Portal <b>anomaly</b></em> Web Service.
059    * <a href="https://api.ztf.fink-portal.org/api/v1/anomaly">https://api.ztf.fink-portal.org/api/v1/anomaly</a>.
060    * @param request  The requested formulated as {@link JSONObject}.
061    * @param endpoint The service endpoint.
062    * @return         The answer formulated as {@link JSONArray}.
063    * @throws LomikelException If call fails. */
064  public JSONArray anomaly(JSONObject request) throws LomikelException {
065    return call(request, ANOMALY_WS);
066    }
067   
068  /** Call <em>Fink Science Portal</em> Web Service.
069    * @param request  The requested formulated as {@link JSONObject}.
070    * @param endpoint The service endpoint.
071    * @return         The answer formulated as {@link JSONArray}.
072    * @throws LomikelException If call fails. */
073  private JSONArray call(JSONObject request,
074                         String     endpoint) throws LomikelException {
075     String answer = shc.postJSON(_restUrl + "/" + endpoint,
076                                  request.toString(),
077                                  null,
078                                  null);
079     JSONArray ja = new JSONArray(answer);
080     return ja;
081    }
082
083  public static SmallHttpClient shc = new SmallHttpClient();
084  
085  private static String OBJECTS_WS = "objects";
086  private static String LATESTS_WS = "latests";
087  private static String ANOMALY_WS = "anomaly";
088  
089  private String _restUrl;
090
091  /** Logging . */
092  private static Logger log = LogManager.getLogger(FPC.class);
093                                                
094  }
095