Client.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. declare(strict_types=1);
  3. namespace SixShop\Wangdian;
  4. use Psr\Http\Client\ClientInterface;
  5. use Psr\Log\LoggerInterface;
  6. use SixShop\Wangdian\Auth\Authenticator;
  7. use SixShop\Wangdian\Config\Config;
  8. use SixShop\Wangdian\Http\HttpClient;
  9. use SixShop\Wangdian\Response\ApiResponse;
  10. use SixShop\Wangdian\Response\ResponseHandler;
  11. use SixShop\Wangdian\Services\BasicService;
  12. use SixShop\Wangdian\Services\GoodsService;
  13. use SixShop\Wangdian\Services\PurchaseService;
  14. use SixShop\Wangdian\Services\RefundService;
  15. use SixShop\Wangdian\Services\StockService;
  16. use SixShop\Wangdian\Services\TradeService;
  17. /**
  18. * Main SDK client for Wangdian API
  19. */
  20. class Client
  21. {
  22. private readonly HttpClient $httpClient;
  23. private readonly Authenticator $authenticator;
  24. private readonly ResponseHandler $responseHandler;
  25. // Service instances
  26. private ?BasicService $basicService = null;
  27. private ?GoodsService $goodsService = null;
  28. private ?PurchaseService $purchaseService = null;
  29. private ?RefundService $refundService = null;
  30. private ?StockService $stockService = null;
  31. private ?TradeService $tradeService = null;
  32. public function __construct(
  33. private readonly Config $config,
  34. ?ClientInterface $httpClient = null,
  35. ?LoggerInterface $logger = null
  36. ) {
  37. $this->httpClient = new HttpClient($this->config, $httpClient, $logger);
  38. $this->authenticator = new Authenticator($this->config);
  39. $this->responseHandler = new ResponseHandler();
  40. }
  41. /**
  42. * Make a raw API call
  43. */
  44. public function call(string $endpoint, array $params = []): ApiResponse
  45. {
  46. // Add authentication parameters and signature
  47. $authenticatedParams = $this->authenticator->addAuthParams($params);
  48. // Make HTTP request
  49. $responseData = $this->httpClient->post($endpoint, $authenticatedParams);
  50. // Handle and validate response
  51. $response = $this->responseHandler->handle($responseData);
  52. // Throw exception if response indicates error
  53. $this->responseHandler->validateOrThrow($responseData);
  54. return $response;
  55. }
  56. /**
  57. * Get Basic service (shops, warehouses, logistics, etc.)
  58. */
  59. public function basic(): BasicService
  60. {
  61. return $this->basicService ??= new BasicService($this);
  62. }
  63. /**
  64. * Get Goods service (products, specifications, etc.)
  65. */
  66. public function goods(): GoodsService
  67. {
  68. return $this->goodsService ??= new GoodsService($this);
  69. }
  70. /**
  71. * Get Purchase service (purchase orders, returns, etc.)
  72. */
  73. public function purchase(): PurchaseService
  74. {
  75. return $this->purchaseService ??= new PurchaseService($this);
  76. }
  77. /**
  78. * Get Refund service (refund processing, etc.)
  79. */
  80. public function refund(): RefundService
  81. {
  82. return $this->refundService ??= new RefundService($this);
  83. }
  84. /**
  85. * Get Stock service (inventory management, transfers, etc.)
  86. */
  87. public function stock(): StockService
  88. {
  89. return $this->stockService ??= new StockService($this);
  90. }
  91. /**
  92. * Get Trade service (orders, logistics sync, etc.)
  93. */
  94. public function trade(): TradeService
  95. {
  96. return $this->tradeService ??= new TradeService($this);
  97. }
  98. /**
  99. * Get the configuration
  100. */
  101. public function getConfig(): Config
  102. {
  103. return $this->config;
  104. }
  105. /**
  106. * Get the HTTP client
  107. */
  108. public function getHttpClient(): HttpClient
  109. {
  110. return $this->httpClient;
  111. }
  112. /**
  113. * Get the authenticator
  114. */
  115. public function getAuthenticator(): Authenticator
  116. {
  117. return $this->authenticator;
  118. }
  119. /**
  120. * Get the response handler
  121. */
  122. public function getResponseHandler(): ResponseHandler
  123. {
  124. return $this->responseHandler;
  125. }
  126. }