package com.uas.module.home.network.grpc;

import io.grpc.*;
public class MobileClientAuthenticationInterceptor implements ClientInterceptor {
    /**
     * 登录成功获取的token串
     */
    private static Metadata.Key<String> AUTH_HEADER_KEY =
            Metadata.Key.of("authentication", Metadata.ASCII_STRING_MARSHALLER);
    @Override
    public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(MethodDescriptor<ReqT, RespT> method, CallOptions callOptions, Channel next) {
        return new ForwardingClientCall.SimpleForwardingClientCall<ReqT, RespT>(next.newCall(method, callOptions)) {
            @Override
            public void start(Listener<RespT> responseListener, Metadata headers) {
                if (Authentication.isAuthenticated()) {
                    headers.put(AUTH_HEADER_KEY, Authentication.get());
                }
                super.start(responseListener, headers);
            }
        };
    }
}