Files
sisvietnamvn_01/appointment/ApiConfig.java
T

96 lines
3.2 KiB
Java

package vn.sis.appointment;
import java.net.URI;
import java.util.Map;
import java.util.Objects;
public record ApiConfig(
String baseUrl,
String tenant,
String clientId,
String clientSecret,
String scope,
String cookieHeader) {
public static ApiConfig fromForm(Map<String, String> form) {
return new ApiConfig(
value(form, "baseUrl", "https://dhs.sisvietnam.vn"),
value(form, "tenant", "3a0f747c-57b6-753b-ba21-3987e8e93b9a"),
value(form, "clientId", "DigitalHealthSolutions_LabConn"),
value(form, "clientSecret", ""),
value(form, "scope", "LabConn"),
value(form, "cookie", ""));
}
public ApiConfig {
baseUrl = trimTrailingSlash(Objects.requireNonNullElse(baseUrl, ""));
tenant = Objects.requireNonNullElse(tenant, "").trim();
clientId = Objects.requireNonNullElse(clientId, "").trim();
clientSecret = Objects.requireNonNullElse(clientSecret, "");
scope = Objects.requireNonNullElse(scope, "").trim();
cookieHeader = Objects.requireNonNullElse(cookieHeader, "").trim();
}
public void validate() {
URI uri;
try {
uri = URI.create(baseUrl);
} catch (IllegalArgumentException ex) {
throw new IllegalArgumentException("Base URL khong hop le.", ex);
}
String scheme = uri.getScheme();
if (scheme == null || !(scheme.equalsIgnoreCase("https") || scheme.equalsIgnoreCase("http"))) {
throw new IllegalArgumentException("Base URL phai bat dau bang http:// hoac https://.");
}
if (tenant.isBlank()) {
throw new IllegalArgumentException("Tenant khong duoc de trong.");
}
if (clientId.isBlank()) {
throw new IllegalArgumentException("Client ID khong duoc de trong.");
}
if (clientSecret.isBlank()) {
throw new IllegalArgumentException("Client Secret khong duoc de trong.");
}
if (scope.isBlank()) {
throw new IllegalArgumentException("Scope khong duoc de trong.");
}
}
public String tokenUrl() {
return baseUrl + "/connect/token";
}
public String doctorsUrl() {
return baseUrl + "/api/app/his/doctors/work-schedules";
}
public String scheduleUrl(String doctorCode) {
return baseUrl + "/api/app/his/doctors/" + Urls.encodePathSegment(doctorCode) + "/work-schedule-days";
}
public String effectiveCookieHeader() {
if (!cookieHeader.isBlank()) {
return cookieHeader;
}
return ".AspNetCore.Culture=c%3Den%7Cuic%3Den; __tenant=" + tenant;
}
public String cacheKeyMaterial() {
return String.join("|", baseUrl, tenant, clientId, clientSecret, scope);
}
private static String value(Map<String, String> form, String key, String defaultValue) {
String value = form.get(key);
return value == null ? defaultValue : value;
}
private static String trimTrailingSlash(String value) {
String result = value.trim();
while (result.endsWith("/")) {
result = result.substring(0, result.length() - 1);
}
return result;
}
}