From 83868c738bb264f648318732ead8475ac591b222 Mon Sep 17 00:00:00 2001 From: VishalMCF <70262922+VishalMCF@users.noreply.github.com> Date: Thu, 28 Dec 2023 20:56:59 +0530 Subject: [PATCH 1/3] added user header in the all api calls --- pom.xml | 8 +++++- .../java/co/novu/common/base/NovuConfig.java | 28 +++++++++++++++++-- .../java/co/novu/common/rest/RestHandler.java | 1 + .../co/novu/common/base/NovuConfigTest.java | 14 ++++++++++ 4 files changed, 48 insertions(+), 3 deletions(-) create mode 100644 src/test/java/co/novu/common/base/NovuConfigTest.java diff --git a/pom.xml b/pom.xml index dd304bf..b00a498 100644 --- a/pom.xml +++ b/pom.xml @@ -196,5 +196,11 @@ okhttp 4.10.0 - + + + org.apache.maven + maven-model + 3.9.6 + + diff --git a/src/main/java/co/novu/common/base/NovuConfig.java b/src/main/java/co/novu/common/base/NovuConfig.java index c3a2ce4..7eae8d4 100644 --- a/src/main/java/co/novu/common/base/NovuConfig.java +++ b/src/main/java/co/novu/common/base/NovuConfig.java @@ -1,5 +1,13 @@ package co.novu.common.base; +import org.apache.maven.model.Model; +import org.apache.maven.model.io.xpp3.MavenXpp3Reader; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileReader; +import java.util.Properties; + import lombok.Getter; import lombok.Setter; @@ -7,10 +15,26 @@ @Setter public class NovuConfig { + private String apiKey; + private String baseUrl = "https://api.novu.co/v1/"; + + private String novuSdk; + private String sdkVersion; + public NovuConfig(String apiKey) { this.apiKey = apiKey; + loadSdkInfoFromPom(); } - private String apiKey; - private String baseUrl = "https://api.novu.co/v1/"; + private void loadSdkInfoFromPom(){ + try { + MavenXpp3Reader reader = new MavenXpp3Reader(); + Model model = reader.read(new FileReader("pom.xml")); // Read the pom.xml file + this.novuSdk = model.getArtifactId(); // Get artifactId + this.sdkVersion = model.getVersion(); + } catch (Exception e) { + e.printStackTrace(); + // Handle exceptions accordingly, e.g., logging or setting default values + } + } } \ No newline at end of file diff --git a/src/main/java/co/novu/common/rest/RestHandler.java b/src/main/java/co/novu/common/rest/RestHandler.java index 49c8796..8e0b57a 100644 --- a/src/main/java/co/novu/common/rest/RestHandler.java +++ b/src/main/java/co/novu/common/rest/RestHandler.java @@ -30,6 +30,7 @@ public Retrofit buildRetrofit() { Request request = chain.request() .newBuilder() .addHeader("Authorization", "ApiKey " + novuConfig.getApiKey()) + .addHeader("User-Agent", "novu/" + novuConfig.getNovuSdk() + "@" + novuConfig.getSdkVersion()) .build(); return chain.proceed(request); }).addInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BASIC)); diff --git a/src/test/java/co/novu/common/base/NovuConfigTest.java b/src/test/java/co/novu/common/base/NovuConfigTest.java new file mode 100644 index 0000000..eac979a --- /dev/null +++ b/src/test/java/co/novu/common/base/NovuConfigTest.java @@ -0,0 +1,14 @@ +package co.novu.common.base; + +import junit.framework.TestCase; + +public class NovuConfigTest extends TestCase { + + public void testNovuConfigConstructor() { + String apiKey = "mockApiKey"; + NovuConfig novuConfig = new NovuConfig(apiKey); + assertEquals(apiKey, novuConfig.getApiKey()); + assertEquals("novu-java", novuConfig.getNovuSdk()); + assertEquals("1.4.0", novuConfig.getSdkVersion()); + } +} From 69f54be2458d914eb97efc9a1be8c645a40f483b Mon Sep 17 00:00:00 2001 From: VishalMCF <70262922+VishalMCF@users.noreply.github.com> Date: Thu, 28 Dec 2023 20:57:35 +0530 Subject: [PATCH 2/3] Revert "added user header in the all api calls" This reverts commit 83868c738bb264f648318732ead8475ac591b222. --- pom.xml | 8 +----- .../java/co/novu/common/base/NovuConfig.java | 28 ++----------------- .../java/co/novu/common/rest/RestHandler.java | 1 - .../co/novu/common/base/NovuConfigTest.java | 14 ---------- 4 files changed, 3 insertions(+), 48 deletions(-) delete mode 100644 src/test/java/co/novu/common/base/NovuConfigTest.java diff --git a/pom.xml b/pom.xml index b00a498..dd304bf 100644 --- a/pom.xml +++ b/pom.xml @@ -196,11 +196,5 @@ okhttp 4.10.0 - - - org.apache.maven - maven-model - 3.9.6 - - + diff --git a/src/main/java/co/novu/common/base/NovuConfig.java b/src/main/java/co/novu/common/base/NovuConfig.java index 7eae8d4..c3a2ce4 100644 --- a/src/main/java/co/novu/common/base/NovuConfig.java +++ b/src/main/java/co/novu/common/base/NovuConfig.java @@ -1,13 +1,5 @@ package co.novu.common.base; -import org.apache.maven.model.Model; -import org.apache.maven.model.io.xpp3.MavenXpp3Reader; - -import java.io.File; -import java.io.FileInputStream; -import java.io.FileReader; -import java.util.Properties; - import lombok.Getter; import lombok.Setter; @@ -15,26 +7,10 @@ @Setter public class NovuConfig { - private String apiKey; - private String baseUrl = "https://api.novu.co/v1/"; - - private String novuSdk; - private String sdkVersion; - public NovuConfig(String apiKey) { this.apiKey = apiKey; - loadSdkInfoFromPom(); } - private void loadSdkInfoFromPom(){ - try { - MavenXpp3Reader reader = new MavenXpp3Reader(); - Model model = reader.read(new FileReader("pom.xml")); // Read the pom.xml file - this.novuSdk = model.getArtifactId(); // Get artifactId - this.sdkVersion = model.getVersion(); - } catch (Exception e) { - e.printStackTrace(); - // Handle exceptions accordingly, e.g., logging or setting default values - } - } + private String apiKey; + private String baseUrl = "https://api.novu.co/v1/"; } \ No newline at end of file diff --git a/src/main/java/co/novu/common/rest/RestHandler.java b/src/main/java/co/novu/common/rest/RestHandler.java index 8e0b57a..49c8796 100644 --- a/src/main/java/co/novu/common/rest/RestHandler.java +++ b/src/main/java/co/novu/common/rest/RestHandler.java @@ -30,7 +30,6 @@ public Retrofit buildRetrofit() { Request request = chain.request() .newBuilder() .addHeader("Authorization", "ApiKey " + novuConfig.getApiKey()) - .addHeader("User-Agent", "novu/" + novuConfig.getNovuSdk() + "@" + novuConfig.getSdkVersion()) .build(); return chain.proceed(request); }).addInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BASIC)); diff --git a/src/test/java/co/novu/common/base/NovuConfigTest.java b/src/test/java/co/novu/common/base/NovuConfigTest.java deleted file mode 100644 index eac979a..0000000 --- a/src/test/java/co/novu/common/base/NovuConfigTest.java +++ /dev/null @@ -1,14 +0,0 @@ -package co.novu.common.base; - -import junit.framework.TestCase; - -public class NovuConfigTest extends TestCase { - - public void testNovuConfigConstructor() { - String apiKey = "mockApiKey"; - NovuConfig novuConfig = new NovuConfig(apiKey); - assertEquals(apiKey, novuConfig.getApiKey()); - assertEquals("novu-java", novuConfig.getNovuSdk()); - assertEquals("1.4.0", novuConfig.getSdkVersion()); - } -} From 933bc2b0bcd92d11661b9ca4fd2718067563e8f9 Mon Sep 17 00:00:00 2001 From: VishalMCF <70262922+VishalMCF@users.noreply.github.com> Date: Thu, 28 Dec 2023 21:15:07 +0530 Subject: [PATCH 3/3] Added User-Agent header --- pom.xml | 8 ++++++- .../java/co/novu/common/base/NovuConfig.java | 22 +++++++++++++++++-- .../java/co/novu/common/rest/RestHandler.java | 1 + 3 files changed, 28 insertions(+), 3 deletions(-) diff --git a/pom.xml b/pom.xml index dd304bf..b00a498 100644 --- a/pom.xml +++ b/pom.xml @@ -196,5 +196,11 @@ okhttp 4.10.0 - + + + org.apache.maven + maven-model + 3.9.6 + + diff --git a/src/main/java/co/novu/common/base/NovuConfig.java b/src/main/java/co/novu/common/base/NovuConfig.java index c3a2ce4..3897fad 100644 --- a/src/main/java/co/novu/common/base/NovuConfig.java +++ b/src/main/java/co/novu/common/base/NovuConfig.java @@ -1,5 +1,10 @@ package co.novu.common.base; +import org.apache.maven.model.Model; +import org.apache.maven.model.io.xpp3.MavenXpp3Reader; + +import java.io.FileReader; + import lombok.Getter; import lombok.Setter; @@ -7,10 +12,23 @@ @Setter public class NovuConfig { + private String apiKey; + private String baseUrl = "https://api.novu.co/v1/"; + private String sdkName; + private String sdkVersion; public NovuConfig(String apiKey) { this.apiKey = apiKey; + loadContextFromPom(); } - private String apiKey; - private String baseUrl = "https://api.novu.co/v1/"; + private void loadContextFromPom(){ + try { + MavenXpp3Reader reader = new MavenXpp3Reader(); + Model model = reader.read(new FileReader("pom.xml")); + this.sdkName = model.getArtifactId(); + this.sdkVersion = model.getVersion(); + } catch (Exception e) { + e.printStackTrace(); + } + } } \ No newline at end of file diff --git a/src/main/java/co/novu/common/rest/RestHandler.java b/src/main/java/co/novu/common/rest/RestHandler.java index 49c8796..c42ad4c 100644 --- a/src/main/java/co/novu/common/rest/RestHandler.java +++ b/src/main/java/co/novu/common/rest/RestHandler.java @@ -30,6 +30,7 @@ public Retrofit buildRetrofit() { Request request = chain.request() .newBuilder() .addHeader("Authorization", "ApiKey " + novuConfig.getApiKey()) + .addHeader("User-Agent", "novu/" + novuConfig.getSdkName() + "@" + novuConfig.getSdkVersion()) .build(); return chain.proceed(request); }).addInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BASIC));