|
|
|
|
package common.ecommerce.edayun.adapter;
|
|
|
|
|
|
|
|
|
|
import common.ecommerce.edayun.auth.EdayunTokenClient;
|
|
|
|
|
import common.ecommerce.edayun.dto.EdayunCreateProductDtos;
|
|
|
|
|
import common.ecommerce.edayun.http.EdayunRequestFactory;
|
|
|
|
|
import common.ecommerce.http.HttpRequest;
|
|
|
|
|
import common.ecommerce.http.HttpResponse;
|
|
|
|
|
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.capability.WarehouseProduct;
|
|
|
|
|
import common.ecommerce.warehouse.capability.WarehouseProductCapable;
|
|
|
|
|
import common.ecommerce.warehouse.enums.WarehouseCapability;
|
|
|
|
|
import common.ecommerce.warehouse.enums.WarehouseProvider;
|
|
|
|
|
import common.ecommerce.warehouse.error.WarehouseException;
|
|
|
|
|
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, WarehouseProductCapable {
|
|
|
|
|
|
|
|
|
|
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.of(WarehouseCapability.PRODUCT);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------ 商品
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 创建产品({@code POST /webApi/skuInfo/v1/createProduct})。
|
|
|
|
|
*
|
|
|
|
|
* <p>易达云这个接口是「创建」语义,不是「保存」——同 SKU 重复调用大概率
|
|
|
|
|
* 报错而不是更新。所以结果里带仓侧的 {@code skuId},宿主可据此判断
|
|
|
|
|
* 是否需要后续更新。</p>
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public ProductResult createOrUpdate(WarehouseContext ctx, WarehouseProduct product) {
|
|
|
|
|
EdayunCreateProductDtos.CreateProductRequest body =
|
|
|
|
|
EdayunProductMapper.toCreateRequest(product);
|
|
|
|
|
String bodyJson = json.toJson(body);
|
|
|
|
|
|
|
|
|
|
AccessToken token = token(ctx);
|
|
|
|
|
ChannelSecret secret = ctx.secret();
|
|
|
|
|
long timestamp = ctx.clock().millis();
|
|
|
|
|
HttpRequest req = requestFactory.business(
|
|
|
|
|
ctx.endpointBaseUrl(), EdayunConstants.PATH_PRODUCT_CREATE, "POST",
|
|
|
|
|
token.value(), secret.appKey(), secret.appSecret(),
|
|
|
|
|
timestamp, bodyJson, "warehouse.product.create");
|
|
|
|
|
|
|
|
|
|
HttpResponse resp = transport.execute(req);
|
|
|
|
|
return parseProductResult(ctx, bodyJson, resp);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private ProductResult parseProductResult(WarehouseContext ctx, String requestJson, HttpResponse resp) {
|
|
|
|
|
int status = resp.statusCode();
|
|
|
|
|
if (status < 200 || status >= 300) {
|
|
|
|
|
throw WarehouseException.builder()
|
|
|
|
|
.message("edayun createProduct failed: http " + status
|
|
|
|
|
+ ", body=" + safeRelay(resp.body()))
|
|
|
|
|
.operation("warehouse.product.create")
|
|
|
|
|
.warehouseCode(ctx.warehouseCode())
|
|
|
|
|
.provider(provider().name())
|
|
|
|
|
.requestId(ctx.requestId())
|
|
|
|
|
.platformCode(String.valueOf(status))
|
|
|
|
|
.rawResponse(safeRelay(resp.body()))
|
|
|
|
|
.retryable(status >= 500)
|
|
|
|
|
.build();
|
|
|
|
|
}
|
|
|
|
|
EdayunCreateProductDtos.CreateProductResponse parsed =
|
|
|
|
|
json.fromJson(resp.body(), EdayunCreateProductDtos.CreateProductResponse.class);
|
|
|
|
|
if (parsed == null || parsed.code == null || parsed.code != EdayunConstants.CODE_SUCCESS) {
|
|
|
|
|
throw WarehouseException.builder()
|
|
|
|
|
.message("edayun createProduct business error: code="
|
|
|
|
|
+ (parsed == null ? null : parsed.code)
|
|
|
|
|
+ ", msg=" + (parsed == null ? null : parsed.msg))
|
|
|
|
|
.operation("warehouse.product.create")
|
|
|
|
|
.warehouseCode(ctx.warehouseCode())
|
|
|
|
|
.provider(provider().name())
|
|
|
|
|
.requestId(ctx.requestId())
|
|
|
|
|
.platformCode(parsed == null ? null : String.valueOf(parsed.code))
|
|
|
|
|
.rawResponse(safeRelay(resp.body()))
|
|
|
|
|
.retryable(false)
|
|
|
|
|
.build();
|
|
|
|
|
}
|
|
|
|
|
String skuCode = parsed.data != null && parsed.data.skuCode != null
|
|
|
|
|
? parsed.data.skuCode : json.stringAt(requestJson, "skuCode");
|
|
|
|
|
return ProductResult.of(skuCode, "CREATED",
|
|
|
|
|
parsed.data == null ? null : parsed.data.skuId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** 原始响应可能含敏感信息,异常里只留长度与首段。 */
|
|
|
|
|
private static String safeRelay(String raw) {
|
|
|
|
|
if (raw == null) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
return raw.length() <= 300 ? raw : raw.substring(0, 300) + "...";
|
|
|
|
|
}
|
|
|
|
|
}
|