package cn.axzo.workflow.client.config;

import feign.RequestInterceptor;
import feign.RequestTemplate;
import feign.Target;

/**
 * 通用的 Feign 请求拦截器
 *
 * @author wangli
 * @since 2024/2/4 15:33
 */
public class WorkflowRequestInterceptor implements RequestInterceptor {
    private final String serviceVersion;

    public WorkflowRequestInterceptor(String serviceVersion) {
        this.serviceVersion = serviceVersion;
    }

    public static final String HEADER_HTTP_CLIENT = "http-client";
    public static final String HEADER_HTTP_CLIENT_VALUE = "workflowEngine-feign";
    public static final String HEADER_API_VERSION = "service-version";
    public static final String HEADER_SERVER_NAME = "X-SERVER-NAME";


    @Override
    public void apply(RequestTemplate requestTemplate) {
        Target.HardCodedTarget target = (Target.HardCodedTarget) requestTemplate.feignTarget();
        String apiClassPath = target.type().getName();
        if (apiClassPath.contains("cn.axzo.workflow.client.feign.bpmn")
                || apiClassPath.contains("cn.axzo.workflow.client.feign.manage")) {
            requestTemplate.header(HEADER_HTTP_CLIENT, HEADER_HTTP_CLIENT_VALUE);
            requestTemplate.header(HEADER_API_VERSION, serviceVersion);
        }
    }
}
