Commit 21032025

This commit is contained in:
2025-03-21 14:24:05 +07:00
parent b248c59e32
commit 80d468a79a
37 changed files with 858 additions and 964 deletions

View File

@@ -3,6 +3,7 @@ package ErhaAPI;
import Config.SomeCodes;
import com.google.gson.Gson;
import lombok.Getter;
import org.tinylog.Logger;
import java.io.File;
@@ -29,6 +30,10 @@ public class ErhaAPI {
private final Gson gson = new Gson();
/**
* Create Erha API object
* @param isProduction if true will use Production URL, if false will use Staging URL
*/
public ErhaAPI(boolean isProduction){
final String API_URL_PROD = "https://connect-api.aryanoble.co.id/api";
final String API_URL_STAGING = "https://connect-api-staging.aryanoble.web.id/api";
@@ -39,6 +44,7 @@ public class ErhaAPI {
* Set API Username
* @param API_USERNAME API Username
*/
@SuppressWarnings("unused")
public void setAPI_USERNAME(String API_USERNAME){
if (ValidString(API_USERNAME)){
if (!API_USERNAME.equals(this.API_USERNAME)){
@@ -52,6 +58,7 @@ public class ErhaAPI {
* Set API Password
* @param API_PASSWORD API Password
*/
@SuppressWarnings("unused")
public void setAPI_PASSWORD(String API_PASSWORD){
if (ValidString(API_PASSWORD)){
if (!API_PASSWORD.equals(this.API_PASSWORD)){
@@ -66,7 +73,7 @@ public class ErhaAPI {
* @param Barcode Barcode to verify
* @return BarcodeResullt object if success, or null if failed
*/
public BarcodeResullt Validate_Barcode(String Barcode){
public BarcodeResullt Validate_Barcode(String Barcode, boolean printdebug){
if (ValidBarCode(Barcode)){
try (HttpClient client = HttpClient.newHttpClient()) {
@@ -79,48 +86,30 @@ public class ErhaAPI {
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
if (response.statusCode()==200){
String body = response.body();
if (printdebug){
System.out.println("Validate_Barcode status code : " + response.statusCode());
System.out.println("Validate_Barcode HTTP body : ");
System.out.println(body);
}
return gson.fromJson(body, BarcodeResullt.class);
} else System.out.println("Validate Barcode status code : " + response.statusCode());
} else {
Logger.error("Validate_Barcode failed, status code : " , response.statusCode());
System.out.println("Validate Barcode status code : " + response.statusCode());
}
} catch (IOException e) {
System.out.println("Validate_Barcode IO Exception, Msg : " + e.getMessage());
Logger.error("Validate_Barcode IO Exception, Msg : " , e.getMessage());
} catch (InterruptedException e) {
System.out.println("Validate_Barcode Interrupted Exception, Msg : " + e.getMessage());
Logger.error("Validate_Barcode Interrupted Exception, Msg : " , e.getMessage());
}
}
return null;
}
// public String Upload_File_OKHttp(String patientID, String filename){
// if (ValidMedicalRecordId(patientID)){
// int medical_record_detail_id = toInt(patientID);
// if (ValidFile(filename)){
// try {
// okhttp3.OkHttpClient client = new okhttp3.OkHttpClient();
// okhttp3.RequestBody requestBody = new okhttp3.MultipartBody.Builder()
// .setType(okhttp3.MultipartBody.FORM)
// .addFormDataPart("medical_record_detail_id", String.valueOf(medical_record_detail_id))
// .addFormDataPart("file", filename, okhttp3.RequestBody.create(okhttp3.MediaType.parse("application/octet-stream"), new java.io.File(filename)))
// .build();
//
// okhttp3.Request request = new okhttp3.Request.Builder()
// .url(API_URL + "/photobooth/photobooth")
// .header("Authorization", "Basic " + auth)
// .post(requestBody)
// .build();
//
// okhttp3.Response response = client.newCall(request).execute();
// if (response.isSuccessful()){
// return response.body().string();
// } else System.out.println("Upload_File_OKHttp status code : " + response.code());
// } catch (Exception e){
// System.out.println("Upload_File_OKHttp Exception, Msg : " + e.getMessage());
// }
// } else return "Invalid File";
// } else return "Invalid Patient ID";
// return null;
// }
/**
@@ -129,7 +118,7 @@ public class ErhaAPI {
* @param filename File to upload
* @return null if failed, or response body if success
*/
public UploadResult Upload_File(String patientID, String filename) {
public UploadResult Upload_File(String patientID, String filename, boolean printdebug) {
if (ValidMedicalRecordId(patientID)){
int medical_record_detail_id = toInt(patientID);
if (ValidFile(filename)){
@@ -173,14 +162,25 @@ public class ErhaAPI {
// Send request
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
if (printdebug){
System.out.println("Upload_File status code : " + response.statusCode());
System.out.println("Upload_File HTTP body : ");
System.out.println(response.body());
}
if (response.statusCode()==200){
return gson.fromJson(response.body(), UploadResult.class);
} else System.out.println("Upload_File status code : " + response.statusCode());
} else {
System.out.println("Upload_File status code : " + response.statusCode());
Logger.error("Upload_File file ",filename," failed, status code : " , response.statusCode());
}
} catch (Exception e){
System.out.println("Upload_File Exception, Msg : " + e.getMessage());
Logger.error("Upload_File file ",filename," failed, Exception, Msg : " , e.getMessage());
}
}
}