Skip to content

Commit 2c85635

Browse files
Refactor(client): adjust GraphAPI with better application
1 parent 8998fa3 commit 2c85635

File tree

4 files changed

+37
-71
lines changed

4 files changed

+37
-71
lines changed

hugegraph-client/src/main/java/org/apache/hugegraph/api/graphs/GraphsAPI.java

Lines changed: 35 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -203,69 +203,20 @@ public void mode(String graph, GraphMode mode) {
203203
this.client.put(joinPath(this.path(), graph, MODE), null, mode);
204204
}
205205

206-
public void mode(String graph, String graphSpace, GraphMode mode) {
206+
public void mode(String graphSpace, String graph, GraphMode mode) {
207207
// NOTE: Must provide id for PUT. If you use "graph/mode", "/" will
208208
// be encoded to "%2F". So use "mode" here, although inaccurate.
209209
this.client.put(joinPath(this.path(), graphSpace, graph, MODE), null, mode);
210210
}
211211

212-
public GraphMode mode(String graph, String graphSpace) {
213-
RestResult result = this.client.get(joinPath(this.path(), graphSpace, graph), MODE);
214-
@SuppressWarnings("unchecked")
215-
Map<String, String> mode = result.readObject(Map.class);
216-
String value = mode.get(MODE);
217-
if (value == null) {
218-
throw new InvalidResponseException("Invalid response, expect 'mode' in response");
219-
}
220-
try {
221-
return GraphMode.valueOf(value);
222-
} catch (IllegalArgumentException e) {
223-
throw new InvalidResponseException("Invalid GraphMode value '%s'", value);
224-
}
225-
}
226-
227-
228-
public GraphMode mode(String graph) {
229-
RestResult result = this.client.get(joinPath(this.path(), graph), MODE);
230-
@SuppressWarnings("unchecked")
231-
Map<String, String> mode = result.readObject(Map.class);
232-
String value = mode.get(MODE);
233-
if (value == null) {
234-
throw new InvalidResponseException("Invalid response, expect 'mode' in response");
235-
}
236-
try {
237-
return GraphMode.valueOf(value);
238-
} catch (IllegalArgumentException e) {
239-
throw new InvalidResponseException("Invalid GraphMode value '%s'", value);
240-
}
241-
}
242-
243-
public void readMode(String graph, String graphSpace, GraphReadMode readMode) {
212+
public void readMode(String graphSpace, String graph, GraphReadMode readMode) {
244213
this.client.checkApiVersion("0.59", "graph read mode");
245214
// NOTE: Must provide id for PUT. If you use "graph/graph_read_mode", "/"
246215
// will be encoded to "%2F". So use "graph_read_mode" here, although
247216
// inaccurate.
248217
this.client.put(joinPath(this.path(), graphSpace, graph, GRAPH_READ_MODE), null, readMode);
249218
}
250219

251-
public GraphReadMode readMode(String graph, String graphSpace) {
252-
this.client.checkApiVersion("0.59", "graph read mode");
253-
RestResult result = this.client.get(joinPath(this.path(), graphSpace, graph),
254-
GRAPH_READ_MODE);
255-
@SuppressWarnings("unchecked")
256-
Map<String, String> readMode = result.readObject(Map.class);
257-
String value = readMode.get(GRAPH_READ_MODE);
258-
if (value == null) {
259-
throw new InvalidResponseException("Invalid response, expect 'graph_read_mode' " +
260-
"in response");
261-
}
262-
try {
263-
return GraphReadMode.valueOf(value);
264-
} catch (IllegalArgumentException e) {
265-
throw new InvalidResponseException("Invalid GraphReadMode value '%s'", value);
266-
}
267-
}
268-
269220
public void readMode(String graph, GraphReadMode readMode) {
270221
this.client.checkApiVersion("0.59", "graph read mode");
271222
// NOTE: Must provide id for PUT. If you use "graph/graph_read_mode", "/"
@@ -274,23 +225,47 @@ public void readMode(String graph, GraphReadMode readMode) {
274225
this.client.put(joinPath(this.path(), graph, GRAPH_READ_MODE), null, readMode);
275226
}
276227

277-
public GraphReadMode readMode(String graph) {
278-
this.client.checkApiVersion("0.59", "graph read mode");
279-
RestResult result = this.client.get(joinPath(this.path(), graph), GRAPH_READ_MODE);
228+
private <T extends Enum<T>> T getEnumMode(String graphSpace, String graph,
229+
String modeKey, Class<T> enumClass) {
230+
String path = (graphSpace != null)
231+
? joinPath(this.path(), graphSpace, graph)
232+
: joinPath(this.path(), graph);
233+
234+
RestResult result = this.client.get(path, modeKey);
280235
@SuppressWarnings("unchecked")
281-
Map<String, String> readMode = result.readObject(Map.class);
282-
String value = readMode.get(GRAPH_READ_MODE);
236+
Map<String, String> map = result.readObject(Map.class);
237+
String value = map.get(modeKey);
238+
283239
if (value == null) {
284-
throw new InvalidResponseException("Invalid response, expect 'graph_read_mode' " +
285-
"in response");
240+
throw new InvalidResponseException(
241+
"Invalid response, expect '%s' in response", modeKey);
286242
}
287243
try {
288-
return GraphReadMode.valueOf(value);
244+
return Enum.valueOf(enumClass, value);
289245
} catch (IllegalArgumentException e) {
290-
throw new InvalidResponseException("Invalid GraphReadMode value '%s'", value);
246+
throw new InvalidResponseException(
247+
"Invalid %s value '%s'", enumClass.getSimpleName(), value);
291248
}
292249
}
293250

251+
public GraphMode mode(String graphSpace, String graph) {
252+
return getEnumMode(graphSpace, graph, MODE, GraphMode.class);
253+
}
254+
255+
public GraphMode mode(String graph) {
256+
return getEnumMode(null, graph, MODE, GraphMode.class);
257+
}
258+
259+
public GraphReadMode readMode(String graphSpace, String graph) {
260+
this.client.checkApiVersion("0.59", "graph read mode");
261+
return getEnumMode(graphSpace, graph, GRAPH_READ_MODE, GraphReadMode.class);
262+
}
263+
264+
public GraphReadMode readMode(String graph) {
265+
this.client.checkApiVersion("0.59", "graph read mode");
266+
return getEnumMode(null, graph, GRAPH_READ_MODE, GraphReadMode.class);
267+
}
268+
294269
public String clone(String graph, Map<String, Object> body) {
295270
RestResult result = this.client.post(joinPath(this.path(), graph,
296271
"clone"), body);

hugegraph-client/src/main/java/org/apache/hugegraph/client/RestClient.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public class RestClient extends AbstractRestClient {
4949
private Version apiVersion = null;
5050
@Setter
5151
@Getter
52-
private boolean supportGs = true;
52+
private boolean supportGs = false;
5353

5454
public RestClient(String url, String username, String password, int timeout) {
5555
super(url, username, password, timeout * SECOND);

hugegraph-client/src/main/java/org/apache/hugegraph/driver/GraphsManager.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,6 @@ public GraphReadMode readMode(String graph) {
143143
return this.graphsAPI.readMode(graph);
144144
}
145145

146-
147146
public String clone(String graph, Map<String, Object> body) {
148147
return this.graphsAPI.clone(graph, body);
149148
}

hugegraph-client/src/main/java/org/apache/hugegraph/driver/HugeClient.java

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -173,18 +173,10 @@ private void checkServerApiVersion() {
173173
// 0.81 equals to the {latest_api_version} +10
174174
VersionUtil.check(apiVersion, "0.38", "0.81", "hugegraph-api in server");
175175
this.client.apiVersion(apiVersion);
176-
boolean supportGs = VersionUtil.gte(this.version.getCoreVersion(), "1.7");
176+
boolean supportGs = VersionUtil.gte(this.version.getCoreVersion(), "1.7.0");
177177
this.client.setSupportGs(supportGs);
178178
}
179179

180-
public String getGraphSpaceName() {
181-
return graphSpaceName;
182-
}
183-
184-
public String getGraphName() {
185-
return graphName;
186-
}
187-
188180
public GraphsManager graphs() {
189181
return this.graphs;
190182
}

0 commit comments

Comments
 (0)