Skip to content

Commit

Permalink
refactor: Move TokenInterceptor
Browse files Browse the repository at this point in the history
  • Loading branch information
Anush008 committed Dec 2, 2023
1 parent 9fd3840 commit 07c2a6c
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 39 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@

<p align="center">
<a href="https://qdrant.github.io/java-client"><img src="https://img.shields.io/badge/Docs-Javadoc%203.6.2-success" alt="Javadoc"></a>
<a href="https://github.com/qdrant/java-client/actions/workflows/cd.yml"><img src="https://github.com/qdrant/java-client/actions/workflows/cd.yml/badge.svg?branch=main" alt="Tests"></a>
<a href="https://github.com/qdrant/java-client/blob/main/LICENSE"><img src="https://img.shields.io/badge/License-Apache%202.0-success" alt="Apache 2.0 License"></a>
<a href="https://github.com/qdrant/java-client/actions/workflows/cd.yml"><img src="https://github.com/qdrant/java-client/actions/workflows/cd.yml/badge.svg?branch=master" alt="Tests"></a>
<a href="https://github.com/qdrant/java-client/blob/master/LICENSE"><img src="https://img.shields.io/badge/License-Apache%202.0-success" alt="Apache 2.0 License"></a>
<a href="https://qdrant.to/discord"><img src="https://img.shields.io/badge/Discord-Qdrant-5865F2.svg?logo=discord" alt="Discord"></a>
<a href="https://qdrant.to/roadmap"><img src="https://img.shields.io/badge/Roadmap-2023-bc1439.svg" alt="Roadmap 2023"></a>
</p>
Expand Down Expand Up @@ -174,4 +174,4 @@ time: 4.63542E-4

## ⚖️ LICENSE

Apache 2.0 © [2023](https://github.com/qdrant/java-client/blob/main/LICENSE)
Apache 2.0 © [2023](https://github.com/qdrant/java-client/blob/master/LICENSE)
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

<groupId>io.qdrant</groupId>
<artifactId>client</artifactId>
<!-- UPDATE THE VERSION IN README.MD TOO-->
<version>1.0</version>

<dependencies>
Expand Down
36 changes: 0 additions & 36 deletions src/main/java/io/qdrant/client/QdrantClient.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
package io.qdrant.client;

import io.grpc.CallOptions;
import io.grpc.Channel;
import io.grpc.ClientCall;
import io.grpc.ClientInterceptor;
import io.grpc.ForwardingClientCall.SimpleForwardingClientCall;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import io.grpc.Metadata;
import io.grpc.MethodDescriptor;
import io.qdrant.client.grpc.Collections;
import io.qdrant.client.grpc.CollectionsGrpc;
import io.qdrant.client.grpc.JsonWithInt.Value;
Expand All @@ -24,35 +17,6 @@
import java.util.List;
import java.util.Map;

/** Interceptor for adding an API key to the headers of gRPC requests. */
class TokenInterceptor implements ClientInterceptor {
String apiKey;

/**
* Constructs a new TokenInterceptor with the specified API key.
*
* @param apiKey the API key to be added to the headers
*/
TokenInterceptor(String apiKey) {
this.apiKey = apiKey;
}

static final Metadata.Key<String> API_KEY =
Metadata.Key.of("api-key", Metadata.ASCII_STRING_MARSHALLER);

@Override
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(
MethodDescriptor<ReqT, RespT> method, CallOptions callOptions, Channel next) {
return new SimpleForwardingClientCall<ReqT, RespT>(next.newCall(method, callOptions)) {
@Override
public void start(Listener<RespT> responseListener, Metadata headers) {
headers.put(API_KEY, apiKey);
super.start(responseListener, headers);
}
};
}
}

/** Client for interfacing with the Qdrant service. */
public class QdrantClient {
private QdrantGrpc.QdrantBlockingStub qdrantStub;
Expand Down
37 changes: 37 additions & 0 deletions src/main/java/io/qdrant/client/TokenInterceptor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package io.qdrant.client;

import io.grpc.CallOptions;
import io.grpc.Channel;
import io.grpc.ClientCall;
import io.grpc.ClientInterceptor;
import io.grpc.ForwardingClientCall.SimpleForwardingClientCall;
import io.grpc.Metadata;
import io.grpc.MethodDescriptor;

/** Interceptor for adding an API key to the headers of gRPC requests. */
final class TokenInterceptor implements ClientInterceptor {
private final String apiKey;
private final Metadata.Key<String> API_KEY =
Metadata.Key.of("api-key", Metadata.ASCII_STRING_MARSHALLER);

/**
* Constructs a new TokenInterceptor with the specified API key.
*
* @param apiKey the API key to be added to the headers
*/
TokenInterceptor(String apiKey) {
this.apiKey = apiKey;
}

@Override
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(
MethodDescriptor<ReqT, RespT> method, CallOptions callOptions, Channel next) {
return new SimpleForwardingClientCall<>(next.newCall(method, callOptions)) {
@Override
public void start(Listener<RespT> responseListener, Metadata headers) {
headers.put(API_KEY, apiKey);
super.start(responseListener, headers);
}
};
}
}

0 comments on commit 07c2a6c

Please sign in to comment.