Skip to content

feat/DEVTOOLING1010 - prehook posthook for java sdk #1065

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.mypurecloud.sdk.v2.hooksmanager;

import java.util.List;
import java.util.ArrayList;
import java.util.Collections;

public class HookManager {
private List<PreRequestHook> preRequestHooks;
private List<PostResponseHook> postResponseHooks;

public HookManager() {
this.preRequestHooks = new ArrayList<>();
this.postResponseHooks = new ArrayList<>();
}

public List<PreRequestHook> getPreRequestHooks() {
return Collections.unmodifiableList(preRequestHooks);
}

public List<PostResponseHook> getPostResponseHooks() {
return Collections.unmodifiableList(postResponseHooks);
}

public void addPreRequestHook(PreRequestHook hook) {
if (hook != null) {
this.preRequestHooks.add(hook);
}
}

public void removePreRequestHook(PreRequestHook hook) {
this.preRequestHooks.remove(hook);
}

public void addPostResponseHook(PostResponseHook hook) {
if (hook != null) {
this.postResponseHooks.add(hook);
}
}

public void removePostResponseHook(PostResponseHook hook) {
this.postResponseHooks.remove(hook);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.mypurecloud.sdk.v2.hooksmanager;
import com.mypurecloud.sdk.v2.ApiResponse;
import com.mypurecloud.sdk.v2.ApiException;

public class LoggingPostResponseHook<T> implements PostResponseHook<T> {
@Override
public <T> ApiResponse<T> execute(ApiResponse<T> response) throws ApiException {
System.out.println("Post-response hook - Status: " + response.getStatusCode());
System.out.println("Post-response hook - CorrelationId: " + response.getCorrelationId());
return response;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.mypurecloud.sdk.v2.hooksmanager;
import com.mypurecloud.sdk.v2.connector.ApiClientConnectorRequest;
import com.mypurecloud.sdk.v2.ApiException;

public class LoggingPreRequestHook implements PreRequestHook {
@Override
public ApiClientConnectorRequest execute(ApiClientConnectorRequest request) throws ApiException {
System.out.println("Pre-request hook - Method: " + request.getMethod());
System.out.println("Pre-request hook - URL: " + request.getUrl());
return request;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.mypurecloud.sdk.v2.hooksmanager;
import com.mypurecloud.sdk.v2.ApiResponse;
import com.mypurecloud.sdk.v2.ApiException;

public interface PostResponseHook<T> {
<T> ApiResponse<T> execute(ApiResponse<T> response) throws ApiException;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.mypurecloud.sdk.v2.hooksmanager;
import com.mypurecloud.sdk.v2.connector.ApiClientConnectorRequest;
import com.mypurecloud.sdk.v2.ApiException;

public interface PreRequestHook {
ApiClientConnectorRequest execute(ApiClientConnectorRequest request) throws ApiException;
}
37 changes: 36 additions & 1 deletion resources/sdk/purecloudjava/templates/ApiClient.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ import {{invokerPackage}}.extensions.AuthResponse;
import {{invokerPackage}}.Logger;
import {{invokerPackage}}.extensions.LocalDateSerializer;

import {{invokerPackage}}.hooksmanager.HookManager;
import {{invokerPackage}}.hooksmanager.LoggingPostResponseHook;
import {{invokerPackage}}.hooksmanager.LoggingPreRequestHook;
import {{invokerPackage}}.hooksmanager.PostResponseHook;
import {{invokerPackage}}.hooksmanager.PreRequestHook;

public class ApiClient implements AutoCloseable {
private static final String DEFAULT_BASE_PATH = "{{basePath}}";
private static final String DEFAULT_USER_AGENT = "{{#httpUserAgent}}{{.}}{{/httpUserAgent}}{{^httpUserAgent}}Swagger-Codegen{{/httpUserAgent}}/java";
Expand Down Expand Up @@ -89,6 +95,9 @@ public class ApiClient implements AutoCloseable {
private String configFilePath;
private Boolean autoReloadConfig;

// prehook/posthook declarations
private final HookManager hookManager;

public ApiClient() {
this(Builder.standard());
}
Expand All @@ -100,6 +109,8 @@ public class ApiClient implements AutoCloseable {
}
this.basePath = basePath;

this.hookManager = new HookManager();

RetryConfiguration retryConfig = builder.retryConfiguration;
if (retryConfig == null) {
retryConfig = DEFAULT_RETRY_CONFIG;
Expand Down Expand Up @@ -283,6 +294,17 @@ public class ApiClient implements AutoCloseable {
this.gatewayConfiguration = new GatewayConfiguration(host, protocol, port, pathParamsLogin, pathParamsApi);
}

/**
* Methods to set the hooks
*/
public void addPreRequestHook(PreRequestHook hook) {
hookManager.addPreRequestHook(hook);
}

public void addPostResponseHook(PostResponseHook hook) {
hookManager.addPostResponseHook(hook);
}

/**
* Helper method to set access token for the first OAuth2 authentication.
*/
Expand Down Expand Up @@ -878,6 +900,12 @@ public class ApiClient implements AutoCloseable {
ApiClientConnectorRequest connectorRequest = prepareConnectorRequest(request, isAuthRequest);
ApiClientConnectorResponse connectorResponse = null;
Map<String,String> requestHeaderCopy = new HashMap<>(connectorRequest.getHeaders());

// Execute pre-request hooks
for (PreRequestHook hook : hookManager.getPreRequestHooks()) {
connectorRequest = hook.execute(connectorRequest);
}

try {
Retry retry = new Retry(retryConfiguration);
do {
Expand All @@ -888,7 +916,14 @@ public class ApiClient implements AutoCloseable {
logger.trace(connectorRequest.getMethod(), connectorRequest.getUrl(), connectorRequest.readBody(), connectorResponse.getStatusCode(), requestHeaderCopy, responseHeaderCopy);
} while (retry.shouldRetry(connectorResponse));
try {
return interpretConnectorResponse(connectorResponse, returnType);
ApiResponse<T> response = interpretConnectorResponse(connectorResponse, returnType);

// Execute post-response hooks
for (PostResponseHook<?> hook : hookManager.getPostResponseHooks()) {
response = hook.execute(response);
}

return response;
} catch (ApiException e) {
if (e.getStatusCode() == 401 && shouldRefreshAccessToken) {
handleExpiredAccessToken();
Expand Down
41 changes: 40 additions & 1 deletion resources/sdk/purecloudjava/tests/SdkTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import com.mypurecloud.sdk.v2.connector.okhttp.OkHttpClientConnectorProvider;
import com.mypurecloud.sdk.v2.extensions.AuthResponse;
import com.mypurecloud.sdk.v2.extensions.notifications.NotificationHandler;
import com.mypurecloud.sdk.v2.hooksmanager.PostResponseHook;
import com.mypurecloud.sdk.v2.hooksmanager.PreRequestHook;
import com.mypurecloud.sdk.v2.model.*;
import org.testng.Assert;
import org.testng.annotations.AfterTest;
Expand Down Expand Up @@ -248,6 +250,44 @@ public void testObjectMapperIgnoresUnknownProperties() {
}

@Test(priority = 9)
public void testPreAndPostHooks() {
try {
// Create hook implementations
PreRequestHook preHook = request -> {
System.out.println("Pre-request hook - Method: " + request.getMethod());
System.out.println("Pre-request hook - URL: " + request.getUrl());
return request;
};

PostResponseHook<UserEntityListing> postHook = new PostResponseHook<UserEntityListing>() {
@Override
public <T> ApiResponse<T> execute(ApiResponse<T> response) throws ApiException {
System.out.println("Post-hook executed. Status code: " + response.getStatusCode());
return response;
}
};

apiClient.addPreRequestHook(preHook);
apiClient.addPostResponseHook(postHook);

User user = usersApi.getUser(userId, Collections.singletonList("profileSkills"), null, null);

Assert.assertEquals(user.getId(), userId);
Assert.assertEquals(user.getName(), userName);
Assert.assertEquals(user.getEmail(), userEmail);
Assert.assertEquals(user.getDepartment(), userDepartment);


} catch (ApiException ex) {
handleApiException(ex);
} catch (Exception ex) {
System.out.println(ex);
Assert.fail();
}
}


@Test(priority = 10)
public void deleteUser() {
try {
usersApi.deleteUser(userId);
Expand All @@ -260,7 +300,6 @@ public void deleteUser() {
}



@AfterTest
public void afterTest() {
System.out.println("After test");
Expand Down