256 lines
9.2 KiB
Java
256 lines
9.2 KiB
Java
package it.acxent.brt.api;
|
|
|
|
import com.google.gson.Gson;
|
|
import it.acxent.brt.api.json.PUSPudoPoint;
|
|
import it.acxent.brt.api.json.PUSResponse;
|
|
import it.acxent.brt.api.json.PudoAddress;
|
|
import it.acxent.brt.api.json.PudoPoint;
|
|
import it.acxent.common.Parm;
|
|
import it.acxent.common.StatusMsg;
|
|
import it.acxent.db.ApplParm;
|
|
import it.acxent.db.ApplParmFull;
|
|
import it.acxent.db.DBAdapter;
|
|
import it.acxent.util.Vectumerator;
|
|
import java.util.Base64;
|
|
import java.util.List;
|
|
import java.util.concurrent.TimeUnit;
|
|
import okhttp3.ConnectionPool;
|
|
import okhttp3.HttpUrl;
|
|
import okhttp3.OkHttpClient;
|
|
import okhttp3.Request;
|
|
import okhttp3.Response;
|
|
|
|
public class BrtApi {
|
|
public static final String API_BRT_ENDPOINT = "https://api.brt.it";
|
|
|
|
public static final String P_BRT_API_USER = "BRT_API_USER";
|
|
|
|
public static final String P_BRT_API_PWD = "BRT_API_PWD";
|
|
|
|
public static final String P_BRT_X_API_Auth = "BRT_X_API_Auth";
|
|
|
|
private static final String URI_CMD_PUDO_LIST_BY_ADDRESS = "/pudo/v1/open/pickup/get-pudo-by-address/";
|
|
|
|
private static final String URI_CMD_PUDO_DETAIL = "/pudo/v1/open/pickup/get-pudo-details/";
|
|
|
|
private static final String PARM_ADDRESS = "address";
|
|
|
|
private static final String PARM_CITY = "city";
|
|
|
|
private static final String PARM_COUNTRY_CODE = "countryCode";
|
|
|
|
private static final String PARM_ZIP_CODE = "zipCode";
|
|
|
|
private static final String PARM_PUDO_ID = "pudoId";
|
|
|
|
private static final String HEADER_X_API_AUTH = "X-API-Auth";
|
|
|
|
private static boolean debug = false;
|
|
|
|
private ApplParmFull apFull = null;
|
|
|
|
private String apiUser;
|
|
|
|
private String apiPwd;
|
|
|
|
public BrtApi(ApplParmFull apFull) {
|
|
this.apFull = apFull;
|
|
}
|
|
|
|
public BrtApi(ApplParmFull apFull, String l_user, String l_pwd) {
|
|
this.apFull = apFull;
|
|
setApiUser(l_user);
|
|
setApiPwd(l_pwd);
|
|
}
|
|
|
|
public BrtApi() {}
|
|
|
|
public static void initApplicationParms(ApplParmFull ap) {
|
|
if (ap != null) {
|
|
DBAdapter.logDebug(true, "FACE initParms: start");
|
|
String l_tipoParm = "BRT-API";
|
|
Parm bean = new Parm(ap);
|
|
StatusMsg.updateMsgByTag(ap, "INIT", l_tipoParm);
|
|
bean.findByCodice("BRT_API_USER");
|
|
bean.setFlgAdmin(1L);
|
|
bean.setTipoParm(l_tipoParm);
|
|
bean.setCodice("BRT_API_USER");
|
|
bean.setDescrizione("BRT_API_USER");
|
|
if (bean.getTesto().isEmpty())
|
|
bean.setTesto("acolzi");
|
|
bean.setNota("API USER ACCESSO A BRT - NON USATO PER ADESSO");
|
|
bean.save();
|
|
bean.findByCodice("BRT_API_PWD");
|
|
bean.setFlgAdmin(1L);
|
|
bean.setTipoParm(l_tipoParm);
|
|
bean.setCodice("BRT_API_PWD");
|
|
bean.setDescrizione("BRT_API_PWD");
|
|
if (bean.getTesto().isEmpty())
|
|
bean.setTesto("pio");
|
|
bean.setNota("API PWD ACCESSO A BRT - NON USATO PER ADESSO");
|
|
bean.save();
|
|
bean.findByCodice("BRT_X_API_Auth");
|
|
bean.setFlgAdmin(1L);
|
|
bean.setTipoParm(l_tipoParm);
|
|
bean.setCodice("BRT_X_API_Auth");
|
|
bean.setDescrizione("BRT_X_API_Auth");
|
|
if (bean.getTesto().isEmpty())
|
|
bean.setTesto("34fcc468-ac08-4a70-8fc1-3da42938e0b3");
|
|
bean.setNota("HEADER X-API-Auth PER API PUDO");
|
|
bean.save();
|
|
DBAdapter.logDebug(true, "FACE initParms: stop");
|
|
StatusMsg.deleteMsgByTag(ap, "INIT");
|
|
}
|
|
}
|
|
|
|
public static void main(String[] args) {
|
|
String hostname = "localhost";
|
|
String db = "tf19";
|
|
ApplParmFull ap = new ApplParmFull(new ApplParm(17, "//" + hostname + "/" + db, db, "root", "root", 1, 10, 300));
|
|
BrtApi brtapi = new BrtApi(ap);
|
|
Vectumerator<PudoPoint> vecPudo = new Vectumerator();
|
|
PudoAddress pa = new PudoAddress("via tobbianese 23/g", "59100", "Prato");
|
|
vecPudo = brtapi.findPudoPointByPudoAddress(pa);
|
|
while (vecPudo.hasMoreElements()) {
|
|
PudoPoint pp = (PudoPoint)vecPudo.nextElement();
|
|
System.out.println(pp.getDescrizione());
|
|
}
|
|
}
|
|
|
|
protected String getBase64BasicCredential() {
|
|
String encoding = getApiBrtUser() + ":" + getApiBrtUser();
|
|
return "Basic " + Base64.getEncoder().encodeToString(encoding.getBytes());
|
|
}
|
|
|
|
public ApplParmFull getApFull() {
|
|
return this.apFull;
|
|
}
|
|
|
|
public void setApFull(ApplParmFull apFull) {
|
|
this.apFull = apFull;
|
|
}
|
|
|
|
private String getApiEndpoint() {
|
|
return "https://api.brt.it";
|
|
}
|
|
|
|
private String getApiBrtPwd() {
|
|
if (this.apiPwd == null)
|
|
return this.apFull.getParm("BRT_API_PWD").getTesto();
|
|
return this.apiPwd.trim();
|
|
}
|
|
|
|
private String getX_API_Auth() {
|
|
return this.apFull.getParm("BRT_X_API_Auth").getTesto();
|
|
}
|
|
|
|
public void setApiUser(String apiUser) {
|
|
this.apiUser = apiUser;
|
|
}
|
|
|
|
public void setApiPwd(String apiPwd) {
|
|
this.apiPwd = apiPwd;
|
|
}
|
|
|
|
public BrtApiResult _pudoByAddress(PudoAddress l_pudoAddress) {
|
|
boolean debug = true;
|
|
if (debug)
|
|
DBAdapter.printDebug(debug, "1 ------------ BrtApi NO COOKIE OKHTTP");
|
|
BrtApiResult resER = new BrtApiResult();
|
|
String l_indirizzoCompleto = l_pudoAddress.getAddress() + " " + l_pudoAddress.getAddress() + " " + l_pudoAddress.getCity();
|
|
DBAdapter.printDebug(debug, "2 BrtApi --> pudoaddress: " + l_indirizzoCompleto);
|
|
try {
|
|
OkHttpClient client = new OkHttpClient.Builder().connectionPool(new ConnectionPool(0, 1L, TimeUnit.NANOSECONDS))
|
|
.followSslRedirects(false).build();
|
|
String baseUrl = getApiEndpoint() + "/pudo/v1/open/pickup/get-pudo-by-address/";
|
|
DBAdapter.printDebug(debug, "3 URI: " + baseUrl);
|
|
DBAdapter.printDebug(debug, "3.1 getX_API_Auth: " + getX_API_Auth());
|
|
HttpUrl url = HttpUrl.parse(baseUrl).newBuilder().addQueryParameter("address", l_pudoAddress.getAddress())
|
|
.addQueryParameter("city", l_pudoAddress.getCity())
|
|
.addQueryParameter("countryCode", l_pudoAddress.getCountryCode())
|
|
.addQueryParameter("zipCode", l_pudoAddress.getZipCode()).build();
|
|
Request request = new Request.Builder().url(url).get().addHeader("X-API-Auth", getX_API_Auth()).build();
|
|
Response response = client.newCall(request).execute();
|
|
String content = response.body().string();
|
|
int statusCode = response.code();
|
|
DBAdapter.printDebug(debug, "4Status Code: " + statusCode);
|
|
DBAdapter.printDebug(debug, "4content = " + content);
|
|
if (statusCode >= 400) {
|
|
resER.setOk(false);
|
|
resER.setMsg(content);
|
|
resER.setResult(content);
|
|
} else {
|
|
Gson gson = new Gson();
|
|
PUSResponse pusResponse = (PUSResponse)gson.fromJson(content, PUSResponse.class);
|
|
resER.setMsg("Lista pudo trovata per: " + l_indirizzoCompleto);
|
|
resER.setOk(true);
|
|
resER.setResult(pusResponse);
|
|
resER.setPusResponse(pusResponse);
|
|
}
|
|
} catch (Exception e) {
|
|
DBAdapter.printDebug(e.getMessage());
|
|
e.printStackTrace();
|
|
resER.setOk(false);
|
|
resER.setMsg(e.getMessage());
|
|
}
|
|
return resER;
|
|
}
|
|
|
|
public BrtApiResult _pudoDetail(String l_pudoId) {
|
|
boolean debug = false;
|
|
DBAdapter.printDebug(debug, "1 ------------ BrtApi NO COOKIE OKHTTP");
|
|
BrtApiResult resER = new BrtApiResult();
|
|
DBAdapter.printDebug(debug, "2 BrtApi --> pudoid: " + l_pudoId);
|
|
try {
|
|
OkHttpClient client = new OkHttpClient.Builder().connectionPool(new ConnectionPool(0, 1L, TimeUnit.NANOSECONDS))
|
|
.followSslRedirects(false).build();
|
|
String baseUrl = getApiEndpoint() + "/pudo/v1/open/pickup/get-pudo-details/";
|
|
DBAdapter.printDebug(debug, " URI: " + baseUrl);
|
|
HttpUrl url = HttpUrl.parse(baseUrl).newBuilder().addQueryParameter("pudoId", l_pudoId).build();
|
|
Request request = new Request.Builder().url(url).get().build();
|
|
Response response = client.newCall(request).execute();
|
|
String content = response.body().string();
|
|
int statusCode = response.code();
|
|
DBAdapter.printDebug(debug, "4Status Code: " + statusCode);
|
|
DBAdapter.printDebug(debug, "4content = " + content);
|
|
if (statusCode >= 400) {
|
|
resER.setOk(false);
|
|
resER.setMsg(content);
|
|
resER.setResult(content);
|
|
} else {
|
|
Gson gson = new Gson();
|
|
PUSResponse pusResponse = (PUSResponse)gson.fromJson(content, PUSResponse.class);
|
|
resER.setMsg("Dettaglio pudo trovato per pudoid: " + l_pudoId);
|
|
resER.setOk(true);
|
|
resER.setResult(pusResponse);
|
|
resER.setPusResponse(pusResponse);
|
|
}
|
|
} catch (Exception e) {
|
|
DBAdapter.printDebug(e.getMessage());
|
|
e.printStackTrace();
|
|
resER.setOk(false);
|
|
resER.setMsg(e.getMessage());
|
|
}
|
|
return resER;
|
|
}
|
|
|
|
private String getApiBrtUser() {
|
|
if (this.apiUser == null)
|
|
return this.apFull.getParm("BRT_API_USER").getTesto();
|
|
return this.apiUser.trim();
|
|
}
|
|
|
|
public Vectumerator<PudoPoint> findPudoPointByPudoAddress(PudoAddress pa) {
|
|
Vectumerator<PudoPoint> vecPudo = new Vectumerator();
|
|
BrtApiResult resBrt = _pudoByAddress(pa);
|
|
PUSResponse pusResponse = resBrt.getPusResponse();
|
|
List<PUSPudoPoint> listPudo = pusResponse.getPudo();
|
|
for (PUSPudoPoint pudoPoint : listPudo) {
|
|
PUSPudoPoint ppp = new PUSPudoPoint(pudoPoint.getPudoId(), pudoPoint.getPointName(), pudoPoint.getStreet(),
|
|
pudoPoint.getStreetNumber(), pudoPoint.getZipCode(), pudoPoint.getTown());
|
|
PudoPoint pp = new PudoPoint(ppp);
|
|
vecPudo.add(pp);
|
|
}
|
|
return vecPudo;
|
|
}
|
|
}
|