-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[MODINV-1162] Send userId in headers for modules clients requests (#810)
- Loading branch information
1 parent
35d60dd
commit 6463989
Showing
22 changed files
with
827 additions
and
67 deletions.
There are no files selected for viewing
85 changes: 85 additions & 0 deletions
85
src/main/java/org/folio/inventory/client/util/ClientWrapperUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package org.folio.inventory.client.util; | ||
|
||
import io.vertx.core.buffer.Buffer; | ||
import io.vertx.ext.web.client.HttpRequest; | ||
import io.vertx.ext.web.client.WebClient; | ||
import org.folio.rest.tools.ClientHelpers; | ||
import io.vertx.core.http.HttpMethod; | ||
|
||
import static org.folio.inventory.dataimport.handlers.matching.util.EventHandlingUtil.OKAPI_TENANT; | ||
import static org.folio.inventory.dataimport.handlers.matching.util.EventHandlingUtil.OKAPI_TOKEN; | ||
import static org.folio.inventory.dataimport.handlers.matching.util.EventHandlingUtil.OKAPI_URL; | ||
import static org.folio.inventory.dataimport.handlers.matching.util.EventHandlingUtil.OKAPI_USER_ID; | ||
|
||
/** | ||
* Utility class for handling client wrapper operations. | ||
*/ | ||
public final class ClientWrapperUtil { | ||
public static final String CONTENT_TYPE = "Content-type"; | ||
public static final String APPLICATION_JSON = "application/json"; | ||
public static final String ACCEPT = "Accept"; | ||
public static final String APPLICATION_JSON_TEXT_PLAIN = "application/json,text/plain"; | ||
|
||
private ClientWrapperUtil() { | ||
} | ||
|
||
/** | ||
* Creates an HTTP request with the specified method and URL, and populates it with Okapi headers. | ||
* | ||
* @param method the HTTP method to use (e.g., GET, POST, PUT) | ||
* @param url the URL for the request | ||
* @param okapiUrl the Okapi URL | ||
* @param tenantId the tenant ID | ||
* @param token the authentication token | ||
* @param userId the user ID | ||
* @param webClient the WebClient instance to use for creating the request | ||
* @return the created HTTP request with populated headers | ||
*/ | ||
public static HttpRequest<Buffer> createRequest(HttpMethod method, String url, String okapiUrl, String tenantId, | ||
String token, String userId, WebClient webClient) { | ||
HttpRequest<Buffer> request = webClient.requestAbs(method, url); | ||
populateOkapiHeaders(request, okapiUrl, tenantId, token, userId); | ||
return request; | ||
} | ||
|
||
/** | ||
* Converts an object to a JSON buffer. | ||
* | ||
* @param object the object to convert | ||
* @return the JSON buffer | ||
*/ | ||
public static Buffer getBuffer(Object object) { | ||
Buffer buffer = Buffer.buffer(); | ||
if (object != null) { | ||
buffer.appendString(ClientHelpers.pojo2json(object)); | ||
} | ||
return buffer; | ||
} | ||
|
||
/** | ||
* Populates the Okapi headers for the given HTTP request. | ||
* | ||
* @param request the HTTP request to populate headers for | ||
* @param okapiUrl the Okapi URL | ||
* @param tenantId the tenant ID | ||
* @param token the authentication token | ||
* @param userId the user ID | ||
*/ | ||
private static void populateOkapiHeaders(HttpRequest<Buffer> request, String okapiUrl, String tenantId, String token, String userId) { | ||
request.putHeader(CONTENT_TYPE, APPLICATION_JSON); | ||
request.putHeader(ACCEPT, APPLICATION_JSON_TEXT_PLAIN); | ||
|
||
if (tenantId != null) { | ||
request.putHeader(OKAPI_TOKEN, token); | ||
request.putHeader(OKAPI_TENANT, tenantId); | ||
} | ||
|
||
if (userId != null) { | ||
request.putHeader(OKAPI_USER_ID, userId); | ||
} | ||
|
||
if (okapiUrl != null) { | ||
request.putHeader(OKAPI_URL, okapiUrl); | ||
} | ||
} | ||
} |
84 changes: 84 additions & 0 deletions
84
src/main/java/org/folio/inventory/client/wrappers/ChangeManagerClientWrapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package org.folio.inventory.client.wrappers; | ||
|
||
import io.vertx.core.Future; | ||
import io.vertx.core.buffer.Buffer; | ||
import io.vertx.core.http.HttpClient; | ||
import io.vertx.core.http.HttpMethod; | ||
import io.vertx.ext.web.client.HttpResponse; | ||
import io.vertx.ext.web.client.WebClient; | ||
import org.folio.rest.client.ChangeManagerClient; | ||
import org.folio.rest.jaxrs.model.InitJobExecutionsRqDto; | ||
import org.folio.rest.jaxrs.model.JobExecution; | ||
import org.folio.rest.jaxrs.model.JobProfileInfo; | ||
import org.folio.rest.jaxrs.model.ParsedRecordDto; | ||
import org.folio.rest.jaxrs.model.RawRecordsDto; | ||
import org.folio.rest.jaxrs.model.StatusDto; | ||
|
||
import static org.folio.inventory.client.util.ClientWrapperUtil.createRequest; | ||
import static org.folio.inventory.client.util.ClientWrapperUtil.getBuffer; | ||
|
||
/** | ||
* Wrapper class for ChangeManagerClient to handle POST and PUT HTTP requests with x-okapi-user-id header. | ||
*/ | ||
public class ChangeManagerClientWrapper extends ChangeManagerClient { | ||
private final String tenantId; | ||
private final String token; | ||
private final String okapiUrl; | ||
private final String userId; | ||
private final WebClient webClient; | ||
public static final String CHANGE_MANAGER_JOB_EXECUTIONS = "/change-manager/jobExecutions/"; | ||
public static final String CHANGE_MANAGER_PARSED_RECORDS = "/change-manager/parsedRecords/"; | ||
|
||
public ChangeManagerClientWrapper(String okapiUrl, String tenantId, String token, String userId, HttpClient httpClient) { | ||
super(okapiUrl, tenantId, token, httpClient); | ||
this.okapiUrl = okapiUrl; | ||
this.tenantId = tenantId; | ||
this.token = token; | ||
this.userId = userId; | ||
this.webClient = WebClient.wrap(httpClient); | ||
} | ||
|
||
@Override | ||
public Future<HttpResponse<Buffer>> postChangeManagerJobExecutions(InitJobExecutionsRqDto initJobExecutionsRqDto) { | ||
return createRequest(HttpMethod.POST, okapiUrl + "/change-manager/jobExecutions", okapiUrl, tenantId, token, userId, webClient) | ||
.sendBuffer(getBuffer(initJobExecutionsRqDto)); | ||
} | ||
|
||
@Override | ||
public Future<HttpResponse<Buffer>> postChangeManagerJobExecutionsRecordsById(String id, boolean acceptInstanceId, RawRecordsDto rawRecordsDto) { | ||
StringBuilder queryParams = new StringBuilder("?"); | ||
queryParams.append("acceptInstanceId="); | ||
queryParams.append(acceptInstanceId); | ||
|
||
return createRequest(HttpMethod.POST, okapiUrl + CHANGE_MANAGER_JOB_EXECUTIONS + id + "/records" + queryParams, | ||
okapiUrl, tenantId, token, userId, webClient) | ||
.sendBuffer(getBuffer(rawRecordsDto)); | ||
} | ||
|
||
@Override | ||
public Future<HttpResponse<Buffer>> putChangeManagerJobExecutionsById(String id, JobExecution jobExecution) { | ||
return createRequest(HttpMethod.PUT, okapiUrl + CHANGE_MANAGER_JOB_EXECUTIONS + id, okapiUrl, tenantId, token, userId, webClient) | ||
.sendBuffer(getBuffer(jobExecution)); | ||
} | ||
|
||
@Override | ||
public Future<HttpResponse<Buffer>> putChangeManagerJobExecutionsJobProfileById(String id, JobProfileInfo jobProfileInfo) { | ||
return createRequest(HttpMethod.PUT, okapiUrl + CHANGE_MANAGER_JOB_EXECUTIONS + id + "/jobProfile", | ||
okapiUrl, tenantId, token, userId, webClient) | ||
.sendBuffer(getBuffer(jobProfileInfo)); | ||
} | ||
|
||
@Override | ||
public Future<HttpResponse<Buffer>> putChangeManagerJobExecutionsStatusById(String id, StatusDto statusDto) { | ||
return createRequest(HttpMethod.PUT, okapiUrl + CHANGE_MANAGER_JOB_EXECUTIONS + id + "/status", | ||
okapiUrl, tenantId, token, userId, webClient) | ||
.sendBuffer(getBuffer(statusDto)); | ||
} | ||
|
||
@Override | ||
public Future<HttpResponse<Buffer>> putChangeManagerParsedRecordsById(String id, ParsedRecordDto parsedRecordDto) { | ||
return createRequest(HttpMethod.PUT, okapiUrl + CHANGE_MANAGER_PARSED_RECORDS + id, | ||
okapiUrl, tenantId, token, userId, webClient) | ||
.sendBuffer(getBuffer(parsedRecordDto)); | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
src/main/java/org/folio/inventory/client/wrappers/SourceStorageRecordsClientWrapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package org.folio.inventory.client.wrappers; | ||
|
||
import io.vertx.core.Future; | ||
import io.vertx.core.buffer.Buffer; | ||
import io.vertx.core.http.HttpClient; | ||
import io.vertx.core.http.HttpMethod; | ||
import io.vertx.ext.web.client.HttpResponse; | ||
import io.vertx.ext.web.client.WebClient; | ||
import org.folio.rest.client.SourceStorageRecordsClient; | ||
import org.folio.rest.jaxrs.model.Record; | ||
import org.folio.util.PercentCodec; | ||
|
||
import static org.folio.inventory.client.util.ClientWrapperUtil.createRequest; | ||
import static org.folio.inventory.client.util.ClientWrapperUtil.getBuffer; | ||
|
||
/** | ||
* Wrapper class for SourceStorageRecordsClient to handle POST and PUT HTTP requests with x-okapi-user-id header. | ||
*/ | ||
public class SourceStorageRecordsClientWrapper extends SourceStorageRecordsClient { | ||
private final String tenantId; | ||
private final String token; | ||
private final String okapiUrl; | ||
private final String userId; | ||
private final WebClient webClient; | ||
public static final String SOURCE_STORAGE_RECORDS = "/source-storage/records/"; | ||
|
||
public SourceStorageRecordsClientWrapper(String okapiUrl, String tenantId, String token, String userId, HttpClient httpClient) { | ||
super(okapiUrl, tenantId, token, httpClient); | ||
this.okapiUrl = okapiUrl; | ||
this.tenantId = tenantId; | ||
this.token = token; | ||
this.userId = userId; | ||
this.webClient = WebClient.wrap(httpClient); | ||
} | ||
|
||
@Override | ||
public Future<HttpResponse<Buffer>> postSourceStorageRecords(Record aRecord) { | ||
return createRequest(HttpMethod.POST, okapiUrl + "/source-storage/records", okapiUrl, tenantId, token, userId, webClient) | ||
.sendBuffer(getBuffer(aRecord)); | ||
} | ||
|
||
@Override | ||
public Future<HttpResponse<Buffer>> putSourceStorageRecordsById(String id, Record aRecord) { | ||
return createRequest(HttpMethod.PUT, okapiUrl + SOURCE_STORAGE_RECORDS + id, okapiUrl, tenantId, token, userId, webClient) | ||
.sendBuffer(getBuffer(aRecord)); | ||
} | ||
|
||
@Override | ||
public Future<HttpResponse<Buffer>> putSourceStorageRecordsGenerationById(String id, Record aRecord) { | ||
return createRequest(HttpMethod.PUT, okapiUrl + SOURCE_STORAGE_RECORDS + id + "/generation", | ||
okapiUrl, tenantId, token, userId, webClient) | ||
.sendBuffer(getBuffer(aRecord)); | ||
} | ||
|
||
@Override | ||
public Future<HttpResponse<Buffer>> putSourceStorageRecordsSuppressFromDiscoveryById(String id, String idType, boolean suppress) { | ||
StringBuilder queryParams = new StringBuilder("?"); | ||
if (idType != null) { | ||
queryParams.append("idType="); | ||
queryParams.append(PercentCodec.encode(idType)); | ||
queryParams.append("&"); | ||
} | ||
|
||
queryParams.append("suppress="); | ||
queryParams.append(suppress); | ||
|
||
return createRequest(HttpMethod.PUT, okapiUrl + SOURCE_STORAGE_RECORDS + id + "/suppress-from-discovery" + queryParams, | ||
okapiUrl, tenantId, token, userId, webClient) | ||
.send(); | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
src/main/java/org/folio/inventory/client/wrappers/SourceStorageSnapshotsClientWrapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package org.folio.inventory.client.wrappers; | ||
|
||
import io.vertx.core.Future; | ||
import io.vertx.core.buffer.Buffer; | ||
import io.vertx.core.http.HttpClient; | ||
import io.vertx.core.http.HttpMethod; | ||
import io.vertx.ext.web.client.HttpResponse; | ||
import io.vertx.ext.web.client.WebClient; | ||
import org.folio.rest.client.SourceStorageSnapshotsClient; | ||
import org.folio.rest.jaxrs.model.Snapshot; | ||
|
||
import static org.folio.inventory.client.util.ClientWrapperUtil.createRequest; | ||
import static org.folio.inventory.client.util.ClientWrapperUtil.getBuffer; | ||
|
||
/** | ||
* Wrapper class for SourceStorageSnapshotsClient to handle POST and PUT HTTP requests with x-okapi-user-id header. | ||
*/ | ||
public class SourceStorageSnapshotsClientWrapper extends SourceStorageSnapshotsClient { | ||
private final String tenantId; | ||
private final String token; | ||
private final String okapiUrl; | ||
private final String userId; | ||
private final WebClient webClient; | ||
|
||
public SourceStorageSnapshotsClientWrapper(String okapiUrl, String tenantId, String token, String userId, HttpClient httpClient) { | ||
super(okapiUrl, tenantId, token, httpClient); | ||
this.okapiUrl = okapiUrl; | ||
this.tenantId = tenantId; | ||
this.token = token; | ||
this.userId = userId; | ||
this.webClient = WebClient.wrap(httpClient); | ||
} | ||
|
||
@Override | ||
public Future<HttpResponse<Buffer>> postSourceStorageSnapshots(Snapshot snapshot) { | ||
return createRequest(HttpMethod.POST, okapiUrl + "/source-storage/snapshots", okapiUrl, tenantId, token, userId, webClient) | ||
.sendBuffer(getBuffer(snapshot)); | ||
} | ||
|
||
@Override | ||
public Future<HttpResponse<Buffer>> putSourceStorageSnapshotsByJobExecutionId(String jobExecutionId, Snapshot snapshot) { | ||
return createRequest(HttpMethod.PUT, okapiUrl + "/source-storage/snapshots/" + jobExecutionId, | ||
okapiUrl, tenantId, token, userId, webClient) | ||
.sendBuffer(getBuffer(snapshot)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.