Skip to content

Commit

Permalink
Added the doc and sample code for setting auth token (#70)
Browse files Browse the repository at this point in the history
* Added the section that explains on how to set metadata headers

* Added the auth-token to the sample code
  • Loading branch information
Lucas Wang authored and danielmai committed Oct 23, 2018
1 parent 8c5d009 commit a031e96
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
20 changes: 20 additions & 0 deletions README.md
Expand Up @@ -27,6 +27,7 @@ and understand how to run and work with Dgraph.
* [Run a query](#run-a-query)
* [Commit a transaction](#commit-a-transaction)
* [Setting Deadlines](#setting-deadlines)
* [Setting Metadata Headers](#setting-metadata-headers)
* [Helper Methods](#helper-methods)
* [Using the Asynchronous Client](#using-the-asynchronous-client)
- [Development](#development)
Expand Down Expand Up @@ -264,6 +265,25 @@ DgraphClient dgraphClient = new DgraphClient(stub);

[deadline-post]: https://discuss.dgraph.io/t/dgraph-java-client-setting-deadlines-per-call/3056

### Setting Metadata Headers
Certain headers such as authentication tokens need to be set globally for all subsequent calls.
Below is an example of setting a header with the name "auth-token":
```java
// create the stub first
ManagedChannel channel = ManagedChannelBuilder.forAddress(TEST_HOSTNAME, TEST_PORT).usePlaintext(true).build();
DgraphStub stub = DgraphGrpc.newStub(channel);

// use MetadataUtils to augment the stub with headers
Metadata metadata = new Metadata();
metadata.put(Metadata.Key.of("auth-token", Metadata.ASCII_STRING_MARSHALLER), "the-auth-token-value");
stub = MetadataUtils.attachHeaders(stub, metadata);

// create the DgraphClient wrapper around the stub
DgraphClient dgraphClient = new DgraphClient(stub);

// trigger a RPC call using the DgraphClient
dgraphClient.alter(Operation.newBuilder().setDropAll(true).build());
```
### Helper Methods

#### Delete multiple edges
Expand Down
20 changes: 17 additions & 3 deletions samples/DgraphJavaSample/src/main/java/App.java
Expand Up @@ -9,6 +9,9 @@
import io.dgraph.Transaction;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import io.grpc.Metadata;
import io.grpc.stub.MetadataUtils;

import java.util.Collections;
import java.util.List;
import java.util.Map;
Expand All @@ -17,11 +20,22 @@ public class App {
private static final String TEST_HOSTNAME = "localhost";
private static final int TEST_PORT = 9080;

public static void main(final String[] args) {
private static DgraphClient createDgraphClient(boolean withAuthHeader) {
ManagedChannel channel =
ManagedChannelBuilder.forAddress(TEST_HOSTNAME, TEST_PORT).usePlaintext(true).build();
ManagedChannelBuilder.forAddress(TEST_HOSTNAME, TEST_PORT).usePlaintext(true).build();
DgraphStub stub = DgraphGrpc.newStub(channel);
DgraphClient dgraphClient = new DgraphClient(stub);

if (withAuthHeader) {
Metadata metadata = new Metadata();
metadata.put(Metadata.Key.of("auth-token", Metadata.ASCII_STRING_MARSHALLER), "the-auth-token-value");
stub = MetadataUtils.attachHeaders(stub, metadata);
}

return new DgraphClient(stub);
}

public static void main(final String[] args) {
DgraphClient dgraphClient = createDgraphClient(false);

// Initialize
dgraphClient.alter(Operation.newBuilder().setDropAll(true).build());
Expand Down

0 comments on commit a031e96

Please sign in to comment.