From 4fe6f3aa40e9ab44e70265e53af7621616d4dc9a Mon Sep 17 00:00:00 2001 From: aparnab93 Date: Wed, 22 Feb 2023 13:21:06 +0530 Subject: [PATCH 1/2] namespace fdk-client changes --- pom.xml | 2 +- .../extension/service/WebhookService.java | 79 ++++++++++--------- .../fynd/extension/storage/RedisStorage.java | 2 +- 3 files changed, 42 insertions(+), 41 deletions(-) diff --git a/pom.xml b/pom.xml index 33021d9..527fe5f 100644 --- a/pom.xml +++ b/pom.xml @@ -17,7 +17,7 @@ 20211205 4.3.1 1.7 - v0.0.62-SNAPSHOT + 1.0.0-inv-SNAPSHOT diff --git a/src/main/java/com/fynd/extension/service/WebhookService.java b/src/main/java/com/fynd/extension/service/WebhookService.java index f0df576..a38fb1d 100644 --- a/src/main/java/com/fynd/extension/service/WebhookService.java +++ b/src/main/java/com/fynd/extension/service/WebhookService.java @@ -10,10 +10,11 @@ import com.sdk.common.model.FDKException; import com.sdk.common.model.FDKServerResponseError; import com.sdk.platform.PlatformClient; -import com.sdk.platform.PlatformModels; +import com.sdk.platform.webhook.WebhookPlatformModels; import com.sdk.universal.PublicClient; import com.sdk.universal.PublicConfig; -import com.sdk.universal.PublicModels; +import com.sdk.universal.webhook.WebhookPublicModels; +import jakarta.servlet.http.HttpServletRequest; import lombok.extern.slf4j.Slf4j; import org.apache.commons.codec.binary.Hex; import org.apache.commons.collections.CollectionUtils; @@ -24,7 +25,6 @@ import javax.crypto.Mac; import javax.crypto.spec.SecretKeySpec; -import jakarta.servlet.http.HttpServletRequest; import java.io.IOException; import java.nio.charset.StandardCharsets; import java.util.*; @@ -63,30 +63,30 @@ public void syncEvents(PlatformClient platformClient, ExtensionProperties extens initialize(extensionProperties); } try { - PlatformModels.EventConfigResponse eventConfigList = platformClient.webhook.fetchAllEventConfigurations(); - PlatformModels.SubscriberResponse subscriberResponse = getSubscriberConfig(platformClient); - PlatformModels.SubscriberConfig subscriberConfig = new PlatformModels.SubscriberConfig(); + WebhookPlatformModels.EventConfigResponse eventConfigList = platformClient.webhook.fetchAllEventConfigurations(); + WebhookPlatformModels.SubscriberResponse subscriberResponse = getSubscriberConfig(platformClient); + WebhookPlatformModels.SubscriberConfig subscriberConfig = new WebhookPlatformModels.SubscriberConfig(); if (Objects.isNull(subscriberResponse) && Objects.nonNull(this.webhookProperties)) { subscriberConfig.setName(this.extensionProperties.getApiKey()); subscriberConfig.setWebhookUrl( getWebhookUrl(this.extensionProperties.getBaseUrl(), this.webhookProperties.getApiPath())); - subscriberConfig.setStatus(PlatformModels.SubscriberStatus.active); + subscriberConfig.setStatus(WebhookPlatformModels.SubscriberStatus.active); subscriberConfig.setEmailId(this.webhookProperties.getNotificationEmail()); subscriberConfig.setEventId(List.copyOf(getEventIds(this.webhookProperties, eventConfigList))); - PlatformModels.Association association = new PlatformModels.Association(); + WebhookPlatformModels.Association association = new WebhookPlatformModels.Association(); association.setCompanyId(Integer.parseInt(platformClient.getConfig() .getCompanyId())); association.setCriteria(getCriteria(this.webhookProperties, Collections.emptyList())); association.setApplicationId(new ArrayList<>()); subscriberConfig.setAssociation(association); subscriberConfig.setAuthMeta( - new PlatformModels.AuthMeta(Fields.HMAC, this.extensionProperties.getApiSecret())); + new WebhookPlatformModels.AuthMeta(Fields.HMAC, this.extensionProperties.getApiSecret())); if (enableWebhooks != null) { if (enableWebhooks.equals(Boolean.TRUE)) { - subscriberConfig.setStatus(PlatformModels.SubscriberStatus.active); + subscriberConfig.setStatus(WebhookPlatformModels.SubscriberStatus.active); } else { - subscriberConfig.setStatus(PlatformModels.SubscriberStatus.inactive); + subscriberConfig.setStatus(WebhookPlatformModels.SubscriberStatus.inactive); } } platformClient.webhook.registerSubscriberToEvent(subscriberConfig); @@ -100,14 +100,14 @@ public void syncEvents(PlatformClient platformClient, ExtensionProperties extens eventConfigList))); if (enableWebhooks != null) { if (enableWebhooks.equals(Boolean.TRUE)) { - subscriberConfig.setStatus(PlatformModels.SubscriberStatus.active); + subscriberConfig.setStatus(WebhookPlatformModels.SubscriberStatus.active); } else { - subscriberConfig.setStatus(PlatformModels.SubscriberStatus.inactive); + subscriberConfig.setStatus(WebhookPlatformModels.SubscriberStatus.inactive); } } if (isConfigurationUpdated(subscriberConfig, this.webhookProperties) || isEventDiff( subscriberResponse, subscriberConfig) || subscriberResponse.getStatus() - .equals(PlatformModels.SubscriberStatus.inactive)) { + .equals(WebhookPlatformModels.SubscriberStatus.inactive)) { platformClient.webhook.updateSubscriberConfig(subscriberConfig); log.info("Webhook Config Details updated"); } @@ -116,8 +116,9 @@ public void syncEvents(PlatformClient platformClient, ExtensionProperties extens } catch (IOException | FDKException | FDKServerResponseError e) { log.error("Exception occurred during Webhook Sync : ", e); throw new FdkWebhookRegistrationError("Failed to sync webhook events. Reason: " + e.getMessage()); + } catch (Exception e) { + throw new RuntimeException(e); } - } public void initialize(ExtensionProperties extensionProperties) { @@ -149,7 +150,7 @@ public void initialize(ExtensionProperties extensionProperties) { } //4. Validate Webhook events - PublicModels.EventConfigResponse eventConfigResponse = getEventConfig(webhookProperties.getEventMap()); + WebhookPublicModels.EventConfigResponse eventConfigResponse = getEventConfig(webhookProperties.getEventMap()); List errorWebhooks = validateEvents(webhookProperties.getEventMap(), eventConfigResponse); if (!errorWebhooks.isEmpty()) { throw new FdkInvalidWebhookConfig("Webhooks events errors" + errorWebhooks); @@ -158,9 +159,9 @@ public void initialize(ExtensionProperties extensionProperties) { log.info("Webhook registry initialized"); } - private PlatformModels.SubscriberResponse getSubscriberConfig(PlatformClient platformClient) { + private WebhookPlatformModels.SubscriberResponse getSubscriberConfig(PlatformClient platformClient) { try { - PlatformModels.SubscriberConfigList subscriberConfigList = platformClient.webhook.getSubscribersByExtensionId( + WebhookPlatformModels.SubscriberConfigList subscriberConfigList = platformClient.webhook.getSubscribersByExtensionId( 1, 1, this.extensionProperties.getApiKey()); if (Objects.nonNull(subscriberConfigList) && CollectionUtils.isNotEmpty(subscriberConfigList.getItems())) { return subscriberConfigList.getItems() @@ -179,7 +180,7 @@ private String getWebhookUrl(String baseURL, String apiPath) { } private Set getEventIds(WebhookProperties webhookProperties, - PlatformModels.EventConfigResponse eventConfigList) { + WebhookPlatformModels.EventConfigResponse eventConfigList) { Set eventIds = new HashSet<>(); webhookProperties.getEventMap() .forEach(eventMap -> { @@ -214,28 +215,28 @@ private String getCriteria(WebhookProperties webhookProperties, List app return Criteria.ALL.getValue(); } - private PlatformModels.SubscriberConfig setSubscriberConfig( - PlatformModels.SubscriberResponse subscriberResponse) { + private WebhookPlatformModels.SubscriberConfig setSubscriberConfig( + WebhookPlatformModels.SubscriberResponse subscriberResponse) { if (Objects.isNull(subscriberResponse)) { throw new FdkWebhookRegistrationError("Subscriber Config Response not found"); } - PlatformModels.SubscriberConfig subscriberConfig = new PlatformModels.SubscriberConfig(); + WebhookPlatformModels.SubscriberConfig subscriberConfig = new WebhookPlatformModels.SubscriberConfig(); subscriberConfig.setName(subscriberResponse.getName()); subscriberConfig.setId(subscriberResponse.getId()); subscriberConfig.setWebhookUrl(subscriberResponse.getWebhookUrl()); subscriberConfig.setAssociation(subscriberResponse.getAssociation()); subscriberConfig.setStatus(subscriberResponse.getStatus()); if (subscriberResponse.getStatus() - .equals(PlatformModels.SubscriberStatus.inactive) || subscriberResponse.getStatus() - .equals(PlatformModels.SubscriberStatus.blocked)) { - subscriberConfig.setStatus(PlatformModels.SubscriberStatus.active); + .equals(WebhookPlatformModels.SubscriberStatus.inactive) || subscriberResponse.getStatus() + .equals(WebhookPlatformModels.SubscriberStatus.blocked)) { + subscriberConfig.setStatus(WebhookPlatformModels.SubscriberStatus.active); } subscriberConfig.setAuthMeta(subscriberResponse.getAuthMeta()); subscriberConfig.setEmailId(subscriberResponse.getEmailId()); return subscriberConfig; } - private boolean isConfigurationUpdated(PlatformModels.SubscriberConfig subscriberConfig, + private boolean isConfigurationUpdated(WebhookPlatformModels.SubscriberConfig subscriberConfig, WebhookProperties webhookProperties) { boolean updated = false; this.associationCriteria = getCriteria(webhookProperties, subscriberConfig.getAssociation() @@ -270,11 +271,11 @@ private boolean isConfigurationUpdated(PlatformModels.SubscriberConfig subscribe return updated; } - private boolean isEventDiff(PlatformModels.SubscriberResponse existingEvents, - PlatformModels.SubscriberConfig newEvents) { + private boolean isEventDiff(WebhookPlatformModels.SubscriberResponse existingEvents, + WebhookPlatformModels.SubscriberConfig newEvents) { Set existingEventIds = existingEvents.getEventConfigs() .stream() - .map(PlatformModels.EventConfig::getId) + .map(WebhookPlatformModels.EventConfig::getId) .collect(Collectors.toSet()); List uniques = new ArrayList<>(newEvents.getEventId()); if (existingEventIds.size() > uniques.size()) { @@ -285,16 +286,16 @@ private boolean isEventDiff(PlatformModels.SubscriberResponse existingEvents, return !uniques.isEmpty(); } - private PublicModels.EventConfigResponse getEventConfig(List eventMap) { + private WebhookPublicModels.EventConfigResponse getEventConfig(List eventMap) { try { - List eventConfigs = new ArrayList<>(); + List eventConfigs = new ArrayList<>(); eventMap.forEach(event -> { String[] eventDetails = event.getName() .split("/"); if (eventDetails.length != 2) { throw new FdkInvalidWebhookConfig("Invalid webhook event map key. Invalid key: " + event.getName()); } - PublicModels.EventConfigBase eventConfig = new PublicModels.EventConfigBase(); + WebhookPublicModels.EventConfigBase eventConfig = new WebhookPublicModels.EventConfigBase(); eventConfig.setEventCategory(event.getCategory()); eventConfig.setEventName(eventDetails[0]); eventConfig.setEventType(eventDetails[1]); @@ -304,7 +305,7 @@ private PublicModels.EventConfigResponse getEventConfig(List PublicConfig publicConfig = new PublicConfig(extensionProperties.getApiKey(), extensionProperties.getCluster()); PublicClient publicClient = new PublicClient(publicConfig); - PublicModels.EventConfigResponse eventConfigResponse = publicClient.webhook.queryWebhookEventDetails( + WebhookPublicModels.EventConfigResponse eventConfigResponse = publicClient.webhook.queryWebhookEventDetails( eventConfigs); log.debug("Webhook events config received: {}", objectMapper.writeValueAsString(eventConfigResponse)); return eventConfigResponse; @@ -315,7 +316,7 @@ private PublicModels.EventConfigResponse getEventConfig(List } private List validateEvents(List eventMap, - PublicModels.EventConfigResponse eventConfigResponse) { + WebhookPublicModels.EventConfigResponse eventConfigResponse) { List errorWebhooks = new ArrayList<>(); eventMap.forEach(eventSubscribed -> { if (!isPartOfEventConfig(eventConfigResponse, eventSubscribed)) { @@ -326,7 +327,7 @@ private List validateEvents(List eventMap, return errorWebhooks; } - private boolean isPartOfEventConfig(PublicModels.EventConfigResponse eventConfigResponse, + private boolean isPartOfEventConfig(WebhookPublicModels.EventConfigResponse eventConfigResponse, EventMapProperties eventSubscribed) { String subscribedEventName = eventSubscribed.getName() .split("/")[0]; @@ -351,8 +352,8 @@ public void disableSalesChannelWebhook(PlatformClient platformClient, String app throw new FdkWebhookRegistrationError("subscribed_sales channel is not set to specific in webhook config"); } try { - PlatformModels.SubscriberResponse subscriberResponse = getSubscriberConfig(platformClient); - PlatformModels.SubscriberConfig subscriberConfig = setSubscriberConfig(subscriberResponse); + WebhookPlatformModels.SubscriberResponse subscriberResponse = getSubscriberConfig(platformClient); + WebhookPlatformModels.SubscriberConfig subscriberConfig = setSubscriberConfig(subscriberResponse); List eventIds = new ArrayList<>(); subscriberResponse.getEventConfigs() .forEach(eventConfig -> eventIds.add(eventConfig.getId())); @@ -391,8 +392,8 @@ public void enableSalesChannelWebhook(PlatformClient platformClient, String appl throw new FdkWebhookRegistrationError("subscribed_sales channel is not set to specific in webhook config"); } try { - PlatformModels.SubscriberResponse subscriberResponse = getSubscriberConfig(platformClient); - PlatformModels.SubscriberConfig subscriberConfig = setSubscriberConfig(subscriberResponse); + WebhookPlatformModels.SubscriberResponse subscriberResponse = getSubscriberConfig(platformClient); + WebhookPlatformModels.SubscriberConfig subscriberConfig = setSubscriberConfig(subscriberResponse); List eventIds = new ArrayList<>(); subscriberResponse.getEventConfigs() .forEach(eventConfig -> eventIds.add(eventConfig.getId())); diff --git a/src/main/java/com/fynd/extension/storage/RedisStorage.java b/src/main/java/com/fynd/extension/storage/RedisStorage.java index 914be94..ca1ac84 100644 --- a/src/main/java/com/fynd/extension/storage/RedisStorage.java +++ b/src/main/java/com/fynd/extension/storage/RedisStorage.java @@ -42,7 +42,7 @@ public Long del(String key) { @Override public String setex(String key, int ttl, String value) { try (Jedis client = pool.getResource()) { - return client.setex(super.prefixKey + key, (long) ttl, value); + return client.setex(super.prefixKey + key, ttl, value); } } From e164782cb84c40053a5180cb831569ba19696e12 Mon Sep 17 00:00:00 2001 From: Bhargav Prajapati Date: Fri, 17 Mar 2023 16:43:55 +0530 Subject: [PATCH 2/2] changed fdk-client-java version --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 527fe5f..e767e03 100644 --- a/pom.xml +++ b/pom.xml @@ -17,7 +17,7 @@ 20211205 4.3.1 1.7 - 1.0.0-inv-SNAPSHOT + v1.0.0