Skip to content

Commit

Permalink
add data from catalog
Browse files Browse the repository at this point in the history
  • Loading branch information
mrt1m committed Jul 7, 2021
1 parent 418b04f commit 4268f9d
Show file tree
Hide file tree
Showing 15 changed files with 360 additions and 66 deletions.
29 changes: 27 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ Create Client object using the following code:

```php
use PlaystationStoreApi\Client;
use PlaystationStoreApi\Enum\Regions;
use PlaystationStoreApi\Enum\Region;
use \GuzzleHttp\Client as HttpClient;

$clientApi = new Client(new Regions(Regions::RUSSIA));
$clientApi = new Client(new Region(Region::RUSSIA), new HttpClient());
```

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

```php
$response = $clientApi->product()->get('EP0001-CUSA12042_00-GAME000000000000');
```

### 4.2. Request catalog data

```php
use PlaystationStoreApi\Query\CatalogProducts;
use \PlaystationStoreApi\Enum\Category;
use PlaystationStoreApi\ValueObject\Pagination;

$sha256Hash = 'get your code from request in browser';
$query = new CatalogProducts(new Category(Category::PS4_GAMES), $sha256Hash);
$query->setPagination(new Pagination(10, 0));

$response = $clientApi->catalog()->products($query);
```

#### 4.2.1 Get sha256Hash

1) Open the Network panel and find query to https://web.np.playstation.com/api/graphql/v1/op
2) Copy the full request URL and use urldecode
3) sha256Hash is in the extensions parameter, example:

```
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"}}
```
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
}
],
"require": {
"php": ">7.4",
"php": ">=7.4",
"myclabs/php-enum": "1.8.0",
"guzzlehttp/guzzle": "7.3.0"
},
Expand Down
53 changes: 53 additions & 0 deletions src/Actions/Catalog.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php
declare(strict_types=1);

namespace PlaystationStoreApi\Actions;

use GuzzleHttp\Psr7\Request;
use PlaystationStoreApi\ApiClients\GraphQL;
use PlaystationStoreApi\Query\CatalogProducts;

class Catalog
{
protected GraphQL $graphQLApiClient;

public function __construct(GraphQL $graphQLApiClient)
{
$this->graphQLApiClient = $graphQLApiClient;
}

/**
* @throws \Psr\Http\Client\ClientExceptionInterface
* @throws \JsonException
*/
public function products(CatalogProducts $query) : string
{
$params = [
'operationName' => 'categoryGridRetrieve',
'variables' => [
'id' => $query->categoryId(),
'pageArgs' => [
'size' => $query->pagination()->size(),
'offset' => $query->pagination()->offset(),
],
'sortBy' => [
'name' => $query->sorting()->fieldName(),
'isAscending' => $query->sorting()->isAscending(),
],
'filterBy' => [],
'facetOptions' => [],
],
'extensions' => [
'persistedQuery' => [
'version' => 1,
'sha256Hash' => $query->sha256Hash(),
],
],
];

$params['variables'] = json_encode($params['variables'], JSON_THROW_ON_ERROR);
$params['extensions'] = json_encode($params['extensions'], JSON_THROW_ON_ERROR);

return $this->graphQLApiClient->get(new Request('GET', '/op?' . http_build_query($params)));
}
}
17 changes: 8 additions & 9 deletions src/Actions/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,24 @@

namespace PlaystationStoreApi\Actions;

use PlaystationStoreApi\Request;
use GuzzleHttp\Psr7\Request;
use PlaystationStoreApi\ApiClients\Chihiro;
use Psr\Http\Client\ClientExceptionInterface;

class Product
{
protected Request $request;
protected Chihiro $chihiroApiCLient;

public function __construct(Request $request)
public function __construct(Chihiro $chihiroApiCLient)
{
$this->request = $request;
$this->chihiroApiCLient = $chihiroApiCLient;
}

/**
* @param string $productId
*
* @return string
* @throws \GuzzleHttp\Exception\GuzzleException
* @throws ClientExceptionInterface
*/
public function get(string $productId) : string
{
return $this->request->get($productId);
return $this->chihiroApiCLient->get(new Request('GET', '/' . $productId));
}
}
49 changes: 49 additions & 0 deletions src/ApiClients/BaseApiClient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php
declare(strict_types=1);

namespace PlaystationStoreApi\ApiClients;

use GuzzleHttp\Psr7\Uri;
use PlaystationStoreApi\Enum\Region;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\UriInterface;

abstract class BaseApiClient
{
protected ClientInterface $client;
protected string $region;
protected string $language;
protected UriInterface $uri;
protected array $headers = [];
protected Uri $basePath;

public function __construct(Region $region, ClientInterface $client)
{
$this->parseRegion($region);

$this->client = $client;
$this->uri = new Uri();
$this->basePath = new Uri();
}

protected function parseRegion(Region $region) : void
{
if ($result = explode('-', $region->getValue())) {
$this->region = array_pop($result);
$this->language = implode('-', $result);
} else {
throw new \UnexpectedValueException('The value [' . $region->getValue() . '] is not valid');
}
}

protected function mergeBasePath(UriInterface $uri) : UriInterface
{
return $uri
->withScheme($this->basePath->getScheme())
->withHost($this->basePath->getHost())
->withPath($this->basePath->getPath() . $uri->getPath());
}

abstract public function get(RequestInterface $request) : string;
}
32 changes: 32 additions & 0 deletions src/ApiClients/Chihiro.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
declare(strict_types=1);

namespace PlaystationStoreApi\ApiClients;

use GuzzleHttp\Psr7\Uri;
use PlaystationStoreApi\Enum\Region;
use Psr\Http\Client\ClientExceptionInterface;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestInterface;

class Chihiro extends BaseApiClient
{
public function __construct(Region $region, ClientInterface $client)
{
parent::__construct($region, $client);

$this->basePath = new Uri('https://store.playstation.com/store/api/chihiro/00_09_000/container/' . strtoupper($this->region) . '/' . $this->language . '/999/');
}

/**
* @throws ClientExceptionInterface
*/
public function get(RequestInterface $request) : string
{
$response = $this->client->sendRequest(
$request->withUri($this->mergeBasePath($request->getUri()))
);

return $response->getBody()->getContents();
}
}
35 changes: 35 additions & 0 deletions src/ApiClients/GraphQL.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php
declare(strict_types=1);

namespace PlaystationStoreApi\ApiClients;

use GuzzleHttp\Psr7\Uri;
use PlaystationStoreApi\Enum\Region;
use Psr\Http\Client\ClientExceptionInterface;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestInterface;

class GraphQL extends BaseApiClient
{

public function __construct(Region $region, ClientInterface $client)
{
parent::__construct($region, $client);

$this->basePath = new Uri('https://web.np.playstation.com/api/graphql/v1/');
}

/**
* @throws ClientExceptionInterface
*/
public function get(RequestInterface $request) : string
{
$response = $this->client->sendRequest(
$request
->withUri($this->mergeBasePath($request->getUri()))
->withAddedHeader('x-psn-store-locale-override', $this->region . '-' . strtoupper($this->language))
);

return $response->getBody()->getContents();
}
}
23 changes: 17 additions & 6 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,32 @@

namespace PlaystationStoreApi;

use PlaystationStoreApi\Actions\Catalog;
use PlaystationStoreApi\Actions\Product;
use PlaystationStoreApi\Enum\Regions;
use PlaystationStoreApi\ApiClients\Chihiro;
use PlaystationStoreApi\ApiClients\GraphQL;
use PlaystationStoreApi\Enum\Region;
use Psr\Http\Client\ClientInterface;

class Client
{
protected Regions $region;
protected Request $request;
protected Chihiro $chihiroApiCLient;
protected GraphQL $graphQLApiClient;

public function __construct(Regions $region) {
public function __construct(Region $region, ClientInterface $client)
{

$this->request = new Request($region);
$this->chihiroApiCLient = new Chihiro($region, $client);
$this->graphQLApiClient = new GraphQL($region, $client);
}

public function product() : Product
{
return new Product($this->request);
return new Product($this->chihiroApiCLient);
}

public function catalog() : Catalog
{
return new Catalog($this->graphQLApiClient);
}
}
15 changes: 15 additions & 0 deletions src/Enum/Category.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
declare(strict_types=1);

namespace PlaystationStoreApi\Enum;

use MyCLabs\Enum\Enum;

class Category extends Enum
{
public const PS4_GAMES = '44d8bb20-653e-431e-8ad0-c0a365f68d2f';
public const PS5_GAMES = '4cbf39e2-5749-4970-ba81-93a489e4570c';
public const PS_PLUS = '038b4df3-bb4c-48f8-8290-3feb35f0f0fd';
public const SALES = '803cee19-e5a1-4d59-a463-0b6b2701bf7c';
public const EA_GAMES = '74d4e266-5c64-4c61-a7e3-1b6e78f643e6';
}
2 changes: 1 addition & 1 deletion src/Enum/Regions.php → src/Enum/Region.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

use MyCLabs\Enum\Enum;

class Regions extends Enum
class Region extends Enum
{
public const ARGENTINA = "es-ar";
public const AUSTRALIA = "en-au";
Expand Down
12 changes: 12 additions & 0 deletions src/Enum/SortingDirection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php
declare(strict_types=1);

namespace PlaystationStoreApi\Enum;

use MyCLabs\Enum\Enum;

class SortingDirection extends Enum
{
public const ASC = true;
public const DESC = false;
}
Loading

0 comments on commit 4268f9d

Please sign in to comment.