|
19 | 19 | import io.dgraph.DgraphProto.TxnContext;
|
20 | 20 | import io.dgraph.DgraphProto.Version;
|
21 | 21 |
|
| 22 | +import io.grpc.ManagedChannelBuilder; |
| 23 | +import io.grpc.Metadata; |
| 24 | +import io.grpc.stub.MetadataUtils; |
| 25 | + |
| 26 | +import java.net.MalformedURLException; |
| 27 | +import java.net.URL; |
| 28 | + |
22 | 29 | /**
|
23 | 30 | * Implementation of a DgraphClient using grpc.
|
24 | 31 | *
|
|
27 | 34 | * @author Edgar Rodriguez-Diaz
|
28 | 35 | * @author Deepak Jois
|
29 | 36 | * @author Michail Klimenkov
|
| 37 | + * @author Neeraj Battan |
| 38 | + * @author Abhimanyu Singh Gaur |
30 | 39 | */
|
31 | 40 | public class DgraphClient {
|
| 41 | + private static final String gRPC_AUTHORIZATION_HEADER_NAME = "authorization"; |
32 | 42 |
|
33 | 43 | private final DgraphAsyncClient asyncClient;
|
34 | 44 |
|
| 45 | + /** |
| 46 | + * Creates a gRPC stub that can be used to construct clients to connect with Slash GraphQL. |
| 47 | + * |
| 48 | + * @param slashEndpoint The url of the Slash GraphQL endpoint. Example: |
| 49 | + * https://your-slash-instance.cloud.dgraph.io/graphql |
| 50 | + * @param apiKey The API key used to connect to your Slash GraphQL instance. |
| 51 | + * @return A new DgraphGrpc.DgraphStub object to be used with DgraphClient/DgraphAsyncClient. |
| 52 | + */ |
| 53 | + public static DgraphGrpc.DgraphStub clientStubFromSlashEndpoint( |
| 54 | + String slashEndpoint, String apiKey) throws MalformedURLException { |
| 55 | + String[] parts = new URL(slashEndpoint).getHost().split("[.]", 2); |
| 56 | + if (parts.length < 2) { |
| 57 | + throw new MalformedURLException("Invalid Slash URL."); |
| 58 | + } |
| 59 | + String gRPCAddress = parts[0] + ".grpc." + parts[1]; |
| 60 | + |
| 61 | + Metadata metadata = new Metadata(); |
| 62 | + metadata.put(Metadata.Key.of(gRPC_AUTHORIZATION_HEADER_NAME, |
| 63 | + Metadata.ASCII_STRING_MARSHALLER), apiKey); |
| 64 | + return MetadataUtils.attachHeaders( |
| 65 | + DgraphGrpc.newStub( |
| 66 | + ManagedChannelBuilder.forAddress(gRPCAddress, 443).useTransportSecurity().build()), |
| 67 | + metadata); |
| 68 | + } |
| 69 | + |
35 | 70 | /**
|
36 | 71 | * Creates a new client for interacting with a Dgraph store.
|
37 | 72 | *
|
38 | 73 | * <p>A single client is thread safe.
|
39 | 74 | *
|
40 | 75 | * @param stubs - an array of grpc stubs to be used by this client. The stubs to be used are
|
41 |
| - * chosen at random per transaction. |
| 76 | + * chosen at random per transaction. |
42 | 77 | */
|
43 | 78 | public DgraphClient(DgraphGrpc.DgraphStub... stubs) {
|
44 | 79 | this.asyncClient = new DgraphAsyncClient(stubs);
|
@@ -142,7 +177,7 @@ public Version checkVersion() {
|
142 | 177 | * access JWT and a refresh JWT, which will be stored in the jwt field of this class, and used for
|
143 | 178 | * authorizing all subsequent requests sent to the server.
|
144 | 179 | *
|
145 |
| - * @param userid the id of the user who is trying to login, e.g. Alice |
| 180 | + * @param userid the id of the user who is trying to login, e.g. Alice |
146 | 181 | * @param password the password of the user
|
147 | 182 | */
|
148 | 183 | public void login(String userid, String password) {
|
|
0 commit comments