Skip to content

Commit

Permalink
refactor: extract opengemini common module (#13)
Browse files Browse the repository at this point in the history
Signed-off-by: ZhangJian He <[email protected]>
  • Loading branch information
ZhangJian He authored Feb 14, 2024
1 parent ef91d1b commit b2e93a9
Show file tree
Hide file tree
Showing 12 changed files with 100 additions and 16 deletions.
5 changes: 5 additions & 0 deletions opengemini-client-asynchttpclient/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@
<artifactId>opengemini-client-api</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.opengemini</groupId>
<artifactId>opengemini-client-common</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.asynchttpclient</groupId>
<artifactId>async-http-client</artifactId>
Expand Down
14 changes: 14 additions & 0 deletions opengemini-client-common/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>io.opengemini</groupId>
<artifactId>opengemini-client-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>

<artifactId>opengemini-client-common</artifactId>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package io.opengemini.client.common;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;

import java.util.List;

public class JacksonService {
private static final ObjectMapper MAPPER = new ObjectMapper();

static {
MAPPER.configure(DeserializationFeature.FAIL_ON_IGNORED_PROPERTIES, false);
MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
}

public static String toJson(Object o) throws JsonProcessingException {
return MAPPER.writeValueAsString(o);
}

public static <T> T toObject(String json, Class<T> type) throws JsonProcessingException {
if (json == null || json.isEmpty()) {
return null;
}
return MAPPER.readValue(json, type);
}

public static <T> T toRefer(String json, TypeReference<T> ref) throws JsonProcessingException {
if (json == null || json.isEmpty()) {
return null;
}
return MAPPER.readValue(json, ref);
}

public static <T> List<T> toList(String json, TypeReference<List<T>> typeRef) throws JsonProcessingException {
if (json == null || json.isEmpty()) {
return List.of();
}
return MAPPER.readValue(json, typeRef);
}

public static JsonNode toJsonNode(String json) throws JsonProcessingException {
return MAPPER.readTree(json);
}

public static ObjectNode createObjectNode() {
return MAPPER.createObjectNode();
}

public static ArrayNode createArrayNode() {
return MAPPER.createArrayNode();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package io.opengemini.client.common;
5 changes: 5 additions & 0 deletions opengemini-client-jdk/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@
<artifactId>opengemini-client-api</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.opengemini</groupId>
<artifactId>opengemini-client-common</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import io.opengemini.client.api.QueryResult;
import io.opengemini.client.api.SslContextUtil;
import io.opengemini.client.api.TlsConfig;
import io.opengemini.client.jdk.common.OpenGeminiCommon;
import io.opengemini.client.common.JacksonService;

import java.net.URI;
import java.net.URISyntaxException;
Expand All @@ -29,7 +29,7 @@ public class OpenGeminiJdkClient {

private final HttpClient client;

private final AtomicInteger prevIndex = new AtomicInteger(0);
private final AtomicInteger prevIndex = new AtomicInteger(-1);

public OpenGeminiJdkClient(Configuration conf) {
this.conf = conf;
Expand Down Expand Up @@ -85,7 +85,7 @@ public CompletableFuture<QueryResult> query(Query query) throws URISyntaxExcepti
CompletableFuture<QueryResult> failedFuture = new CompletableFuture<>();
if (response.statusCode() >= 200 && response.statusCode() < 300) {
try {
QueryResult body = OpenGeminiCommon.converJson2Bean(response.body(), QueryResult.class);
QueryResult body = JacksonService.toObject(response.body(), QueryResult.class);
failedFuture.complete(body);
} catch (JsonProcessingException e) {
failedFuture.completeExceptionally(e);
Expand Down

This file was deleted.

This file was deleted.

5 changes: 5 additions & 0 deletions opengemini-client-okhttp/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
<artifactId>opengemini-client-api</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.opengemini</groupId>
<artifactId>opengemini-client-common</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
Expand Down
5 changes: 5 additions & 0 deletions opengemini-client-reactor/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@
<artifactId>opengemini-client-api</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.opengemini</groupId>
<artifactId>opengemini-client-common</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.projectreactor.netty</groupId>
<artifactId>reactor-netty-http</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

public class OpenGeminiReactorClient {
private final HttpClient client;

private final List<String> serverUrls = new ArrayList<>();

private final AtomicInteger prevIndex = new AtomicInteger(-1);

public OpenGeminiReactorClient(Configuration conf) {
HttpClient client = HttpClient.create();

Expand Down
3 changes: 2 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@
<packaging>pom</packaging>
<modules>
<module>opengemini-client-api</module>
<module>opengemini-client-asynchttpclient</module>
<module>opengemini-client-common</module>
<module>opengemini-client-jdk</module>
<module>opengemini-client-okhttp</module>
<module>opengemini-client-reactor</module>
<module>opengemini-client-asynchttpclient</module>
</modules>

<properties>
Expand Down

0 comments on commit b2e93a9

Please sign in to comment.