|
|
|
@ -22,6 +22,8 @@ import common.ecommerce.channel.model.account.ChannelAccount; |
|
|
|
import common.ecommerce.channel.spi.ChannelAccountRepository; |
|
|
|
import common.ecommerce.channel.spi.ChannelAccountRepository; |
|
|
|
import common.ecommerce.channel.sdk.AdapterFactory.AdapterConfig; |
|
|
|
import common.ecommerce.channel.sdk.AdapterFactory.AdapterConfig; |
|
|
|
import common.ecommerce.shared.spi.IdempotencyStore; |
|
|
|
import common.ecommerce.shared.spi.IdempotencyStore; |
|
|
|
|
|
|
|
import common.ecommerce.shared.spi.ChannelSecret; |
|
|
|
|
|
|
|
import common.ecommerce.shared.spi.SecretProvider; |
|
|
|
import org.slf4j.Logger; |
|
|
|
import org.slf4j.Logger; |
|
|
|
import org.slf4j.LoggerFactory; |
|
|
|
import org.slf4j.LoggerFactory; |
|
|
|
|
|
|
|
|
|
|
|
@ -112,12 +114,21 @@ public final class ChannelSdk implements ProviderResolver { |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
private final Supplier<String> requestIdSupplier; |
|
|
|
private final Supplier<String> requestIdSupplier; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* 凭据解析器;把账户的 {@code credentialRef} 换成真实密钥。 |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* <p>未注入时为 {@code null},此时只能用带 {@code ChannelConnection} 的入口; |
|
|
|
|
|
|
|
* 注入后可使用免连接重载({@code salesOrders(key)} 等)。</p> |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
private final SecretProvider secretProvider; |
|
|
|
|
|
|
|
|
|
|
|
private ChannelSdk(Builder b) { |
|
|
|
private ChannelSdk(Builder b) { |
|
|
|
this.adapters = Collections.unmodifiableMap(new LinkedHashMap<String, PlatformAdapter>(b.adapters)); |
|
|
|
this.adapters = Collections.unmodifiableMap(new LinkedHashMap<String, PlatformAdapter>(b.adapters)); |
|
|
|
this.accountRepository = b.accountRepository; |
|
|
|
this.accountRepository = b.accountRepository; |
|
|
|
this.clock = b.clock; |
|
|
|
this.clock = b.clock; |
|
|
|
this.callObserver = b.callObserver == null ? CallObserver.NOOP : b.callObserver; |
|
|
|
this.callObserver = b.callObserver == null ? CallObserver.NOOP : b.callObserver; |
|
|
|
this.requestIdSupplier = b.requestIdSupplier; |
|
|
|
this.requestIdSupplier = b.requestIdSupplier; |
|
|
|
|
|
|
|
this.secretProvider = b.secretProvider; |
|
|
|
this.idempotencyGuard = b.idempotencyStore == null ? null |
|
|
|
this.idempotencyGuard = b.idempotencyStore == null ? null |
|
|
|
: new IdempotencyGuard(b.idempotencyStore, b.idempotencyOwner, b.clock, |
|
|
|
: new IdempotencyGuard(b.idempotencyStore, b.idempotencyOwner, b.clock, |
|
|
|
IdempotencyGuard.DEFAULT_CLAIM_TTL_SECONDS, callObserver); |
|
|
|
IdempotencyGuard.DEFAULT_CLAIM_TTL_SECONDS, callObserver); |
|
|
|
@ -157,6 +168,19 @@ public final class ChannelSdk implements ProviderResolver { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
|
|
|
|
* 3P 销售订单视图(免连接)。 |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* <p>连接由账户端点 + {@link SecretProvider} 自动组装;需要显式覆盖地址或密钥时 |
|
|
|
|
|
|
|
* 用 {@link #salesOrders(AccountKey, ChannelConnection)}。</p> |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @throws IllegalStateException 未注入 {@code secretProvider},或账户缺端点且适配器无默认地址 |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public SalesOrders salesOrders(AccountKey key) { |
|
|
|
|
|
|
|
ChannelAccount account = requireAccount(key); |
|
|
|
|
|
|
|
return new SalesOrders(resolve(key), account, newContext(account, connectionOf(key, account)), callObserver); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
* 3P 销售订单视图。 |
|
|
|
* 3P 销售订单视图。 |
|
|
|
* |
|
|
|
* |
|
|
|
* <p>能力检查发生在 {@code pull()} 调用时,不是取视图时。</p> |
|
|
|
* <p>能力检查发生在 {@code pull()} 调用时,不是取视图时。</p> |
|
|
|
@ -167,6 +191,16 @@ public final class ChannelSdk implements ProviderResolver { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
|
|
|
|
* 商品 / Listing 视图(免连接)。 |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @throws IllegalStateException 未注入 {@code secretProvider},或账户缺端点且适配器无默认地址 |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public Products products(AccountKey key) { |
|
|
|
|
|
|
|
ChannelAccount account = requireAccount(key); |
|
|
|
|
|
|
|
return new Products(resolve(key), account, newContext(account, connectionOf(key, account)), callObserver); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
* 商品 / Listing 视图。 |
|
|
|
* 商品 / Listing 视图。 |
|
|
|
* |
|
|
|
* |
|
|
|
* <p>能力检查发生在 {@code pull()} 调用时,不是取视图时。</p> |
|
|
|
* <p>能力检查发生在 {@code pull()} 调用时,不是取视图时。</p> |
|
|
|
@ -176,6 +210,12 @@ public final class ChannelSdk implements ProviderResolver { |
|
|
|
return new Products(resolve(key), account, newContext(account, connection), callObserver); |
|
|
|
return new Products(resolve(key), account, newContext(account, connection), callObserver); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** 结算 / 账单交易明细视图(免连接)。 */ |
|
|
|
|
|
|
|
public Settlements settlements(AccountKey key) { |
|
|
|
|
|
|
|
ChannelAccount account = requireAccount(key); |
|
|
|
|
|
|
|
return new Settlements(resolve(key), account, newContext(account, connectionOf(key, account)), callObserver); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** 结算 / 账单交易明细视图。 */ |
|
|
|
/** 结算 / 账单交易明细视图。 */ |
|
|
|
public Settlements settlements(AccountKey key, ChannelConnection connection) { |
|
|
|
public Settlements settlements(AccountKey key, ChannelConnection connection) { |
|
|
|
ChannelAccount account = requireAccount(key); |
|
|
|
ChannelAccount account = requireAccount(key); |
|
|
|
@ -183,6 +223,16 @@ public final class ChannelSdk implements ProviderResolver { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
|
|
|
|
* 售后视图(免连接):拉取退款 / 退货 / 换货订单(只读)。 |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @throws IllegalStateException 未注入 {@code secretProvider},或账户缺端点且适配器无默认地址 |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public AfterSales afterSales(AccountKey key) { |
|
|
|
|
|
|
|
ChannelAccount account = requireAccount(key); |
|
|
|
|
|
|
|
return new AfterSales(resolve(key), account, newContext(account, connectionOf(key, account)), callObserver); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
* 售后视图:拉取退款 / 退货 / 换货订单(只读)。 |
|
|
|
* 售后视图:拉取退款 / 退货 / 换货订单(只读)。 |
|
|
|
* |
|
|
|
* |
|
|
|
* <p>能力检查发生在 {@code pull()} 调用时;适配器未实现 {@code AfterSaleCapable} |
|
|
|
* <p>能力检查发生在 {@code pull()} 调用时;适配器未实现 {@code AfterSaleCapable} |
|
|
|
@ -194,6 +244,16 @@ public final class ChannelSdk implements ProviderResolver { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
|
|
|
|
* 履约 / 出库视图(免连接)。 |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @throws IllegalStateException 未注入 {@code secretProvider},或账户缺端点且适配器无默认地址 |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public Outbounds outbounds(AccountKey key) { |
|
|
|
|
|
|
|
ChannelAccount account = requireAccount(key); |
|
|
|
|
|
|
|
return new Outbounds(resolve(key), account, newContext(account, connectionOf(key, account)), callObserver); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
* 履约 / 出库视图。 |
|
|
|
* 履约 / 出库视图。 |
|
|
|
* |
|
|
|
* |
|
|
|
* <p><b>写操作</b>:会改变平台数据。领星没有测试环境,验证阶段禁止调用。</p> |
|
|
|
* <p><b>写操作</b>:会改变平台数据。领星没有测试环境,验证阶段禁止调用。</p> |
|
|
|
@ -204,6 +264,16 @@ public final class ChannelSdk implements ProviderResolver { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
|
|
|
|
* 入库视图(免连接):平台仓入库货件(FBT / WFS / AWD)与 FBA 到货接收明细(只读)。 |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @throws IllegalStateException 未注入 {@code secretProvider},或账户缺端点且适配器无默认地址 |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public Inbounds inbounds(AccountKey key) { |
|
|
|
|
|
|
|
ChannelAccount account = requireAccount(key); |
|
|
|
|
|
|
|
return new Inbounds(resolve(key), account, newContext(account, connectionOf(key, account)), callObserver); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
* 入库视图:平台仓入库货件(FBT / WFS / AWD)与 FBA 到货接收明细(只读)。 |
|
|
|
* 入库视图:平台仓入库货件(FBT / WFS / AWD)与 FBA 到货接收明细(只读)。 |
|
|
|
* |
|
|
|
* |
|
|
|
* <p>能力检查发生在 {@code shipments()} / {@code receipts()} 调用时; |
|
|
|
* <p>能力检查发生在 {@code shipments()} / {@code receipts()} 调用时; |
|
|
|
@ -216,6 +286,16 @@ public final class ChannelSdk implements ProviderResolver { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
|
|
|
|
* 聚合器的子店铺列表(免连接)。 |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @throws IllegalStateException 未注入 {@code secretProvider},或账户缺端点且适配器无默认地址 |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public List<AggregatorCapable.SubChannel> subChannels(AccountKey key) { |
|
|
|
|
|
|
|
ChannelAccount account = requireAccount(key); |
|
|
|
|
|
|
|
return subChannels(key, connectionOf(key, account)); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
* 聚合器的子店铺列表(如领星下已授权的亚马逊 / 沃尔玛店铺)。 |
|
|
|
* 聚合器的子店铺列表(如领星下已授权的亚马逊 / 沃尔玛店铺)。 |
|
|
|
* |
|
|
|
* |
|
|
|
* <p>典型用途是<b>首次接入</b>:把 platformCode + externalStoreId 拉下来, |
|
|
|
* <p>典型用途是<b>首次接入</b>:把 platformCode + externalStoreId 拉下来, |
|
|
|
@ -326,6 +406,37 @@ public final class ChannelSdk implements ProviderResolver { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
|
|
|
|
* 免连接重载用:按账户端点 + {@link SecretProvider} 组装连接。 |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* <p>端点优先级:{@code account.endpointBaseUrl()} → 适配器默认地址。 |
|
|
|
|
|
|
|
* 两者都没有时明确报错,不猜地址。</p> |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @throws IllegalStateException 未注入 secretProvider、账户无端点且适配器无默认地址, |
|
|
|
|
|
|
|
* 或解析器返回 null |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
private ChannelConnection connectionOf(AccountKey key, ChannelAccount account) { |
|
|
|
|
|
|
|
if (secretProvider == null) { |
|
|
|
|
|
|
|
throw new IllegalStateException( |
|
|
|
|
|
|
|
"ChannelSdk requires a secretProvider for connection-less calls; " |
|
|
|
|
|
|
|
+ "either inject secretProvider(...) or pass a ChannelConnection"); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
String endpoint = account.endpointBaseUrl(); |
|
|
|
|
|
|
|
if (endpoint == null || endpoint.trim().isEmpty()) { |
|
|
|
|
|
|
|
endpoint = resolve(key).defaultEndpointBaseUrl(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (endpoint == null || endpoint.trim().isEmpty()) { |
|
|
|
|
|
|
|
throw new IllegalStateException("no endpoint configured for " + key |
|
|
|
|
|
|
|
+ "; set ChannelAccount.endpointBaseUrl or use the ChannelConnection overload"); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
ChannelSecret secret = secretProvider.get(account.credentialRef()); |
|
|
|
|
|
|
|
if (secret == null) { |
|
|
|
|
|
|
|
throw new IllegalStateException("secretProvider returned null for credentialRef=" |
|
|
|
|
|
|
|
+ account.credentialRef()); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return ChannelConnection.of(endpoint, secret); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
* 没有账户也要能埋点的调用({@code capabilities}):成功路径能拿到真实账户, |
|
|
|
* 没有账户也要能埋点的调用({@code capabilities}):成功路径能拿到真实账户, |
|
|
|
* 账户缺失时用 {@link ChannelContext#ofKey} 记一条失败记录,而不是什么都没留。 |
|
|
|
* 账户缺失时用 {@link ChannelContext#ofKey} 记一条失败记录,而不是什么都没留。 |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
@ -388,6 +499,22 @@ public final class ChannelSdk implements ProviderResolver { |
|
|
|
/** 时钟,默认 UTC;测试注入固定时钟。 */ |
|
|
|
/** 时钟,默认 UTC;测试注入固定时钟。 */ |
|
|
|
private Clock clock = Clock.systemUTC(); |
|
|
|
private Clock clock = Clock.systemUTC(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** 凭据解析器;注入后才能用免连接重载。 */ |
|
|
|
|
|
|
|
private SecretProvider secretProvider; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* 注入凭据解析器。 |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* <p>注入后可使用 {@code salesOrders(key)} 这类免连接入口:SDK 用 |
|
|
|
|
|
|
|
* {@code ChannelAccount.endpointBaseUrl()}(为空则取适配器默认地址)和 |
|
|
|
|
|
|
|
* 本解析器按 {@code credentialRef} 取到的密钥组装连接。 |
|
|
|
|
|
|
|
* 密钥只在内存中出现,SDK 不保存、不落盘。</p> |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public Builder secretProvider(SecretProvider secretProvider) { |
|
|
|
|
|
|
|
this.secretProvider = secretProvider; |
|
|
|
|
|
|
|
return this; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* 注入账户仓库。<b>必需</b>。 |
|
|
|
* 注入账户仓库。<b>必需</b>。 |
|
|
|
* |
|
|
|
* |
|
|
|
|