|
|
|
|
package common.ecommerce.edayun.adapter;
|
|
|
|
|
|
|
|
|
|
import common.ecommerce.edayun.auth.EdayunTokenClient;
|
|
|
|
|
import common.ecommerce.edayun.http.EdayunRequestFactory;
|
|
|
|
|
import common.ecommerce.http.HttpTransport;
|
|
|
|
|
import common.ecommerce.http.JacksonJsonCodec;
|
|
|
|
|
import common.ecommerce.http.TokenManager;
|
|
|
|
|
import common.ecommerce.shared.spi.AccessToken;
|
|
|
|
|
import common.ecommerce.shared.spi.ChannelSecret;
|
|
|
|
|
import common.ecommerce.shared.spi.LockProvider;
|
|
|
|
|
import common.ecommerce.shared.spi.TokenStore;
|
|
|
|
|
import common.ecommerce.warehouse.enums.WarehouseCapability;
|
|
|
|
|
import common.ecommerce.warehouse.enums.WarehouseProvider;
|
|
|
|
|
import common.ecommerce.warehouse.sdk.WarehouseAdapter;
|
|
|
|
|
import common.ecommerce.warehouse.sdk.WarehouseContext;
|
|
|
|
|
import common.ecommerce.edayun.EdayunConstants;
|
|
|
|
|
|
|
|
|
|
import java.time.Clock;
|
|
|
|
|
import java.util.EnumSet;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 易达云适配器。
|
|
|
|
|
*
|
|
|
|
|
* <p><b>骨架阶段</b>:只搭好类型与能力声明,业务接口等文档到位后逐个实现。
|
|
|
|
|
* 未实现的能力<b>不声明</b>,门面调用会显式抛
|
|
|
|
|
* {@code WarehouseUnsupportedCapabilityException},不返回空集合。</p>
|
|
|
|
|
*
|
|
|
|
|
* <p>适配器只持有协议与基础设施,不持有端点与凭据——两者随每次调用经
|
|
|
|
|
* {@code WarehouseConnection} 传入。</p>
|
|
|
|
|
*/
|
|
|
|
|
public final class EdayunAdapter implements WarehouseAdapter {
|
|
|
|
|
|
|
|
|
|
private final HttpTransport transport;
|
|
|
|
|
private final JacksonJsonCodec json;
|
|
|
|
|
private final EdayunRequestFactory requestFactory;
|
|
|
|
|
private final EdayunTokenClient tokenClient;
|
|
|
|
|
private final TokenManager tokenManager;
|
|
|
|
|
|
|
|
|
|
public EdayunAdapter(HttpTransport transport, JacksonJsonCodec json) {
|
|
|
|
|
this(transport, json, null, null, Clock.systemUTC());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param tokenStore 令牌缓存;为 null 时 {@link #token(WarehouseContext)} 每次都重新登录
|
|
|
|
|
* @param lockProvider 防击穿锁;与 tokenStore 成对提供
|
|
|
|
|
*/
|
|
|
|
|
public EdayunAdapter(HttpTransport transport, JacksonJsonCodec json,
|
|
|
|
|
TokenStore tokenStore, LockProvider lockProvider, Clock clock) {
|
|
|
|
|
this.transport = transport;
|
|
|
|
|
this.json = json;
|
|
|
|
|
this.requestFactory = new EdayunRequestFactory(json);
|
|
|
|
|
this.tokenClient = new EdayunTokenClient(transport, json, clock);
|
|
|
|
|
this.tokenManager = tokenStore == null || lockProvider == null ? null
|
|
|
|
|
: new TokenManager(tokenStore, lockProvider, clock,
|
|
|
|
|
EdayunConstants.TOKEN_EARLY_REFRESH_SECONDS) {
|
|
|
|
|
@Override
|
|
|
|
|
protected AccessToken refresh(ChannelSecret secret, AccessToken previous) {
|
|
|
|
|
return tokenClient.fetch(secret.endpointBaseUrl(),
|
|
|
|
|
secret.clientId(), secret.clientSecret());
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 取一个可用 token(缓存命中则不登录)。
|
|
|
|
|
*
|
|
|
|
|
* <p>token 按 {@code appKey + clientId} 维度缓存——同一 OMS 授权账号下多个仓共用一个。</p>
|
|
|
|
|
*/
|
|
|
|
|
public AccessToken token(WarehouseContext ctx) {
|
|
|
|
|
ChannelSecret secret = ctx.secret();
|
|
|
|
|
if (tokenManager == null) {
|
|
|
|
|
return tokenClient.fetch(ctx.endpointBaseUrl(), secret.clientId(), secret.clientSecret());
|
|
|
|
|
}
|
|
|
|
|
return tokenManager.getOrRefresh(tokenKey(secret), secret);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** 令牌缓存维度。 */
|
|
|
|
|
static String tokenKey(ChannelSecret secret) {
|
|
|
|
|
return EdayunConstants.ADAPTER_ID + ":" + secret.appKey() + ":" + secret.clientId();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public String adapterId() {
|
|
|
|
|
return EdayunConstants.ADAPTER_ID;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public WarehouseProvider provider() {
|
|
|
|
|
return WarehouseProvider.EDAYUN;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 本期没有已实现的能力。
|
|
|
|
|
*
|
|
|
|
|
* <p>接入业务接口时逐个声明——<b>声明了就必须有实现</b>,
|
|
|
|
|
* 否则调用方看能力位会以为能用。</p>
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public EnumSet<WarehouseCapability> capabilities() {
|
|
|
|
|
return EnumSet.noneOf(WarehouseCapability.class);
|
|
|
|
|
}
|
|
|
|
|
}
|