Browse Source
注册表(方案 A): - LingxingSubPlatforms 从全局静态改成 builder().register(...).build() - 同一平台重复注册在 build() 时抛异常,不再静默覆盖 - 新增 LingxingSubPlatformDefaults.all(transport, json) 装配默认实现 - LingxingAdapter 持有注册表实例;新增可注入注册表的构造器, 传 null 时用默认装配 - 路由第一层仍由调用方传入的 AccountKey(provider + platform)决定 快速出库(多平台通用,qps 10): - LingxingApi 新增 FAST_OUTBOUND / FAST_OUTBOUND_RESULT - core 新增 OutboundCapable / Outbounds / OutboundPackage / OutboundResult - ChannelSdk 新增 outbounds(key, conn) - 新增 platform/outbound/LingxingOutboundSource 能力接口 - 新增 platform/multiplatform/outbound/MultiPlatformOutboundSource (两个接口都不带平台维度,所有子平台共用) - 九个多平台子平台声明 outboundSource() - 适配器 capabilities() 增加 MARK_SHIPPED,supports() 按 outboundSource 判定 注意:submit 仅表示任务受理,结果需用 results() 回查。 Co-Authored-By: Claude <noreply@anthropic.com>feature/supplier-lingxing
24 changed files with 880 additions and 63 deletions
@ -0,0 +1,25 @@
@@ -0,0 +1,25 @@
|
||||
package common.ecommerce.capability; |
||||
|
||||
import common.ecommerce.model.fulfillment.OutboundPackage; |
||||
import common.ecommerce.model.fulfillment.OutboundResult; |
||||
import common.ecommerce.sdk.ChannelContext; |
||||
|
||||
import java.util.List; |
||||
|
||||
/** |
||||
* 快速出库能力:提交出库任务 + 查询任务结果。 |
||||
* |
||||
* <p>出库是<b>异步</b>的:{@link #submit} 只表示任务已受理,真正的成功/失败 |
||||
* 要用 {@link #results} 按系统单号回查。</p> |
||||
* |
||||
* <p>写操作,必须经 {@code IdempotencyGuard} 执行;幂等键按系统单号, |
||||
* 与领星「一个订单只能出库一次」的约束一致。</p> |
||||
*/ |
||||
public interface OutboundCapable extends PlatformAdapter { |
||||
|
||||
/** 提交出库任务;返回是否受理。 */ |
||||
boolean submit(ChannelContext ctx, List<OutboundPackage> packages); |
||||
|
||||
/** 按系统单号查询出库结果。 */ |
||||
OutboundResult results(ChannelContext ctx, List<String> globalOrderNos); |
||||
} |
||||
@ -0,0 +1,42 @@
@@ -0,0 +1,42 @@
|
||||
package common.ecommerce.capability; |
||||
|
||||
import common.ecommerce.error.ChannelException; |
||||
import common.ecommerce.model.Capability; |
||||
import common.ecommerce.model.account.ChannelAccount; |
||||
import common.ecommerce.model.fulfillment.OutboundPackage; |
||||
import common.ecommerce.model.fulfillment.OutboundResult; |
||||
import common.ecommerce.sdk.ChannelContext; |
||||
|
||||
import java.util.List; |
||||
|
||||
/** |
||||
* 门面持有的出库视图。调用前做能力检查,缺失时显式拒绝。 |
||||
*/ |
||||
public final class Outbounds { |
||||
|
||||
private final PlatformAdapter adapter; |
||||
private final ChannelAccount account; |
||||
private final ChannelContext ctx; |
||||
|
||||
public Outbounds(PlatformAdapter adapter, ChannelAccount account, ChannelContext ctx) { |
||||
this.adapter = adapter; |
||||
this.account = account; |
||||
this.ctx = ctx; |
||||
} |
||||
|
||||
public boolean submit(List<OutboundPackage> packages) { |
||||
requireOutboundCapable(); |
||||
return ((OutboundCapable) adapter).submit(ctx, packages); |
||||
} |
||||
|
||||
public OutboundResult results(List<String> globalOrderNos) { |
||||
requireOutboundCapable(); |
||||
return ((OutboundCapable) adapter).results(ctx, globalOrderNos); |
||||
} |
||||
|
||||
private void requireOutboundCapable() { |
||||
if (!adapter.supports(account, Capability.MARK_SHIPPED) || !(adapter instanceof OutboundCapable)) { |
||||
throw new ChannelException.UnsupportedCapabilityException(Capability.MARK_SHIPPED); |
||||
} |
||||
} |
||||
} |
||||
@ -0,0 +1,101 @@
@@ -0,0 +1,101 @@
|
||||
package common.ecommerce.model.fulfillment; |
||||
|
||||
import java.util.Objects; |
||||
|
||||
/** |
||||
* 一次快速出库请求中的单个包裹。 |
||||
* |
||||
* <p>字段以领星「快速出库」接口为准;各平台接入时由各自 Source 翻译。 |
||||
* {@code globalOrderNo} 是平台系统单号(领星侧 {@code global_order_no}), |
||||
* 也是出库结果的查询键。</p> |
||||
*/ |
||||
public final class OutboundPackage { |
||||
|
||||
private final String globalOrderNo; |
||||
private final Long warehouseId; |
||||
private final String logisticsTypeId; |
||||
private final String waybillNo; |
||||
private final String trackingNo; |
||||
private final String weightUnit; |
||||
private final String realWeight; |
||||
private final String sizeUnit; |
||||
private final String length; |
||||
private final String width; |
||||
private final String height; |
||||
private final String feeWeight; |
||||
private final String logisticsFreight; |
||||
private final String logisticsFreightCurrency; |
||||
|
||||
private OutboundPackage(Builder b) { |
||||
this.globalOrderNo = Objects.requireNonNull(b.globalOrderNo, "globalOrderNo"); |
||||
this.warehouseId = b.warehouseId; |
||||
this.logisticsTypeId = b.logisticsTypeId; |
||||
this.waybillNo = b.waybillNo; |
||||
this.trackingNo = b.trackingNo; |
||||
this.weightUnit = b.weightUnit; |
||||
this.realWeight = b.realWeight; |
||||
this.sizeUnit = b.sizeUnit; |
||||
this.length = b.length; |
||||
this.width = b.width; |
||||
this.height = b.height; |
||||
this.feeWeight = b.feeWeight; |
||||
this.logisticsFreight = b.logisticsFreight; |
||||
this.logisticsFreightCurrency = b.logisticsFreightCurrency; |
||||
} |
||||
|
||||
public static Builder builder() { |
||||
return new Builder(); |
||||
} |
||||
|
||||
public String globalOrderNo() { return globalOrderNo; } |
||||
public Long warehouseId() { return warehouseId; } |
||||
public String logisticsTypeId() { return logisticsTypeId; } |
||||
public String waybillNo() { return waybillNo; } |
||||
public String trackingNo() { return trackingNo; } |
||||
public String weightUnit() { return weightUnit; } |
||||
public String realWeight() { return realWeight; } |
||||
public String sizeUnit() { return sizeUnit; } |
||||
public String length() { return length; } |
||||
public String width() { return width; } |
||||
public String height() { return height; } |
||||
public String feeWeight() { return feeWeight; } |
||||
public String logisticsFreight() { return logisticsFreight; } |
||||
public String logisticsFreightCurrency() { return logisticsFreightCurrency; } |
||||
|
||||
public static final class Builder { |
||||
|
||||
private String globalOrderNo; |
||||
private Long warehouseId; |
||||
private String logisticsTypeId; |
||||
private String waybillNo; |
||||
private String trackingNo; |
||||
private String weightUnit; |
||||
private String realWeight; |
||||
private String sizeUnit; |
||||
private String length; |
||||
private String width; |
||||
private String height; |
||||
private String feeWeight; |
||||
private String logisticsFreight; |
||||
private String logisticsFreightCurrency; |
||||
|
||||
public Builder globalOrderNo(String v) { this.globalOrderNo = v; return this; } |
||||
public Builder warehouseId(Long v) { this.warehouseId = v; return this; } |
||||
public Builder logisticsTypeId(String v) { this.logisticsTypeId = v; return this; } |
||||
public Builder waybillNo(String v) { this.waybillNo = v; return this; } |
||||
public Builder trackingNo(String v) { this.trackingNo = v; return this; } |
||||
public Builder weightUnit(String v) { this.weightUnit = v; return this; } |
||||
public Builder realWeight(String v) { this.realWeight = v; return this; } |
||||
public Builder sizeUnit(String v) { this.sizeUnit = v; return this; } |
||||
public Builder length(String v) { this.length = v; return this; } |
||||
public Builder width(String v) { this.width = v; return this; } |
||||
public Builder height(String v) { this.height = v; return this; } |
||||
public Builder feeWeight(String v) { this.feeWeight = v; return this; } |
||||
public Builder logisticsFreight(String v) { this.logisticsFreight = v; return this; } |
||||
public Builder logisticsFreightCurrency(String v) { this.logisticsFreightCurrency = v; return this; } |
||||
|
||||
public OutboundPackage build() { |
||||
return new OutboundPackage(this); |
||||
} |
||||
} |
||||
} |
||||
@ -0,0 +1,57 @@
@@ -0,0 +1,57 @@
|
||||
package common.ecommerce.model.fulfillment; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.Collections; |
||||
import java.util.List; |
||||
|
||||
/** |
||||
* 快速出库结果:成功与失败两组,按系统单号对应。 |
||||
*/ |
||||
public final class OutboundResult { |
||||
|
||||
private final List<Success> success; |
||||
private final List<Failure> failure; |
||||
|
||||
private OutboundResult(List<Success> success, List<Failure> failure) { |
||||
this.success = Collections.unmodifiableList(new ArrayList<Success>(success)); |
||||
this.failure = Collections.unmodifiableList(new ArrayList<Failure>(failure)); |
||||
} |
||||
|
||||
public static OutboundResult of(List<Success> success, List<Failure> failure) { |
||||
return new OutboundResult(success, failure); |
||||
} |
||||
|
||||
public List<Success> success() { return success; } |
||||
public List<Failure> failure() { return failure; } |
||||
|
||||
/** 出库成功的单。 */ |
||||
public static final class Success { |
||||
|
||||
private final String globalOrderNo; |
||||
private final String woNumber; |
||||
|
||||
public Success(String globalOrderNo, String woNumber) { |
||||
this.globalOrderNo = globalOrderNo; |
||||
this.woNumber = woNumber; |
||||
} |
||||
|
||||
public String globalOrderNo() { return globalOrderNo; } |
||||
/** 销售出库单号。 */ |
||||
public String woNumber() { return woNumber; } |
||||
} |
||||
|
||||
/** 出库失败的单及原因。 */ |
||||
public static final class Failure { |
||||
|
||||
private final String globalOrderNo; |
||||
private final String errorMessage; |
||||
|
||||
public Failure(String globalOrderNo, String errorMessage) { |
||||
this.globalOrderNo = globalOrderNo; |
||||
this.errorMessage = errorMessage; |
||||
} |
||||
|
||||
public String globalOrderNo() { return globalOrderNo; } |
||||
public String errorMessage() { return errorMessage; } |
||||
} |
||||
} |
||||
@ -0,0 +1,40 @@
@@ -0,0 +1,40 @@
|
||||
package common.ecommerce.lingxing.platform; |
||||
|
||||
import common.ecommerce.http.JacksonJsonCodec; |
||||
import common.ecommerce.http.HttpTransport; |
||||
import common.ecommerce.lingxing.platform.aliexpress.AliexpressSubPlatform; |
||||
import common.ecommerce.lingxing.platform.amazon.sc.AmazonScSubPlatform; |
||||
import common.ecommerce.lingxing.platform.amazon.vc.AmazonVcSubPlatform; |
||||
import common.ecommerce.lingxing.platform.ebay.EbaySubPlatform; |
||||
import common.ecommerce.lingxing.platform.shein.SheinSubPlatform; |
||||
import common.ecommerce.lingxing.platform.shopify.ShopifySubPlatform; |
||||
import common.ecommerce.lingxing.platform.temu.TemuSubPlatform; |
||||
import common.ecommerce.lingxing.platform.tiktok.TiktokSubPlatform; |
||||
import common.ecommerce.lingxing.platform.walmart.WalmartSubPlatform; |
||||
|
||||
/** |
||||
* 领星子平台的默认装配:把当前已实现的子平台全部注册进来。 |
||||
* |
||||
* <p>要把某个子平台换掉或只装一部分(测试、灰度),直接用 |
||||
* {@link LingxingSubPlatforms#builder()} 自行组装,不要改这里。</p> |
||||
*/ |
||||
public final class LingxingSubPlatformDefaults { |
||||
|
||||
private LingxingSubPlatformDefaults() { |
||||
} |
||||
|
||||
/** 用给定的传输与编解码装配全部已实现子平台。 */ |
||||
public static LingxingSubPlatforms all(HttpTransport transport, JacksonJsonCodec json) { |
||||
return LingxingSubPlatforms.builder() |
||||
.register(new AmazonScSubPlatform(transport, json)) |
||||
.register(new AmazonVcSubPlatform(transport, json)) |
||||
.register(new WalmartSubPlatform(transport, json)) |
||||
.register(new TemuSubPlatform(transport, json)) |
||||
.register(new SheinSubPlatform(transport, json)) |
||||
.register(new ShopifySubPlatform(transport, json)) |
||||
.register(new EbaySubPlatform(transport, json)) |
||||
.register(new AliexpressSubPlatform(transport, json)) |
||||
.register(new TiktokSubPlatform(transport, json)) |
||||
.build(); |
||||
} |
||||
} |
||||
@ -0,0 +1,112 @@
@@ -0,0 +1,112 @@
|
||||
package common.ecommerce.lingxing.platform.multiplatform.outbound; |
||||
|
||||
import common.ecommerce.http.HttpRequest; |
||||
import common.ecommerce.http.HttpResponse; |
||||
import common.ecommerce.lingxing.LingxingApi; |
||||
import common.ecommerce.lingxing.http.LingxingEnvelope; |
||||
import common.ecommerce.lingxing.platform.LingxingCall; |
||||
import common.ecommerce.lingxing.platform.outbound.LingxingOutboundSource; |
||||
import common.ecommerce.model.fulfillment.OutboundPackage; |
||||
import common.ecommerce.model.fulfillment.OutboundResult; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.LinkedHashMap; |
||||
import java.util.List; |
||||
import java.util.Map; |
||||
|
||||
/** |
||||
* 领星 · 快速出库(多平台通用)。 |
||||
* |
||||
* <p>两个接口都不带平台维度,靠领星系统单号 {@code global_order_no} 定位订单, |
||||
* 因此所有子平台共用同一实现。</p> |
||||
* |
||||
* <ul> |
||||
* <li>{@code POST /pb/mp/order/v2/fastOutbound}:创建异步出库任务,返回 {@code data: true}</li> |
||||
* <li>{@code POST /pb/mp/order/v2/getFastOutboundResult}:按单号数组查结果, |
||||
* 返回 {@code data.success[]} 与 {@code data.failure[]}</li> |
||||
* </ul> |
||||
* |
||||
* <p>两个接口的令牌桶都是 10 req/s。</p> |
||||
*/ |
||||
public final class MultiPlatformOutboundSource implements LingxingOutboundSource { |
||||
|
||||
/** 单次最多 1000 单(两个接口一致)。 */ |
||||
private static final int MAX_BATCH = 1000; |
||||
|
||||
private static final String OP_SUBMIT = "outbound.submit"; |
||||
private static final String OP_RESULTS = "outbound.results"; |
||||
|
||||
@Override |
||||
public boolean submit(LingxingCall call, List<OutboundPackage> packages) { |
||||
if (packages == null || packages.isEmpty()) { |
||||
throw new IllegalArgumentException("packages must not be empty"); |
||||
} |
||||
if (packages.size() > MAX_BATCH) { |
||||
throw new IllegalArgumentException("at most " + MAX_BATCH + " packages per call, got " + packages.size()); |
||||
} |
||||
|
||||
List<Map<String, Object>> body = new ArrayList<Map<String, Object>>(packages.size()); |
||||
for (OutboundPackage p : packages) { |
||||
body.add(toBody(p)); |
||||
} |
||||
Map<String, Object> payload = new LinkedHashMap<String, Object>(); |
||||
payload.put("package", body); |
||||
|
||||
HttpRequest req = call.signed(LingxingApi.FAST_OUTBOUND, payload, OP_SUBMIT); |
||||
HttpResponse resp = call.execute(req); |
||||
LingxingEnvelope envelope = call.envelope(resp, OP_SUBMIT); |
||||
// 该接口 data 是 boolean,true 表示任务已受理(不代表出库成功)
|
||||
return "true".equalsIgnoreCase(call.json().stringAt(envelope.body(), "data")); |
||||
} |
||||
|
||||
@Override |
||||
public OutboundResult results(LingxingCall call, List<String> globalOrderNos) { |
||||
if (globalOrderNos == null || globalOrderNos.isEmpty()) { |
||||
throw new IllegalArgumentException("globalOrderNos must not be empty"); |
||||
} |
||||
if (globalOrderNos.size() > MAX_BATCH) { |
||||
throw new IllegalArgumentException("at most " + MAX_BATCH + " orders per call, got " + globalOrderNos.size()); |
||||
} |
||||
|
||||
Map<String, Object> payload = new LinkedHashMap<String, Object>(); |
||||
payload.put("global_order_no", new ArrayList<String>(globalOrderNos)); |
||||
|
||||
HttpRequest req = call.signed(LingxingApi.FAST_OUTBOUND_RESULT, payload, OP_RESULTS); |
||||
HttpResponse resp = call.execute(req); |
||||
LingxingEnvelope envelope = call.envelope(resp, OP_RESULTS); |
||||
|
||||
List<OutboundOkDto> ok = call.json().listAt(envelope.body(), "data.success", OutboundOkDto.class); |
||||
List<OutboundFailDto> fail = call.json().listAt(envelope.body(), "data.failure", OutboundFailDto.class); |
||||
|
||||
List<OutboundResult.Success> success = new ArrayList<OutboundResult.Success>(ok.size()); |
||||
for (OutboundOkDto dto : ok) { |
||||
success.add(new OutboundResult.Success(dto.getGlobalOrderNo(), dto.getWoNumber())); |
||||
} |
||||
List<OutboundResult.Failure> failure = new ArrayList<OutboundResult.Failure>(fail.size()); |
||||
for (OutboundFailDto dto : fail) { |
||||
failure.add(new OutboundResult.Failure(dto.getGlobalOrderNo(), dto.getErrorMessage())); |
||||
} |
||||
return OutboundResult.of(success, failure); |
||||
} |
||||
|
||||
/** 统一模型 → 领星出库请求体;值为 null 的字段不发送。 */ |
||||
private static Map<String, Object> toBody(OutboundPackage p) { |
||||
Map<String, Object> item = new LinkedHashMap<String, Object>(); |
||||
item.put("global_order_no", p.globalOrderNo()); |
||||
item.put("wid", p.warehouseId()); |
||||
item.put("logistics_type_id", p.logisticsTypeId()); |
||||
item.put("waybill_no", p.waybillNo()); |
||||
item.put("tracking_no", p.trackingNo()); |
||||
item.put("weight_unit", p.weightUnit()); |
||||
item.put("real_weight", p.realWeight()); |
||||
item.put("size_unit", p.sizeUnit()); |
||||
item.put("length", p.length()); |
||||
item.put("width", p.width()); |
||||
item.put("height", p.height()); |
||||
item.put("fee_weight", p.feeWeight()); |
||||
item.put("logistics_freight", p.logisticsFreight()); |
||||
item.put("logistics_freight_currency_code", p.logisticsFreightCurrency()); |
||||
item.values().removeAll(java.util.Collections.singleton(null)); |
||||
return item; |
||||
} |
||||
} |
||||
@ -0,0 +1,21 @@
@@ -0,0 +1,21 @@
|
||||
package common.ecommerce.lingxing.platform.multiplatform.outbound; |
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; |
||||
import com.fasterxml.jackson.annotation.JsonProperty; |
||||
import lombok.Getter; |
||||
import lombok.Setter; |
||||
|
||||
/** 快速出库结果 · 失败项({@code data.failure[]})。 */ |
||||
@Getter |
||||
@Setter |
||||
@JsonIgnoreProperties(ignoreUnknown = true) |
||||
public class OutboundFailDto { |
||||
|
||||
/** 系统单号。 */ |
||||
@JsonProperty("global_order_no") |
||||
private String globalOrderNo; |
||||
|
||||
/** 失败原因,如「正在处理」「库存不足」「订单没提交快速出库」。 */ |
||||
@JsonProperty("error_message") |
||||
private String errorMessage; |
||||
} |
||||
@ -0,0 +1,21 @@
@@ -0,0 +1,21 @@
|
||||
package common.ecommerce.lingxing.platform.multiplatform.outbound; |
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; |
||||
import com.fasterxml.jackson.annotation.JsonProperty; |
||||
import lombok.Getter; |
||||
import lombok.Setter; |
||||
|
||||
/** 快速出库结果 · 成功项({@code data.success[]})。 */ |
||||
@Getter |
||||
@Setter |
||||
@JsonIgnoreProperties(ignoreUnknown = true) |
||||
public class OutboundOkDto { |
||||
|
||||
/** 系统单号。 */ |
||||
@JsonProperty("global_order_no") |
||||
private String globalOrderNo; |
||||
|
||||
/** 销售出库单号。 */ |
||||
@JsonProperty("wo_number") |
||||
private String woNumber; |
||||
} |
||||
@ -0,0 +1,21 @@
@@ -0,0 +1,21 @@
|
||||
package common.ecommerce.lingxing.platform.outbound; |
||||
|
||||
import common.ecommerce.lingxing.platform.LingxingCall; |
||||
import common.ecommerce.model.fulfillment.OutboundPackage; |
||||
import common.ecommerce.model.fulfillment.OutboundResult; |
||||
|
||||
import java.util.List; |
||||
|
||||
/** |
||||
* 子平台的出库能力。 |
||||
* |
||||
* <p>出库是异步的:{@code submit} 只表示任务受理,结果用 {@code results} 回查。</p> |
||||
*/ |
||||
public interface LingxingOutboundSource { |
||||
|
||||
/** 提交出库任务;返回是否受理。 */ |
||||
boolean submit(LingxingCall call, List<OutboundPackage> packages); |
||||
|
||||
/** 按系统单号查询出库结果。 */ |
||||
OutboundResult results(LingxingCall call, List<String> globalOrderNos); |
||||
} |
||||
@ -0,0 +1,229 @@
@@ -0,0 +1,229 @@
|
||||
package common.ecommerce.lingxing.platform.amazon.sc; |
||||
|
||||
import common.ecommerce.capability.AggregatorCapable.SubChannel; |
||||
import common.ecommerce.http.HttpRequest; |
||||
import common.ecommerce.http.HttpResponse; |
||||
import common.ecommerce.http.HttpTransport; |
||||
import common.ecommerce.http.JacksonJsonCodec; |
||||
import common.ecommerce.http.OkHttpTransport; |
||||
import common.ecommerce.lingxing.LingxingConstants; |
||||
import common.ecommerce.lingxing.adapter.LingxingAdapter; |
||||
import common.ecommerce.model.ChannelProvider; |
||||
import common.ecommerce.model.Platform; |
||||
import common.ecommerce.model.account.AccountKey; |
||||
import common.ecommerce.model.account.ChannelAccount; |
||||
import common.ecommerce.model.product.ProductListing; |
||||
import common.ecommerce.model.product.ProductQuery; |
||||
import common.ecommerce.model.support.Page; |
||||
import common.ecommerce.sdk.ChannelConnection; |
||||
import common.ecommerce.sdk.ChannelContext; |
||||
import common.ecommerce.spi.ChannelSecret; |
||||
import common.ecommerce.testkit.InMemoryLockProvider; |
||||
import common.ecommerce.testkit.InMemoryTokenStore; |
||||
import org.junit.jupiter.api.Test; |
||||
|
||||
import java.time.Clock; |
||||
import java.time.Duration; |
||||
import java.time.Instant; |
||||
import java.util.List; |
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse; |
||||
import static org.junit.jupiter.api.Assertions.assertNotNull; |
||||
|
||||
/** |
||||
* 领星 · 亚马逊 SC 真实环境测试:先查店铺列表拿 sid,再用 sid 拉 Listing。 |
||||
* |
||||
* <p><b>跑之前填 APP_KEY / APP_SECRET 即可</b>;SID 留空时会自动从店铺列表取第一个。</p> |
||||
* <pre> |
||||
* mvn -pl lingxing -am test -Dtest=AmazonScProductLiveTest |
||||
* </pre> |
||||
* |
||||
* <p>调用前确认出口 IP 已加入领星白名单。本类只用于联调,不要把真实凭证提交到仓库。</p> |
||||
* |
||||
* <p>两个接口的令牌桶容量都是 1,且按「账户 + path」共享,必须串行调用。</p> |
||||
*/ |
||||
class AmazonScProductLiveTest { |
||||
|
||||
// ==================== 填写这里 ====================
|
||||
/** 领星 ERP 开放接口的 AppID。 */ |
||||
private static final String APP_KEY = "ak_MWoHJz2tcIwKF"; |
||||
/** 领星 ERP 开放接口的 AppSecret。 */ |
||||
private static final String APP_SECRET = "YOUkd5efWJSK71jLvDyz+g=="; |
||||
/** |
||||
* 亚马逊店铺的领星 sid。 |
||||
* 留空则自动调用店铺列表接口,取第一个正常状态的店铺。 |
||||
*/ |
||||
private static final String SID = "13839"; |
||||
// ================================================
|
||||
|
||||
private static final String BASE_URL = LingxingConstants.PRODUCTION_BASE_URL; |
||||
|
||||
private final JacksonJsonCodec json = new JacksonJsonCodec(); |
||||
private final LoggingTransport transport = |
||||
new LoggingTransport(new OkHttpTransport("common-ecommerce-smoke")); |
||||
|
||||
/** |
||||
* 店铺列表:一次性返回企业已授权到领星的全部亚马逊 SC 店铺。 |
||||
* |
||||
* <p>接口 {@code GET /erp/sc/data/seller/lists},非分页。打印每个店铺的 sid 与名称, |
||||
* 同时打印请求与响应原始报文(见 LoggingTransport),便于核对字段名。</p> |
||||
*/ |
||||
@Test |
||||
void listsAmazonShops() { |
||||
requireConfigured(); |
||||
|
||||
List<SubChannel> channels = adapter().listSubChannels(shopListContext()); |
||||
System.out.println("[shops] subChannels size=" + channels.size()); |
||||
for (SubChannel c : channels) { |
||||
System.out.println(" platformCode=" + c.platformCode() |
||||
+ " sid=" + c.externalStoreId() |
||||
+ " platform=" + c.platform() |
||||
+ " name=" + c.name()); |
||||
} |
||||
assertNotNull(channels); |
||||
} |
||||
|
||||
/** 拿第一个正常店铺的 sid,调 Listing 接口。 */ |
||||
@Test |
||||
void pullsOnePageOfListings() { |
||||
requireConfigured(); |
||||
|
||||
String sid = resolveSid(); |
||||
System.out.println("[listings] 使用 sid=" + sid); |
||||
|
||||
ChannelContext ctx = ChannelContext.of(amazonAccount(sid), connection(), |
||||
"smoke-" + System.currentTimeMillis(), Clock.systemUTC()); |
||||
|
||||
Instant to = Instant.now(); |
||||
Instant from = to.minus(Duration.ofDays(7)); |
||||
|
||||
Page<ProductListing> page = adapter().pull(ctx, ProductQuery.builder() |
||||
.updatedAfter(from) |
||||
.updatedBefore(to) |
||||
.pageSize(20) |
||||
.build()); |
||||
|
||||
System.out.println("[listings] window=" + from + " .. " + to |
||||
+ " items=" + page.size() + " nextCursor=" + page.nextCursor()); |
||||
for (ProductListing p : page.items()) { |
||||
System.out.println(" sku=" + p.platformSku() |
||||
+ " title=" + p.title() |
||||
+ " status=" + p.status() |
||||
+ " images=" + p.imageUrls().size() |
||||
+ " updatedAt=" + (p.updatedAt() == null ? null : p.updatedAt().utc())); |
||||
} |
||||
assertFalse(page.size() > 20, "pageSize 未被平台遵守"); |
||||
} |
||||
|
||||
/** 不带时间窗,确认默认分页与解析也能走通。 */ |
||||
@Test |
||||
void pullsFirstPageWithoutTimeWindow() { |
||||
requireConfigured(); |
||||
|
||||
String sid = resolveSid(); |
||||
ChannelContext ctx = ChannelContext.of(amazonAccount(sid), connection(), |
||||
"smoke-" + System.currentTimeMillis(), Clock.systemUTC()); |
||||
|
||||
Page<ProductListing> page = adapter().pull(ctx, ProductQuery.builder().pageSize(10).build()); |
||||
|
||||
System.out.println("[listings/no-window] sid=" + sid + " items=" + page.size() |
||||
+ " nextCursor=" + page.nextCursor()); |
||||
for (ProductListing p : page.items()) { |
||||
System.out.println(" sku=" + p.platformSku() + " status=" + p.status()); |
||||
} |
||||
// 店铺可能没有商品,空列表也算通过
|
||||
assertNotNull(page); |
||||
} |
||||
|
||||
// ---------------------------------------------------------------- helpers
|
||||
|
||||
private static void requireConfigured() { |
||||
org.junit.jupiter.api.Assumptions.assumeTrue( |
||||
!APP_KEY.startsWith("在这里填") && !APP_SECRET.startsWith("在这里填"), |
||||
"请先填写 AmazonScProductLiveTest 顶部的 APP_KEY / APP_SECRET"); |
||||
} |
||||
|
||||
/** 优先用常量里的 SID;留空则从店铺列表取第一个。 */ |
||||
private String resolveSid() { |
||||
if (SID != null && !SID.trim().isEmpty() && !SID.startsWith("在这里填")) { |
||||
return SID.trim(); |
||||
} |
||||
List<SubChannel> channels = adapter().listSubChannels(shopListContext()); |
||||
if (channels.isEmpty()) { |
||||
throw new AssertionError("店铺列表为空:该企业没有已授权且状态正常的亚马逊 SC 店铺"); |
||||
} |
||||
return channels.get(0).externalStoreId(); |
||||
} |
||||
|
||||
private LingxingAdapter adapter() { |
||||
return new LingxingAdapter(transport, json, |
||||
new InMemoryTokenStore(), new InMemoryLockProvider(), Clock.systemUTC()); |
||||
} |
||||
|
||||
private ChannelConnection connection() { |
||||
return ChannelConnection.of(BASE_URL, ChannelSecret.builder() |
||||
.appKey(APP_KEY).appSecret(APP_SECRET).build()); |
||||
} |
||||
|
||||
/** 店铺列表所用的上下文:店铺维度此时还不存在,用占位 omsStoreId。 */ |
||||
private ChannelContext shopListContext() { |
||||
return ChannelContext.of(placeholderAccount(), connection(), |
||||
"smoke-shops-" + System.currentTimeMillis(), Clock.systemUTC()); |
||||
} |
||||
|
||||
private ChannelAccount placeholderAccount() { |
||||
return ChannelAccount.builder() |
||||
.key(AccountKey.of(ChannelProvider.LINGXING, Platform.AMAZON_SC, "oms-amz-discovery")) |
||||
.externalStoreId("discovery") |
||||
.platformCode(10001) |
||||
.credentialRef("smoke") |
||||
.marketplaceZone("America/Los_Angeles") |
||||
.currency("USD") |
||||
.build(); |
||||
} |
||||
|
||||
private ChannelAccount amazonAccount(String sid) { |
||||
return ChannelAccount.builder() |
||||
.key(AccountKey.of(ChannelProvider.LINGXING, Platform.AMAZON_SC, "oms-amz-smoke")) |
||||
.externalStoreId(sid) |
||||
.platformCode(10001) |
||||
.credentialRef("smoke") |
||||
.marketplaceZone("America/Los_Angeles") |
||||
.currency("USD") |
||||
.build(); |
||||
} |
||||
|
||||
/** 打印真实请求与响应,签名/字段名不对时靠这个定位。Authorization 只打指纹。 */ |
||||
static final class LoggingTransport implements HttpTransport { |
||||
|
||||
private static final int MAX_BODY = 2000; |
||||
|
||||
private final HttpTransport delegate; |
||||
|
||||
LoggingTransport(HttpTransport delegate) { |
||||
this.delegate = delegate; |
||||
} |
||||
|
||||
@Override |
||||
public HttpResponse execute(HttpRequest request) { |
||||
System.out.println("--> " + request.method() + " " + request.url()); |
||||
System.out.println(" headers " + mask(request.headers().toString())); |
||||
System.out.println(" body " + truncate(request.body())); |
||||
HttpResponse response = delegate.execute(request); |
||||
System.out.println("<-- " + response.statusCode() + " (" + response.latencyMillis() + " ms) " |
||||
+ truncate(response.body())); |
||||
return response; |
||||
} |
||||
|
||||
private static String mask(String headers) { |
||||
return headers.replaceAll("(?i)(bearer\\s+)[^,\\]]+", "$1***"); |
||||
} |
||||
|
||||
private static String truncate(String body) { |
||||
if (body == null) { |
||||
return "null"; |
||||
} |
||||
return body.length() <= MAX_BODY ? body : body.substring(0, MAX_BODY) + "...(" + body.length() + " chars)"; |
||||
} |
||||
} |
||||
} |
||||
Loading…
Reference in new issue