Skip to content

Commit 4268f9d

Browse files
author
mrt1m
committed
add data from catalog
1 parent 418b04f commit 4268f9d

File tree

15 files changed

+360
-66
lines changed

15 files changed

+360
-66
lines changed

README.md

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@ Create Client object using the following code:
1818

1919
```php
2020
use PlaystationStoreApi\Client;
21-
use PlaystationStoreApi\Enum\Regions;
21+
use PlaystationStoreApi\Enum\Region;
22+
use \GuzzleHttp\Client as HttpClient;
2223

23-
$clientApi = new Client(new Regions(Regions::RUSSIA));
24+
$clientApi = new Client(new Region(Region::RUSSIA), new HttpClient());
2425
```
2526

2627
## 4. API Requests
@@ -29,4 +30,28 @@ $clientApi = new Client(new Regions(Regions::RUSSIA));
2930

3031
```php
3132
$response = $clientApi->product()->get('EP0001-CUSA12042_00-GAME000000000000');
33+
```
34+
35+
### 4.2. Request catalog data
36+
37+
```php
38+
use PlaystationStoreApi\Query\CatalogProducts;
39+
use \PlaystationStoreApi\Enum\Category;
40+
use PlaystationStoreApi\ValueObject\Pagination;
41+
42+
$sha256Hash = 'get your code from request in browser';
43+
$query = new CatalogProducts(new Category(Category::PS4_GAMES), $sha256Hash);
44+
$query->setPagination(new Pagination(10, 0));
45+
46+
$response = $clientApi->catalog()->products($query);
47+
```
48+
49+
#### 4.2.1 Get sha256Hash
50+
51+
1) Open the Network panel and find query to https://web.np.playstation.com/api/graphql/v1/op
52+
2) Copy the full request URL and use urldecode
53+
3) sha256Hash is in the extensions parameter, example:
54+
55+
```
56+
https://web.np.playstation.com/api/graphql/v1//op?operationName=categoryGridRetrieve&variables={"id":"44d8bb20-653e-431e-8ad0-c0a365f68d2f","pageArgs":{"size":24,"offset":0},"sortBy":{"name":"productReleaseDate","isAscending":false},"filterBy":[],"facetOptions":[]}&extensions={"persistedQuery":{"version":1,"sha256Hash":"9845afc0dbaab4965f6563fffc703f588c8e76792000e8610843b8d3ee9c4c09"}}
3257
```

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
}
1111
],
1212
"require": {
13-
"php": ">7.4",
13+
"php": ">=7.4",
1414
"myclabs/php-enum": "1.8.0",
1515
"guzzlehttp/guzzle": "7.3.0"
1616
},

src/Actions/Catalog.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace PlaystationStoreApi\Actions;
5+
6+
use GuzzleHttp\Psr7\Request;
7+
use PlaystationStoreApi\ApiClients\GraphQL;
8+
use PlaystationStoreApi\Query\CatalogProducts;
9+
10+
class Catalog
11+
{
12+
protected GraphQL $graphQLApiClient;
13+
14+
public function __construct(GraphQL $graphQLApiClient)
15+
{
16+
$this->graphQLApiClient = $graphQLApiClient;
17+
}
18+
19+
/**
20+
* @throws \Psr\Http\Client\ClientExceptionInterface
21+
* @throws \JsonException
22+
*/
23+
public function products(CatalogProducts $query) : string
24+
{
25+
$params = [
26+
'operationName' => 'categoryGridRetrieve',
27+
'variables' => [
28+
'id' => $query->categoryId(),
29+
'pageArgs' => [
30+
'size' => $query->pagination()->size(),
31+
'offset' => $query->pagination()->offset(),
32+
],
33+
'sortBy' => [
34+
'name' => $query->sorting()->fieldName(),
35+
'isAscending' => $query->sorting()->isAscending(),
36+
],
37+
'filterBy' => [],
38+
'facetOptions' => [],
39+
],
40+
'extensions' => [
41+
'persistedQuery' => [
42+
'version' => 1,
43+
'sha256Hash' => $query->sha256Hash(),
44+
],
45+
],
46+
];
47+
48+
$params['variables'] = json_encode($params['variables'], JSON_THROW_ON_ERROR);
49+
$params['extensions'] = json_encode($params['extensions'], JSON_THROW_ON_ERROR);
50+
51+
return $this->graphQLApiClient->get(new Request('GET', '/op?' . http_build_query($params)));
52+
}
53+
}

src/Actions/Product.php

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,24 @@
33

44
namespace PlaystationStoreApi\Actions;
55

6-
use PlaystationStoreApi\Request;
6+
use GuzzleHttp\Psr7\Request;
7+
use PlaystationStoreApi\ApiClients\Chihiro;
8+
use Psr\Http\Client\ClientExceptionInterface;
79

810
class Product
911
{
10-
protected Request $request;
12+
protected Chihiro $chihiroApiCLient;
1113

12-
public function __construct(Request $request)
14+
public function __construct(Chihiro $chihiroApiCLient)
1315
{
14-
$this->request = $request;
16+
$this->chihiroApiCLient = $chihiroApiCLient;
1517
}
1618

1719
/**
18-
* @param string $productId
19-
*
20-
* @return string
21-
* @throws \GuzzleHttp\Exception\GuzzleException
20+
* @throws ClientExceptionInterface
2221
*/
2322
public function get(string $productId) : string
2423
{
25-
return $this->request->get($productId);
24+
return $this->chihiroApiCLient->get(new Request('GET', '/' . $productId));
2625
}
2726
}

src/ApiClients/BaseApiClient.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace PlaystationStoreApi\ApiClients;
5+
6+
use GuzzleHttp\Psr7\Uri;
7+
use PlaystationStoreApi\Enum\Region;
8+
use Psr\Http\Client\ClientInterface;
9+
use Psr\Http\Message\RequestInterface;
10+
use Psr\Http\Message\UriInterface;
11+
12+
abstract class BaseApiClient
13+
{
14+
protected ClientInterface $client;
15+
protected string $region;
16+
protected string $language;
17+
protected UriInterface $uri;
18+
protected array $headers = [];
19+
protected Uri $basePath;
20+
21+
public function __construct(Region $region, ClientInterface $client)
22+
{
23+
$this->parseRegion($region);
24+
25+
$this->client = $client;
26+
$this->uri = new Uri();
27+
$this->basePath = new Uri();
28+
}
29+
30+
protected function parseRegion(Region $region) : void
31+
{
32+
if ($result = explode('-', $region->getValue())) {
33+
$this->region = array_pop($result);
34+
$this->language = implode('-', $result);
35+
} else {
36+
throw new \UnexpectedValueException('The value [' . $region->getValue() . '] is not valid');
37+
}
38+
}
39+
40+
protected function mergeBasePath(UriInterface $uri) : UriInterface
41+
{
42+
return $uri
43+
->withScheme($this->basePath->getScheme())
44+
->withHost($this->basePath->getHost())
45+
->withPath($this->basePath->getPath() . $uri->getPath());
46+
}
47+
48+
abstract public function get(RequestInterface $request) : string;
49+
}

src/ApiClients/Chihiro.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace PlaystationStoreApi\ApiClients;
5+
6+
use GuzzleHttp\Psr7\Uri;
7+
use PlaystationStoreApi\Enum\Region;
8+
use Psr\Http\Client\ClientExceptionInterface;
9+
use Psr\Http\Client\ClientInterface;
10+
use Psr\Http\Message\RequestInterface;
11+
12+
class Chihiro extends BaseApiClient
13+
{
14+
public function __construct(Region $region, ClientInterface $client)
15+
{
16+
parent::__construct($region, $client);
17+
18+
$this->basePath = new Uri('https://store.playstation.com/store/api/chihiro/00_09_000/container/' . strtoupper($this->region) . '/' . $this->language . '/999/');
19+
}
20+
21+
/**
22+
* @throws ClientExceptionInterface
23+
*/
24+
public function get(RequestInterface $request) : string
25+
{
26+
$response = $this->client->sendRequest(
27+
$request->withUri($this->mergeBasePath($request->getUri()))
28+
);
29+
30+
return $response->getBody()->getContents();
31+
}
32+
}

src/ApiClients/GraphQL.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace PlaystationStoreApi\ApiClients;
5+
6+
use GuzzleHttp\Psr7\Uri;
7+
use PlaystationStoreApi\Enum\Region;
8+
use Psr\Http\Client\ClientExceptionInterface;
9+
use Psr\Http\Client\ClientInterface;
10+
use Psr\Http\Message\RequestInterface;
11+
12+
class GraphQL extends BaseApiClient
13+
{
14+
15+
public function __construct(Region $region, ClientInterface $client)
16+
{
17+
parent::__construct($region, $client);
18+
19+
$this->basePath = new Uri('https://web.np.playstation.com/api/graphql/v1/');
20+
}
21+
22+
/**
23+
* @throws ClientExceptionInterface
24+
*/
25+
public function get(RequestInterface $request) : string
26+
{
27+
$response = $this->client->sendRequest(
28+
$request
29+
->withUri($this->mergeBasePath($request->getUri()))
30+
->withAddedHeader('x-psn-store-locale-override', $this->region . '-' . strtoupper($this->language))
31+
);
32+
33+
return $response->getBody()->getContents();
34+
}
35+
}

src/Client.php

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,32 @@
33

44
namespace PlaystationStoreApi;
55

6+
use PlaystationStoreApi\Actions\Catalog;
67
use PlaystationStoreApi\Actions\Product;
7-
use PlaystationStoreApi\Enum\Regions;
8+
use PlaystationStoreApi\ApiClients\Chihiro;
9+
use PlaystationStoreApi\ApiClients\GraphQL;
10+
use PlaystationStoreApi\Enum\Region;
11+
use Psr\Http\Client\ClientInterface;
812

913
class Client
1014
{
11-
protected Regions $region;
12-
protected Request $request;
15+
protected Chihiro $chihiroApiCLient;
16+
protected GraphQL $graphQLApiClient;
1317

14-
public function __construct(Regions $region) {
18+
public function __construct(Region $region, ClientInterface $client)
19+
{
1520

16-
$this->request = new Request($region);
21+
$this->chihiroApiCLient = new Chihiro($region, $client);
22+
$this->graphQLApiClient = new GraphQL($region, $client);
1723
}
1824

1925
public function product() : Product
2026
{
21-
return new Product($this->request);
27+
return new Product($this->chihiroApiCLient);
28+
}
29+
30+
public function catalog() : Catalog
31+
{
32+
return new Catalog($this->graphQLApiClient);
2233
}
2334
}

src/Enum/Category.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace PlaystationStoreApi\Enum;
5+
6+
use MyCLabs\Enum\Enum;
7+
8+
class Category extends Enum
9+
{
10+
public const PS4_GAMES = '44d8bb20-653e-431e-8ad0-c0a365f68d2f';
11+
public const PS5_GAMES = '4cbf39e2-5749-4970-ba81-93a489e4570c';
12+
public const PS_PLUS = '038b4df3-bb4c-48f8-8290-3feb35f0f0fd';
13+
public const SALES = '803cee19-e5a1-4d59-a463-0b6b2701bf7c';
14+
public const EA_GAMES = '74d4e266-5c64-4c61-a7e3-1b6e78f643e6';
15+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
use MyCLabs\Enum\Enum;
77

8-
class Regions extends Enum
8+
class Region extends Enum
99
{
1010
public const ARGENTINA = "es-ar";
1111
public const AUSTRALIA = "en-au";

0 commit comments

Comments
 (0)