8000 Add convenience methods for arbitrary requests · MladenMitev/arangodb-java-driver@9833035 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9833035

Browse files
author
mpv1989
committed
Add convenience methods for arbitrary requests
1 parent 736f382 commit 9833035

File tree

8 files changed

+395
-2
lines changed

8 files changed

+395
-2
lines changed

ChangeLog.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a
88

99
### Added
1010

11+
- added convenience methods for arbitrary requests
12+
- added `ArangoDatabase.route(String...)`
1113
- added `DocumentCreateOptions#silent(Boolean)`
1214
- added `DocumentReplaceOptions#silent(Boolean)`
1315
- added `DocumentUpdateOptions#silent(Boolean)`

src/main/java/com/arangodb/ArangoDatabase.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -632,4 +632,14 @@ <V, E> TraversalEntity<V, E> executeTraversal(
632632
*/
633633
void reloadRouting() throws ArangoDBException;
634634

635+
/**
636+
* Returns a new {@link ArangoRoute} instance for the given path (relative to the database) that can be used to
637+
* perform arbitrary requests.
638+
*
639+
* @param path
640+
* The database-relative URL of the route
641+
* @return {@link ArangoRoute}
642+
*/
643+
ArangoRoute route(String... path);
644+
635645
}
Lines changed: 120 additions & 0 deletions
48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
/*
2+
* DISCLAIMER
3+
*
4+
* Copyright 2018 ArangoDB GmbH, Cologne, Germany
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*
18+
* Copyright holder is ArangoDB GmbH, Cologne, Germany
19+
*/
20+
21+
package com.arangodb;
22+
23+
import com.arangodb.velocypack.VPackSlice;
24+
import com.arangodb.velocystream.Response;
25+
26+
/**
27+
* @author Mark Vollmary
28+
*
29+
*/
30+
public interface ArangoRoute {
31+
32+
/**
33+
* Returns a new {@link ArangoRoute} instance for the given path (relative to the current route) that can be used to
34+
* perform arbitrary requests.
35+
*
36+
* @param path
37+
* The relative URL of the route
38+
* @return {@link ArangoRoute}
39+
*/
40+
ArangoRoute route(String... path);
41+
42+
/**
43+
* Header that should be sent with each request to the route.
44+
*
45+
* @param key
46+
* Header key
47+
* @param value
+
* Header value (the <code>toString()</code> method will be called for the value}
49+
* @return {@link ArangoRoute}
50+
*/
51+
ArangoRoute withHeader(String key, Object value);
52+
53+
/**
54+
* Query parameter that should be sent with each request to the route.
55+
*
56+
* @param key
57+
* Query parameter key
58+
* @param value
59+
* Query parameter value (the <code>toString()</code> method will be called for the value}
60+
* @return {@link ArangoRoute}
61+
*/
62+
ArangoRoute withQueryParam(String key, Object value);
63+
64+
/**
65+
* The response body. The body will be serialized to {@link VPackSlice}.
66+
*
67+
* @param body
68+
* The response body
69+
* @return {@link ArangoRoute}
70+
*/
71+
ArangoRoute withBody(Object body);
72+
73+
/**
74+
* Performs a DELETE request to the given URL and returns the server response.
75+
*
76+
* @return server response
77+
*/
78+
Response delete();
79+
80+
/**
81+
* Performs a GET request to the given URL and returns the server response.
82+
*
83+
* @return server response
84+
*/
85+
86+
Response get();
87+
88+
/**
89+
* Performs a HEAD request to the given URL and returns the server response.
90+
*
91 F438 +
* @return server response
92+
*/
93+
94+
Response head();
95+
96+
/**
97+
* Performs a PATCH request to the given URL and returns the server response.
98+
*
99+
* @return server response
100+
*/
101+
102+
Response patch();
103+
104+
/**
105+
* Performs a POST request to the given URL and returns the server response.
106+
*
107+
* @return server response
108+
*/
109+
110+
Response post();
111+
112+
/**
113+
* Performs a PUT request to the given URL and returns the server response.
114+
*
115+
* @return server response
116+
*/
117+
118+
Response put();
119+
120+
}

src/main/java/com/arangodb/internal/ArangoContext.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ public Map<String, String> getHeaderParam() {
4040
return headerParam;
4141
}
4242

43-
public ArangoContext putHeaderParam(final String key, final String value) {
43+
public ArangoContext putHeaderParam(final String key, final Object value) {
4444
if (value != null) {
45-
headerParam.put(key, value);
45+
headerParam.put(key, value.toString());
4646
}
4747
return this;
4848
}

src/main/java/com/arangodb/internal/ArangoDatabaseImpl.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,15 @@
2121
package com.arangodb.internal;
2222

2323
import java.util.Collection;
24+
import java.util.Collections;
2425
import java.util.Map;
2526

2627
import com.arangodb.ArangoCollection;
2728
import com.arangodb.ArangoCursor;
2829
import com.arangodb.ArangoDBException;
2930
import com.arangodb.ArangoDatabase;
3031
import com.arangodb.ArangoGraph;
32+
import com.arangodb.ArangoRoute;
3133
import com.arangodb.entity.AqlExecutionExplainEntity;
3234
import com.arangodb.entity.AqlFunctionEntity;
3335
import com.arangodb.entity.AqlParseEntity;
@@ -365,4 +367,9 @@ protected ArangoDatabaseImpl setCursorInitializer(final ArangoCursorInitializer
365367
return this;
366368
}
367369

370+
@Override
371+
public ArangoRoute route(final String... path) {
372+
return new ArangoRouteImpl(this, createPath(path), Collections.<String, String> emptyMap());
373+
}
374+
368375
}
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
/*
2+
* DISCLAIMER
3+
*
4+
* Copyright 2018 ArangoDB GmbH, Cologne, Germany
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*
18+
* Copyright holder is ArangoDB GmbH, Cologne, Germany
19+
*/
20+
21+
package com.arangodb.internal;
22+
23+
import java.util.HashMap;
24+
import java.util.Map;
25+
import java.util.Map.Entry;
26+
27+
import com.arangodb.ArangoRoute;
28+
import com.arangodb.internal.ArangoExecutor.ResponseDeserializer;
29+
import com.arangodb.velocypack.exception.VPackException;
30+
import com.arangodb.velocystream.Request;
31+
import com.arangodb.velocystream.RequestType;
32+
import com.arangodb.velocystream.Response;
33+
34+
/**
35+
* @author Mark Vollmary
36+
*
37+
*/
38+
public class ArangoRouteImpl extends InternalArangoRoute<ArangoDBImpl, ArangoDatabaseImpl, ArangoExecutorSync>
39+
implements ArangoRoute {
40+
41+
private final Map<String, String> queryParam;
42+
private final Map<String, String> headerParam;
43+
private Object body;
44+
45+
protected ArangoRouteImpl(final ArangoDatabaseImpl db, final String path, final Map<String, String> headerParam) {
46+
super(db, path);
47+
queryParam = new HashMap<String, String>();
48+
this.headerParam = new HashMap<String, String>();
49+
this.headerParam.putAll(headerParam);
50+
}
51+
52+
@Override
53+
public ArangoRoute route(final String... path) {
54+
return new ArangoRouteImpl(db, createPath(this.path, createPath(path)), headerParam);
55+
}
56+
57+
@Override
58+
public ArangoRoute withHeader(final String key, final Object value) {
59+
if (value != null) {
60+
headerParam.put(key, value.toString());
61+
}
62+
return this;
63+
}
64+
65+
@Override
66+
public ArangoRoute withQueryParam(final String key, final Object value) {
67+
if (value != null) {
68+
queryParam.put(key, value.toString());
69+
}
70+
return this;
71+
}
72+
73+
@Override
74+
public ArangoRoute withBody(final Object body) {
75+
this.body = body;
76+
return this;
77+
}
78+
79+
private Response request(final RequestType requestType) {
80+
final Request request = request(db.name(), requestType, path);
81+
for (final Entry<String, String> param : headerParam.entrySet()) {
82+
request.putHeaderParam(param.getKey(), param.getValue());
83+
}
84+
for (final Entry<String, String> param : queryParam.entrySet()) {
85+
request.putQueryParam(param.getKey(), param.getValue());
86+
}
87+
if (body != null) {
88+
request.setBody(util().serialize(body));
89+
}
90+
return executor.execute(request, new ResponseDeserializer<Response>() {
91+
@Override
92+
public Response deserialize(final Response response) throws VPackException {
93+
return response;
94+
}
95+
});
96+
}
97+
98+
@Override
99+
public Response delete() {
100+
return request(RequestType.DELETE);
101+
}
102+
103+
@Override
104+
public Response get() {
105+
return request(RequestType.GET);
106+
}
107+
108+
@Override
109+
public Response head() {
110+
return request(RequestType.HEAD);
111+
}
112+
113+
@Override
114+
public Response patch() {
115+
return request(RequestType.PATCH);
116+
}
117+
118+
@Override
119+
public Response post() {
120+
return request(RequestType.POST);
121+
}
122+
123+
@Override
124+
public Response put() {
125+
return request(RequestType.PUT);
126+
}
127+
128+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* DISCLAIMER
3+
*
4+
* Copyright 2018 ArangoDB GmbH, Cologne, Germany
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*
18+
* Copyright holder is ArangoDB GmbH, Cologne, Germany
19+
*/
20+
21+
package com.arangodb.internal;
22+
23+
/**
24+
* @author Mark Vollmary
25+
*
26+
*/
27+
public class InternalArangoRoute<A extends InternalArangoDB<E>, D extends InternalArangoDatabase<A, E>, E extends ArangoExecutor>
28+
extends ArangoExecuteable<E> {
29+
30+
protected final D db;
31+
protected final String path;
32+
33+
protected InternalArangoRoute(final D db, final String path) {
34+
super(db.executor, db.util, db.context);
35+
this.db = db;
36+
this.path = path;
37+
}
38+
39+
}

0 commit comments

Comments
 (0)
0