108 lines
3.6 KiB
Java
108 lines
3.6 KiB
Java
|
|
package it.acxent.cc.api;
|
||
|
|
|
||
|
|
import it.acxent.api.ApiClientResult;
|
||
|
|
import it.acxent.api._AblApiClient;
|
||
|
|
import it.acxent.cc.Attivita;
|
||
|
|
import it.acxent.db.ApplParm;
|
||
|
|
import it.acxent.db.ApplParmFull;
|
||
|
|
import org.apache.http.client.methods.CloseableHttpResponse;
|
||
|
|
import org.apache.http.client.methods.HttpPost;
|
||
|
|
import org.apache.http.client.methods.HttpUriRequest;
|
||
|
|
import org.apache.http.impl.client.CloseableHttpClient;
|
||
|
|
import org.apache.http.impl.client.HttpClients;
|
||
|
|
import org.apache.http.util.EntityUtils;
|
||
|
|
import org.json.JSONObject;
|
||
|
|
|
||
|
|
public class CcApi extends _AblApiClient {
|
||
|
|
public static final String API_PRODUCTION = "https://api.cupsolidale.it/api/v1/";
|
||
|
|
|
||
|
|
public static final String API_SANDBOX = "https://sandboxapi.cupsolidale.it/api/v1/";
|
||
|
|
|
||
|
|
private Attivita attivita;
|
||
|
|
|
||
|
|
private boolean debug = false;
|
||
|
|
|
||
|
|
private static final String URI_POST_IMPORT_CC = "/importcc/{id_documento}";
|
||
|
|
|
||
|
|
public CcApi(ApplParmFull apFull) {
|
||
|
|
super(apFull);
|
||
|
|
setUsername("acx-test");
|
||
|
|
setPassword("1qaz2wsx");
|
||
|
|
setUseSandbox(false);
|
||
|
|
}
|
||
|
|
|
||
|
|
public CcApi() {}
|
||
|
|
|
||
|
|
public static void main(String[] args) {
|
||
|
|
String hostname = "localhost";
|
||
|
|
String db = "misericordia";
|
||
|
|
ApplParmFull ap = new ApplParmFull(new ApplParm(17, "//" + hostname + ":3308/" + db, db, "root", "root", 1, 10, 300));
|
||
|
|
CcApi api = new CcApi(ap);
|
||
|
|
ApiClientResult res = api._postImportcc(1234L);
|
||
|
|
System.out.println(res.getMsg());
|
||
|
|
}
|
||
|
|
|
||
|
|
public Attivita getAttivita() {
|
||
|
|
if (this.attivita == null)
|
||
|
|
this.attivita = Attivita.getDefaultInstance(getApFull());
|
||
|
|
return this.attivita;
|
||
|
|
}
|
||
|
|
|
||
|
|
public String getEndpointProduction() {
|
||
|
|
return "https://atr.f3.com/api/";
|
||
|
|
}
|
||
|
|
|
||
|
|
public String getEndpointSandbox() {
|
||
|
|
return "http://localhost/atr4/api/";
|
||
|
|
}
|
||
|
|
|
||
|
|
public ApiClientResult _postImportcc(long l_id_documento) {
|
||
|
|
ApiClientResult resER = new ApiClientResult();
|
||
|
|
try {
|
||
|
|
if (this.debug)
|
||
|
|
System.out.println("CCApi --> _postImportcc");
|
||
|
|
CloseableHttpClient client = HttpClients.createDefault();
|
||
|
|
HttpPost request = new HttpPost(getEndpoint() + getEndpoint());
|
||
|
|
request.setHeader("Authorization", getBase64BasicCredential());
|
||
|
|
request.setHeader("Accept", "application/json");
|
||
|
|
request.setHeader("Content-Type", "application/json");
|
||
|
|
request.setHeader("Content-Language", "it-IT");
|
||
|
|
CloseableHttpResponse closeableHttpResponse = client.execute((HttpUriRequest)request);
|
||
|
|
String content = EntityUtils.toString(closeableHttpResponse.getEntity());
|
||
|
|
int statusCode = closeableHttpResponse.getStatusLine().getStatusCode();
|
||
|
|
if (this.debug) {
|
||
|
|
System.out.println("Status Code: " + statusCode);
|
||
|
|
System.out.println("\ncontent = " + content);
|
||
|
|
}
|
||
|
|
if (statusCode >= 400) {
|
||
|
|
resER.setOk(false);
|
||
|
|
JSONObject jo = new JSONObject(content);
|
||
|
|
resER.setMsg(jo.toString(2));
|
||
|
|
resER.setResult(jo);
|
||
|
|
} else {
|
||
|
|
JSONObject jo = new JSONObject(content);
|
||
|
|
if (this.debug)
|
||
|
|
System.out.println(jo.toString(4));
|
||
|
|
if (jo.has("success")) {
|
||
|
|
if (jo.getBoolean("success") == true) {
|
||
|
|
resER.setMsg(jo.getString("msg"));
|
||
|
|
resER.setOk(true);
|
||
|
|
resER.setResult(Long.valueOf(jo.getLong("id_rigaBolla")));
|
||
|
|
} else {
|
||
|
|
resER.setMsg(jo.getString("msg"));
|
||
|
|
resER.setOk(false);
|
||
|
|
}
|
||
|
|
} else {
|
||
|
|
resER.setMsg("Errore! Risposta non corretta!");
|
||
|
|
resER.setOk(false);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
} catch (Exception e) {
|
||
|
|
System.out.println(e.getMessage());
|
||
|
|
e.printStackTrace();
|
||
|
|
resER.setOk(false);
|
||
|
|
resER.setMsg(e.getMessage());
|
||
|
|
}
|
||
|
|
return resER;
|
||
|
|
}
|
||
|
|
}
|