Skip to content

Commit 573465e

Browse files
committed
Update Walmart class to support multiple hosts
1 parent 0998d3b commit 573465e

2 files changed

Lines changed: 108 additions & 72 deletions

File tree

resources/templates/Walmart.hbs

Lines changed: 54 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ abstract class Walmart
2525
{
2626
public const VERSION = '{{artifactVersion}}';
2727

28+
/**
29+
* The base URL for all API requests. This can be overridden in child classes.
30+
*/
31+
public const HOST = 'https://marketplace.walmartapis.com';
32+
2833
/**
2934
* The configuration for this client.
3035
* @var Configuration
@@ -41,11 +46,57 @@ abstract class Walmart
4146
* Create a new Walmart client.
4247
*
4348
* @param Configuration $config The configuration for this client.
44-
* @param ?string $country The country to use for this client.
49+
* @param bool $clone If false, the configuration object will be used as-is. If true, the
50+
* configuration object will be cloned before use. Default is true.
51+
*/
52+
public function __construct(Configuration $config, protected bool $clone = true)
53+
{
54+
// We clone $config so that if the same Configuration object is used for multiple API
55+
// instances, the host changes made in the ::contentProvider(), ::marketplace(), and
56+
// ::supplier() methods don't affect the other API instances.
57+
if ($this->clone) {
58+
$this->config = clone $config;
59+
} else {
60+
$this->config = $config;
61+
}
62+
63+
$config->setHost(static::HOST);
64+
}
65+
66+
/**
67+
* Return an instance of the Content Provider API provider.
68+
*
69+
* @param Configuration $config
70+
* @param bool $clone
71+
* @return ContentProviderApi
72+
*/
73+
public static function contentProvider(Configuration $config, bool $clone = true): ContentProviderApi
74+
{
75+
return new ContentProviderApi($config, $clone);
76+
}
77+
78+
/**
79+
* Return an instance of the Marketplace API provider.
80+
*
81+
* @param Configuration $config
82+
* @param bool $clone
83+
* @return MarketplaceApi
4584
*/
46-
public function __construct(Configuration $config)
85+
public static function marketplace(Configuration $config, bool $clone = true): MarketplaceApi
4786
{
48-
$this->config = $config;
87+
return new MarketplaceApi($config, $clone);
88+
}
89+
90+
/**
91+
* Return an instance of the 1P Supplier API provider.
92+
*
93+
* @param Configuration $config
94+
* @param bool $clone
95+
* @return SupplierApi
96+
*/
97+
public static function supplier(Configuration $config, bool $clone = true): SupplierApi
98+
{
99+
return new SupplierApi($config, $clone);
49100
}
50101

51102
/**
@@ -79,37 +130,4 @@ abstract class Walmart
79130
$apiClass = static::$countryApiMap[$name][$this->config->getCountry()];
80131
return new $apiClass($this->config);
81132
}
82-
83-
/**
84-
* Return an instance of the Content Provider API provider.
85-
*
86-
* @param Configuration $config
87-
* @return ContentProviderApi
88-
*/
89-
public static function contentProvider(Configuration $config): ContentProviderApi
90-
{
91-
return new ContentProviderApi($config);
92-
}
93-
94-
/**
95-
* Return an instance of the 1P Supplier API provider.
96-
*
97-
* @param Configuration $config
98-
* @return SupplierApi
99-
*/
100-
public static function supplier(Configuration $config): SupplierApi
101-
{
102-
return new SupplierApi($config);
103-
}
104-
105-
/**
106-
* Return an instance of the Marketplace API provider.
107-
*
108-
* @param Configuration $config
109-
* @return MarketplaceApi
110-
*/
111-
public static function marketplace(Configuration $config): MarketplaceApi
112-
{
113-
return new MarketplaceApi($config);
114-
}
115133
}

src/Walmart.php

Lines changed: 54 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ abstract class Walmart
3030
{
3131
public const VERSION = '0.6.3';
3232

33+
/**
34+
* The base URL for all API requests. This can be overridden in child classes.
35+
*/
36+
public const HOST = 'https://marketplace.walmartapis.com';
37+
3338
/**
3439
* The configuration for this client.
3540
* @var Configuration
@@ -46,11 +51,57 @@ abstract class Walmart
4651
* Create a new Walmart client.
4752
*
4853
* @param Configuration $config The configuration for this client.
49-
* @param ?string $country The country to use for this client.
54+
* @param bool $clone If false, the configuration object will be used as-is. If true, the
55+
* configuration object will be cloned before use. Default is true.
56+
*/
57+
public function __construct(Configuration $config, protected bool $clone = true)
58+
{
59+
// We clone $config so that if the same Configuration object is used for multiple API
60+
// instances, the host changes made in the ::contentProvider(), ::marketplace(), and
61+
// ::supplier() methods don't affect the other API instances.
62+
if ($this->clone) {
63+
$this->config = clone $config;
64+
} else {
65+
$this->config = $config;
66+
}
67+
68+
$config->setHost(static::HOST);
69+
}
70+
71+
/**
72+
* Return an instance of the Content Provider API provider.
73+
*
74+
* @param Configuration $config
75+
* @param bool $clone
76+
* @return ContentProviderApi
77+
*/
78+
public static function contentProvider(Configuration $config, bool $clone = true): ContentProviderApi
79+
{
80+
return new ContentProviderApi($config, $clone);
81+
}
82+
83+
/**
84+
* Return an instance of the Marketplace API provider.
85+
*
86+
* @param Configuration $config
87+
* @param bool $clone
88+
* @return MarketplaceApi
5089
*/
51-
public function __construct(Configuration $config)
90+
public static function marketplace(Configuration $config, bool $clone = true): MarketplaceApi
5291
{
53-
$this->config = $config;
92+
return new MarketplaceApi($config, $clone);
93+
}
94+
95+
/**
96+
* Return an instance of the 1P Supplier API provider.
97+
*
98+
* @param Configuration $config
99+
* @param bool $clone
100+
* @return SupplierApi
101+
*/
102+
public static function supplier(Configuration $config, bool $clone = true): SupplierApi
103+
{
104+
return new SupplierApi($config, $clone);
54105
}
55106

56107
/**
@@ -84,37 +135,4 @@ public function __call($name, $arguments)
84135
$apiClass = static::$countryApiMap[$name][$this->config->getCountry()];
85136
return new $apiClass($this->config);
86137
}
87-
88-
/**
89-
* Return an instance of the Content Provider API provider.
90-
*
91-
* @param Configuration $config
92-
* @return ContentProviderApi
93-
*/
94-
public static function contentProvider(Configuration $config): ContentProviderApi
95-
{
96-
return new ContentProviderApi($config);
97-
}
98-
99-
/**
100-
* Return an instance of the 1P Supplier API provider.
101-
*
102-
* @param Configuration $config
103-
* @return SupplierApi
104-
*/
105-
public static function supplier(Configuration $config): SupplierApi
106-
{
107-
return new SupplierApi($config);
108-
}
109-
110-
/**
111-
* Return an instance of the Marketplace API provider.
112-
*
113-
* @param Configuration $config
114-
* @return MarketplaceApi
115-
*/
116-
public static function marketplace(Configuration $config): MarketplaceApi
117-
{
118-
return new MarketplaceApi($config);
119-
}
120138
}

0 commit comments

Comments
 (0)