Skip to content
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
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@
</dependencies>

<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<!-- Test Surefire Plugin -->
<plugin>
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/ai/pluggy/client/PluggyClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import ai.pluggy.client.response.ErrorResponse;
import ai.pluggy.exception.PluggyException;
import ai.pluggy.utils.Utils;
import ai.pluggy.utils.Version;
import com.google.gson.FieldNamingPolicy;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
Expand Down Expand Up @@ -292,7 +293,7 @@ public String authenticate() throws IOException, PluggyException {
.post(body)
.addHeader("content-type", "application/json")
.addHeader("cache-control", "no-cache")
.addHeader("User-Agent", "PluggyJava/0.16.2")
.addHeader("User-Agent", Version.USER_AGENT)
.build();

String apiKey;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static ai.pluggy.utils.Asserts.assertNotNull;

import ai.pluggy.utils.Version;
import com.google.gson.Gson;

import java.io.IOException;
Expand Down Expand Up @@ -114,8 +115,7 @@ private Request requestWithAuth(Request originalRequest, String apiKey) {
// override the apiKey of the original request with the new one
return originalRequest.newBuilder()
.header(X_API_KEY_HEADER, apiKey)
// TOOD: add dynamic version
.header("User-Agent", "PluggyJava/0.16.2")
.header("User-Agent", Version.USER_AGENT)
.build();
}

Expand Down
24 changes: 24 additions & 0 deletions src/main/java/ai/pluggy/utils/Version.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package ai.pluggy.utils;

import java.io.InputStream;
import java.util.Properties;
import lombok.SneakyThrows;

public final class Version {

public static final String SDK_VERSION = loadSdkVersion();
public static final String USER_AGENT =
"PluggyJava/" + SDK_VERSION + " (Java " + System.getProperty("java.version") + ")";

private Version() {
}

@SneakyThrows
private static String loadSdkVersion() {
Properties props = new Properties();
try (InputStream in = Version.class.getResourceAsStream("/pluggy-version.properties")) {
props.load(in);
}
return props.getProperty("version");
}
}
1 change: 1 addition & 0 deletions src/main/resources/pluggy-version.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
version=${project.version}
27 changes: 27 additions & 0 deletions src/test/java/ai/pluggy/client/unit/VersionTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package ai.pluggy.client.unit;

import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import ai.pluggy.utils.Version;
import org.junit.jupiter.api.Test;

public class VersionTest {

@Test
void sdkVersion_isResolvedFromPom_notTheLiteralPlaceholder() {
assertNotNull(Version.SDK_VERSION);
assertTrue(Version.SDK_VERSION.matches("\\d+\\.\\d+\\.\\d+(-\\w+)?"),
"SDK_VERSION should look like a semver, got: '" + Version.SDK_VERSION
+ "'. If it's '${project.version}', Maven resource filtering is broken.");
}

@Test
void userAgent_followsExpectedFormat() {
assertTrue(Version.USER_AGENT.startsWith("PluggyJava/" + Version.SDK_VERSION + " (Java "),
"USER_AGENT should start with 'PluggyJava/<version> (Java ', got: '"
+ Version.USER_AGENT + "'");
assertTrue(Version.USER_AGENT.endsWith(")"),
"USER_AGENT should end with ')', got: '" + Version.USER_AGENT + "'");
}
}
Loading