forked from quarkusio/quarkus
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
gRPC: simplify adding headers to client calls
fixes quarkusio#19209
- Loading branch information
1 parent
9fc6d6d
commit f555667
Showing
5 changed files
with
89 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
extensions/grpc/runtime/src/main/java/io/quarkus/grpc/GrpcClientUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package io.quarkus.grpc; | ||
|
||
import io.grpc.Metadata; | ||
import io.grpc.stub.AbstractStub; | ||
import io.grpc.stub.MetadataUtils; | ||
import io.quarkus.grpc.runtime.MutinyClient; | ||
|
||
/** | ||
* gRPC client utilities | ||
*/ | ||
public class GrpcClientUtils { | ||
|
||
/** | ||
* Attach headers to a gRPC client. | ||
* | ||
* To make a call with headers, first invoke this method and then perform the intended call with the <b>returned</b> client | ||
* | ||
* @param client any kind of gRPC client | ||
* @param extraHeaders headers to attach | ||
* @param <T> type of the client | ||
* @return a client with headers attached | ||
*/ | ||
@SuppressWarnings({ "unchecked", "rawtypes" }) | ||
public static <T> T attachHeaders(T client, Metadata extraHeaders) { | ||
if (client == null) { | ||
throw new NullPointerException("Cannot attach headers to a null client"); | ||
} | ||
if (client instanceof AbstractStub) { | ||
return (T) MetadataUtils.attachHeaders((AbstractStub) client, extraHeaders); | ||
} else if (client instanceof MutinyClient) { | ||
MutinyClient mutinyClient = (MutinyClient) client; | ||
AbstractStub stub = MetadataUtils.attachHeaders(mutinyClient.getStub(), extraHeaders); | ||
return (T) ((MutinyClient) client).cloneWith(stub); | ||
} else { | ||
throw new IllegalArgumentException("Unsupported client type " + client.getClass()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,4 +13,5 @@ public interface MutinyClient<T extends AbstractStub<T>> { | |
*/ | ||
T getStub(); | ||
|
||
MutinyClient<T> cloneWith(T stub); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters