From 3fdb77ab9ae1759ad819689378e3f25ca2f0adc7 Mon Sep 17 00:00:00 2001 From: divyanshusinghh <122528880+divyanshusinghh@users.noreply.github.com> Date: Fri, 3 Jan 2025 11:21:38 +0530 Subject: [PATCH] Offline token partner client support added (#49) --- README.md | 34 +++++++++++++++++++ .../extension/service/ExtensionService.java | 25 ++++++++++++++ 2 files changed, 59 insertions(+) diff --git a/README.md b/README.md index 466d6e8..e221535 100644 --- a/README.md +++ b/README.md @@ -198,6 +198,40 @@ public class ExampleOfflineAccessMode { } ``` +#### How to get partnerClient for offline access_mode? + +To obtain the `PartnerClient` for offline access mode in your Java extension, use the provided `ExtensionService` class. +This example demonstrates how to retrieve response time using the obtained `PartnerClient` + +```java +@RestController +@RequestMapping("/api/v1") +public class ExampleOfflineAccessMode { + + @Autowired + ExtensionService extensionService; + + @GetMapping(value = "/response-time", produces = "application/json") + public WebhookPartnerModels.ResponseTimeTs getResponseTime() { + try { + String organizationId = "64b0e718f56860951c34d735"; + PartnerClient partnerClient = extensionService.getPartnerClient("64b0e718f56860951c34d735"); + WebhookPartnerModels.ResponseTimeTs responseTime = partnerClient.webhook.responseTimeSummary( + "66c5b90b2d58ee212f3891f8", + "2024-11-18T11:38:44+05:30", + "2024-11-18T14:38:44+05:30" + ); + + return responseTime; + + } catch (Exception e) { + System.out.println(e.getMessage()); + throw new RuntimeException(e); + } + } +} +``` + #### How to call partner apis? To call partner api you need to have instance of `PartnerClient`. Instance holds methods for SDK classes. diff --git a/src/main/java/com/fynd/extension/service/ExtensionService.java b/src/main/java/com/fynd/extension/service/ExtensionService.java index a7f5dd7..e392c2e 100644 --- a/src/main/java/com/fynd/extension/service/ExtensionService.java +++ b/src/main/java/com/fynd/extension/service/ExtensionService.java @@ -7,6 +7,7 @@ import com.sdk.application.ApplicationClient; import com.sdk.application.ApplicationConfig; import com.sdk.common.model.AccessTokenDto; +import com.sdk.partner.PartnerClient; import com.sdk.platform.PlatformClient; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; @@ -49,6 +50,30 @@ public PlatformClient getPlatformClient(String companyId) { return client; } + public PartnerClient getPartnerClient(String organizationId) { + log.debug("Fetching PartnerClient for organizationId : {}", organizationId); + PartnerClient client = null; + try { + if (!this.ext.isOnlineAccessMode()) { + String sid = Session.generateSessionId(false, new Option(organizationId, this.ext.getExtensionProperties() + .getCluster())); + Session session = sessionStorage.getSession(sid); + if (Objects.nonNull(session)) { + AccessTokenDto rawToken = new AccessTokenDto(); + rawToken.setExpiresIn((session.getExpiresIn())); + rawToken.setAccessToken(session.getAccessToken()); + rawToken.setRefreshToken(session.getRefreshToken()); + client = this.ext.getPartnerClient(organizationId, session); + } + log.info("Partner Client for Organization Id : {} and Session Id :{}", organizationId, sid); + } + } catch (Exception e) { + log.error("Exception in getting Partner Client : ", e); + } + + return client; + } + public ApplicationClient getApplicationClient(String applicationId, String applicationToken) { log.debug("Fetching ApplicationClient for applicationId : {}", applicationId);