Browse Source
结构: - 新增 LingxingApi 枚举:路径 + 限流,一条路径只写一次 - 新增 platform/ 包:LingxingSubPlatform 接口、LingxingSubPlatforms 注册表、 LingxingCall 调用上下文、walmart/WalmartSubPlatform 首个实现 - LingxingAdapter.pull 改为按 Platform 分派,supports 委托子平台 删除假数据: - LingxingApi 里凭空猜测的 SHOP_LIST_* / PRODUCT_LIST_* 路径 - LingxingAdapter.listSubChannels 由账户拼出的假子渠道,改为显式抛异常 - LingxingConstants 中与 LingxingApi 重复的路径与限流常量 - README / GUIDE 中已删除的 FakeTransport、Fixtures 相关内容 Co-Authored-By: Claude <noreply@anthropic.com>feature/supplier-lingxing
10 changed files with 390 additions and 142 deletions
@ -0,0 +1,42 @@
@@ -0,0 +1,42 @@
|
||||
package common.ecommerce.lingxing; |
||||
|
||||
/** |
||||
* 领星开放接口目录:路径 + 限流值。 |
||||
* |
||||
* <p><b>一条路径只写一次。</b>子平台通过 {@code LingxingSubPlatforms} 引用需要的条目, |
||||
* 不在平台枚举里重复写字符串。</p> |
||||
* |
||||
* <p>路径按领星侧的子平台分组(如 {@code /pb/mp/amazon/...} 与 {@code /pb/mp/walmart/...}), |
||||
* 若某天两个子平台共用一个路径,让两个平台引用同一个枚举值即可,不必新增条目。</p> |
||||
* |
||||
* <p>名字里的 AMAZON / WALMART 指<b>领星侧的子平台</b>,与业务枚举 {@code Platform} 的对应关系 |
||||
* 见 {@code LingxingSubPlatforms}。</p> |
||||
*/ |
||||
public enum LingxingApi { |
||||
|
||||
/** 换取令牌;所有子平台共用。 */ |
||||
TOKEN("/api/auth-server/oauth/access-token", 100), |
||||
|
||||
// 店铺列表、商品列表的路径尚未核对,不预置占位值;
|
||||
// 拿到官方文档后在此追加,一条路径只写一次。
|
||||
|
||||
/** 多平台订单列表;当前实现使用的跨平台接口。 */ |
||||
ORDER_LIST("/pb/mp/order/v2/list", 10); |
||||
|
||||
private final String path; |
||||
private final int permitsPerSecond; |
||||
|
||||
LingxingApi(String path, int permitsPerSecond) { |
||||
this.path = path; |
||||
this.permitsPerSecond = permitsPerSecond; |
||||
} |
||||
|
||||
public String path() { |
||||
return path; |
||||
} |
||||
|
||||
/** 该接口的令牌桶容量(req/s),限流器按 path 维度使用。 */ |
||||
public int permitsPerSecond() { |
||||
return permitsPerSecond; |
||||
} |
||||
} |
||||
@ -0,0 +1,70 @@
@@ -0,0 +1,70 @@
|
||||
package common.ecommerce.lingxing.platform; |
||||
|
||||
import common.ecommerce.http.HttpRequest; |
||||
import common.ecommerce.http.HttpResponse; |
||||
import common.ecommerce.http.JacksonJsonCodec; |
||||
import common.ecommerce.lingxing.LingxingApi; |
||||
import common.ecommerce.lingxing.http.LingxingEnvelope; |
||||
import common.ecommerce.lingxing.http.LingxingRateLimiter; |
||||
import common.ecommerce.lingxing.http.LingxingRequestFactory; |
||||
import common.ecommerce.sdk.ChannelContext; |
||||
import common.ecommerce.spi.AccessToken; |
||||
import common.ecommerce.spi.ChannelSecret; |
||||
|
||||
import java.util.Map; |
||||
|
||||
/** |
||||
* 一次调用所需的公共设施,交给子平台实现使用。 |
||||
* |
||||
* <p>把「取 token、限流、签名、校验信封」这些每个子平台都要做的事收在一处, |
||||
* 子平台实现只需要关心<b>拼请求体</b>和<b>解析响应</b>。</p> |
||||
*/ |
||||
public final class LingxingCall { |
||||
|
||||
private final ChannelContext ctx; |
||||
private final AccessToken token; |
||||
private final LingxingRequestFactory requestFactory; |
||||
private final LingxingRateLimiter rateLimiter; |
||||
private final JacksonJsonCodec json; |
||||
|
||||
public LingxingCall(ChannelContext ctx, AccessToken token, LingxingRequestFactory requestFactory, |
||||
LingxingRateLimiter rateLimiter, JacksonJsonCodec json) { |
||||
this.ctx = ctx; |
||||
this.token = token; |
||||
this.requestFactory = requestFactory; |
||||
this.rateLimiter = rateLimiter; |
||||
this.json = json; |
||||
} |
||||
|
||||
public ChannelContext ctx() { |
||||
return ctx; |
||||
} |
||||
|
||||
public ChannelSecret secret() { |
||||
return ctx.secret(); |
||||
} |
||||
|
||||
public AccessToken token() { |
||||
return token; |
||||
} |
||||
|
||||
public JacksonJsonCodec json() { |
||||
return json; |
||||
} |
||||
|
||||
/** 按接口自己的令牌桶限流,然后构造已签名的请求。 */ |
||||
public HttpRequest signed(LingxingApi api, Map<String, Object> body, String operation) { |
||||
rateLimiter.acquire(api.path(), api.permitsPerSecond()); |
||||
return requestFactory.post(ctx.endpointBaseUrl(), api.path(), body, ctx.secret(), token, ctx, operation); |
||||
} |
||||
|
||||
/** 校验 HTTP 状态与业务码,返回原始 body 供子平台解析。 */ |
||||
public String body(HttpResponse response, String operation) { |
||||
return LingxingEnvelope.of(response, json, operation, ctx.omsStoreId(), ctx.requestId()).body(); |
||||
} |
||||
|
||||
/** 同上,同时给出 data.total;接口没有 total 时为负数。 */ |
||||
public LingxingEnvelope envelope(HttpResponse response, String operation) { |
||||
return LingxingEnvelope.of(response, json, operation, ctx.omsStoreId(), ctx.requestId()); |
||||
} |
||||
} |
||||
@ -0,0 +1,56 @@
@@ -0,0 +1,56 @@
|
||||
package common.ecommerce.lingxing.platform; |
||||
|
||||
import common.ecommerce.capability.AggregatorCapable; |
||||
import common.ecommerce.model.Capability; |
||||
import common.ecommerce.model.Platform; |
||||
import common.ecommerce.model.order.SalesOrder; |
||||
import common.ecommerce.model.order.SalesOrderQuery; |
||||
import common.ecommerce.model.product.ProductListing; |
||||
import common.ecommerce.model.product.ProductQuery; |
||||
import common.ecommerce.model.support.Page; |
||||
|
||||
import java.util.List; |
||||
|
||||
/** |
||||
* 领星侧的一个子平台(亚马逊、沃尔玛、eBay……)。 |
||||
* |
||||
* <p>同一个聚合器下各子平台的接口路径、请求体、响应字段都不同,差异全部收敛到实现里; |
||||
* {@code LingxingAdapter} 只做分派。</p> |
||||
* |
||||
* <p>不支持的能力不覆写即可,默认实现直接抛 |
||||
* {@code ChannelException.UnsupportedCapabilityException}——不静默返回空。</p> |
||||
*/ |
||||
public interface LingxingSubPlatform { |
||||
|
||||
/** 对应的业务平台,用于注册表与日志。 */ |
||||
Platform platform(); |
||||
|
||||
/** 这个子平台实际支持哪些能力;{@code LingxingAdapter.supports} 据此细化。 */ |
||||
boolean supports(Capability capability); |
||||
|
||||
/** 销售订单分页拉取。 */ |
||||
default Page<SalesOrder> orders(LingxingCall call, SalesOrderQuery query) { |
||||
throw unsupported(Capability.SALES_ORDER); |
||||
} |
||||
|
||||
/** 单订单详情。 */ |
||||
default SalesOrder order(LingxingCall call, String channelOrderId) { |
||||
throw unsupported(Capability.SALES_ORDER); |
||||
} |
||||
|
||||
/** 商品 / Listing 分页拉取。 */ |
||||
default Page<ProductListing> products(LingxingCall call, ProductQuery query) { |
||||
throw unsupported(Capability.PRODUCT); |
||||
} |
||||
|
||||
/** 店铺(子渠道)列表。 */ |
||||
default List<AggregatorCapable.SubChannel> shops(LingxingCall call) { |
||||
throw unsupported(Capability.SALES_ORDER); |
||||
} |
||||
|
||||
/** 缺失能力的统一异常,避免各实现各写一遍。 */ |
||||
static common.ecommerce.error.ChannelException.UnsupportedCapabilityException unsupported( |
||||
Capability capability) { |
||||
return new common.ecommerce.error.ChannelException.UnsupportedCapabilityException(capability); |
||||
} |
||||
} |
||||
@ -0,0 +1,69 @@
@@ -0,0 +1,69 @@
|
||||
package common.ecommerce.lingxing.platform; |
||||
|
||||
import common.ecommerce.error.ChannelException; |
||||
import common.ecommerce.http.JacksonJsonCodec; |
||||
import common.ecommerce.http.OkHttpTransport; |
||||
import common.ecommerce.model.Platform; |
||||
import common.ecommerce.lingxing.platform.walmart.WalmartSubPlatform; |
||||
|
||||
import java.util.Collections; |
||||
import java.util.EnumMap; |
||||
import java.util.Map; |
||||
import java.util.Set; |
||||
|
||||
/** |
||||
* 子平台注册表:业务平台 → 领星侧实现。 |
||||
* |
||||
* <p>新增一个子平台 = 写一个 {@link LingxingSubPlatform} 实现 + 在这里登记一行。 |
||||
* 未登记的平台在调用时显式失败,不静默返回空。</p> |
||||
*/ |
||||
public final class LingxingSubPlatforms { |
||||
|
||||
private static final Map<Platform, LingxingSubPlatform> BY_PLATFORM; |
||||
|
||||
/** |
||||
* 子平台实现共享同一套传输与编解码;端点与凭据不在这里,随每次调用传入。 |
||||
*/ |
||||
private static final OkHttpTransport SHARED_TRANSPORT = new OkHttpTransport("common-ecommerce/1.0.0 (lingxing)"); |
||||
private static final JacksonJsonCodec SHARED_JSON = new JacksonJsonCodec(); |
||||
|
||||
static { |
||||
Map<Platform, LingxingSubPlatform> map = new EnumMap<Platform, LingxingSubPlatform>(Platform.class); |
||||
register(map, new WalmartSubPlatform(SHARED_TRANSPORT, SHARED_JSON)); |
||||
// 亚马逊、eBay、SHEIN… 落地后继续 register(...)
|
||||
BY_PLATFORM = Collections.unmodifiableMap(map); |
||||
} |
||||
|
||||
private static void register(Map<Platform, LingxingSubPlatform> map, LingxingSubPlatform impl) { |
||||
map.put(impl.platform(), impl); |
||||
} |
||||
|
||||
private LingxingSubPlatforms() { |
||||
} |
||||
|
||||
/** |
||||
* 按业务平台取实现;未登记时抛带平台信息的异常,便于定位是哪个子平台还没接。 |
||||
*/ |
||||
public static LingxingSubPlatform of(Platform platform) { |
||||
LingxingSubPlatform impl = BY_PLATFORM.get(platform); |
||||
if (impl == null) { |
||||
throw ChannelException.builder() |
||||
.message("lingxing sub platform not implemented: " + platform) |
||||
.operation("subPlatform.resolve") |
||||
.provider("lingxing") |
||||
.platform(platform) |
||||
.retryable(false) |
||||
.build(); |
||||
} |
||||
return impl; |
||||
} |
||||
|
||||
public static boolean isRegistered(Platform platform) { |
||||
return BY_PLATFORM.containsKey(platform); |
||||
} |
||||
|
||||
/** 已登记的平台,便于能力报告与日志。 */ |
||||
public static Set<Platform> registered() { |
||||
return BY_PLATFORM.keySet(); |
||||
} |
||||
} |
||||
@ -0,0 +1,108 @@
@@ -0,0 +1,108 @@
|
||||
package common.ecommerce.lingxing.platform.walmart; |
||||
|
||||
import common.ecommerce.http.HttpRequest; |
||||
import common.ecommerce.http.HttpResponse; |
||||
import common.ecommerce.http.HttpTransport; |
||||
import common.ecommerce.http.JacksonJsonCodec; |
||||
import common.ecommerce.lingxing.LingxingApi; |
||||
import common.ecommerce.lingxing.LingxingConstants; |
||||
import common.ecommerce.lingxing.dto.LingxingOrderDto; |
||||
import common.ecommerce.lingxing.http.LingxingEnvelope; |
||||
import common.ecommerce.lingxing.mapper.LingxingOrderMapper; |
||||
import common.ecommerce.lingxing.platform.LingxingCall; |
||||
import common.ecommerce.lingxing.platform.LingxingSubPlatform; |
||||
import common.ecommerce.model.Capability; |
||||
import common.ecommerce.model.Platform; |
||||
import common.ecommerce.model.account.ChannelAccount; |
||||
import common.ecommerce.model.order.SalesOrder; |
||||
import common.ecommerce.model.order.SalesOrderQuery; |
||||
import common.ecommerce.model.support.Page; |
||||
|
||||
import java.time.Duration; |
||||
import java.time.Instant; |
||||
import java.time.ZoneId; |
||||
import java.util.ArrayList; |
||||
import java.util.Collections; |
||||
import java.util.LinkedHashMap; |
||||
import java.util.List; |
||||
import java.util.Map; |
||||
|
||||
/** |
||||
* 领星 · 沃尔玛子平台。 |
||||
* |
||||
* <p>当前复用领星跨平台的订单列表接口 {@link LingxingApi#ORDER_LIST}; |
||||
* 沃尔玛若确认为独立订单接口,只需把 {@link #orderApi()} 换成对应枚举值。</p> |
||||
*/ |
||||
public final class WalmartSubPlatform implements LingxingSubPlatform { |
||||
|
||||
private final HttpTransport transport; |
||||
private final LingxingOrderMapper mapper; |
||||
|
||||
public WalmartSubPlatform(HttpTransport transport, JacksonJsonCodec json) { |
||||
this.transport = transport; |
||||
this.mapper = new LingxingOrderMapper(json); |
||||
} |
||||
|
||||
/** 该子平台使用的订单接口;当前为领星统一接口。 */ |
||||
protected LingxingApi orderApi() { |
||||
return LingxingApi.ORDER_LIST; |
||||
} |
||||
|
||||
@Override |
||||
public Platform platform() { |
||||
return Platform.WALMART_SC; |
||||
} |
||||
|
||||
@Override |
||||
public boolean supports(Capability capability) { |
||||
return capability == Capability.SALES_ORDER; |
||||
} |
||||
|
||||
@Override |
||||
public Page<SalesOrder> orders(LingxingCall call, SalesOrderQuery query) { |
||||
ChannelAccount account = call.ctx().account(); |
||||
ZoneId zone = ZoneId.of(account.marketplaceZone()); |
||||
|
||||
Instant from = query.updatedAfter(); |
||||
Instant to = query.updatedBefore() != null ? query.updatedBefore() : Instant.now(call.ctx().clock()); |
||||
if (Duration.between(from, to).toDays() > LingxingConstants.MAX_QUERY_DAYS) { |
||||
from = to.minus(Duration.ofDays(LingxingConstants.MAX_QUERY_DAYS)); |
||||
} |
||||
|
||||
int length = Math.min(query.pageSize() <= 0 ? 100 : query.pageSize(), LingxingConstants.MAX_PAGE_SIZE); |
||||
int offset = query.cursor() == null ? 0 : Integer.parseInt(query.cursor()); |
||||
Map<String, Object> body = orderBody(account, from, to, offset, length); |
||||
|
||||
HttpRequest req = call.signed(orderApi(), body, "salesOrders.pull"); |
||||
HttpResponse resp = transport.execute(req); |
||||
LingxingEnvelope envelope = call.envelope(resp, "salesOrders.pull"); |
||||
|
||||
List<LingxingOrderDto> dtos = mapper.parseList(envelope.body()); |
||||
List<SalesOrder> orders = new ArrayList<SalesOrder>(dtos.size()); |
||||
for (LingxingOrderDto dto : dtos) { |
||||
orders.add(mapper.toSalesOrder(dto, account.omsStoreId(), zone)); |
||||
} |
||||
|
||||
int nextOffset = offset + dtos.size(); |
||||
boolean hasMore = dtos.size() >= length |
||||
&& (envelope.total() < 0 || nextOffset < envelope.total()); |
||||
return Page.of(orders, hasMore ? String.valueOf(nextOffset) : null); |
||||
} |
||||
|
||||
private Map<String, Object> orderBody(ChannelAccount account, Instant from, Instant to, |
||||
int offset, int length) { |
||||
Map<String, Object> body = new LinkedHashMap<String, Object>(); |
||||
body.put("offset", offset); |
||||
body.put("length", length); |
||||
body.put("date_type", "update_time"); |
||||
body.put("start_time", from.getEpochSecond()); |
||||
body.put("end_time", to.getEpochSecond()); |
||||
if (account.platformCode() != null) { |
||||
body.put("platform_code", Collections.singletonList(account.platformCode())); |
||||
} |
||||
if (account.externalStoreId() != null) { |
||||
body.put("store_id", Collections.singletonList(account.externalStoreId())); |
||||
} |
||||
return body; |
||||
} |
||||
} |
||||
Loading…
Reference in new issue