Skip to content

Commit

Permalink
implementation for retreiving page property items
Browse files Browse the repository at this point in the history
- incl. some tests
  • Loading branch information
johguentner committed Jul 21, 2023
1 parent b5b978c commit 8f98375
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 6 deletions.
51 changes: 51 additions & 0 deletions src/Endpoints/Page.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace FiveamCode\LaravelNotionApi\Endpoints;

use FiveamCode\LaravelNotionApi\Entities\Properties\Property;
use FiveamCode\LaravelNotionApi\Exceptions\HandlingException;
use FiveamCode\LaravelNotionApi\Exceptions\NotionException;
use FiveamCode\LaravelNotionApi\Notion;

/**
* Class Page.
*/
class Page extends Endpoint
{

/**
* @var ?string
*/
private ?string $pageId = null;

public function __construct(Notion $notion, string $pageId)
{
parent::__construct($notion);
$this->pageId = $pageId;
}

/**
* Retrieve a page property item
*
* @url https://api.notion.com/{version}/pages/{page_id}/properties/{property_id} [get]
*
* @reference https://developers.notion.com/reference/retrieve-a-page-property.
*
* @param string $propertyId
* @return Page
*
* @throws HandlingException
* @throws NotionException
*/
public function property(string $propertyId): Property
{
$response = $this->get(
$this->url(Endpoint::PAGES . '/' . $this->pageId . '/' . 'properties' . '/' . urlencode($propertyId))
);

return Property::fromResponse(
propertyKey: null,
rawContent: $response->json()
);
}
}
2 changes: 1 addition & 1 deletion src/Entities/Properties/Property.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ public function getContent()
*
* @throws HandlingException
*/
public static function fromResponse(string $propertyKey, $rawContent): Property
public static function fromResponse(?string $propertyKey, $rawContent): Property
{
$propertyClass = self::mapTypeToClass($rawContent['type']);
$property = new $propertyClass($propertyKey);
Expand Down
10 changes: 6 additions & 4 deletions src/Macros/PestHttpRecorder.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class PestHttpRecorder
public static function register()
{
Http::macro('recordAndFakeLater', function (array|string $urls = ['*']) {
if (! is_array($urls)) {
if (!is_array($urls)) {
$urls = [$urls];
}

Expand Down Expand Up @@ -71,16 +71,18 @@ public function handle(Request $request)
$method = Str::lower($request->method());
$name = Str::slug(Str::replace('/', '-', $urlInfo['path']));
$payload = ($method === 'get') ? ($urlInfo['query'] ?? null) : $request->body();
$queryName = array_pop($this->requestNames) ?? hash('adler32', $payload);
$queryName = array_pop($this->requestNames) ?? ($payload ? hash('adler32', $payload) : '');

$fileName = "{$method}_{$name}_{$queryName}.json";
if ($queryName != '') $queryName = '_' . $queryName;

$fileName = "{$method}_{$name}{$queryName}.json";
$directoryPath = "tests/{$this->snapshotDirectory}";
$filePath = "{$directoryPath}/{$fileName}";

// filter out Notion API Token Header
$header = Arr::except($header, ['Authorization']);

if ($forceRecording || ! File::exists($filePath)) {
if ($forceRecording || !File::exists($filePath)) {
File::makeDirectory($directoryPath, 0744, true, true);

$client = new Client();
Expand Down
13 changes: 12 additions & 1 deletion src/Notion.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use FiveamCode\LaravelNotionApi\Endpoints\Database;
use FiveamCode\LaravelNotionApi\Endpoints\Databases;
use FiveamCode\LaravelNotionApi\Endpoints\Endpoint;
use FiveamCode\LaravelNotionApi\Endpoints\Page;
use FiveamCode\LaravelNotionApi\Endpoints\Pages;
use FiveamCode\LaravelNotionApi\Endpoints\Resolve;
use FiveamCode\LaravelNotionApi\Endpoints\Search;
Expand Down Expand Up @@ -152,6 +153,16 @@ public function pages(): Pages
return new Pages($this);
}

/**
* @return Page
*
* @throws HandlingException
*/
public function page(string $pageId): Page
{
return new Page($this, $pageId);
}

/**
* @param string $blockId
* @return Block
Expand Down Expand Up @@ -227,7 +238,7 @@ public function getConnection(): ?PendingRequest
*/
public function checkValidVersion(string $version): void
{
if (! $this->validVersions->contains($version)) {
if (!$this->validVersions->contains($version)) {
throw HandlingException::instance('Invalid version for Notion-API endpoint', ['invalidVersion' => $version]);
}
}
Expand Down
40 changes: 40 additions & 0 deletions tests/RecordedEndpointPageTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

use Carbon\Carbon;
use FiveamCode\LaravelNotionApi\Entities\Collections\CommentCollection;
use FiveamCode\LaravelNotionApi\Entities\Comment;
use FiveamCode\LaravelNotionApi\Entities\Properties\Property;
use FiveamCode\LaravelNotionApi\Entities\PropertyItems\RichText;
use FiveamCode\LaravelNotionApi\Exceptions\NotionException;
use Illuminate\Support\Facades\Http;

$httpRecorder = null;

beforeEach(function () {
$this->httpRecorder = Http::recordAndFakeLater([
'https://api.notion.com/v1/pages*',
'https://api.notion.com/v1/databases*',
])->storeIn('snapshots/page-property-items');
});

it('should fetch specific property items of a page', function () {

$this->httpRecorder->nameForNextRequest('database-for-properties');
$databaseStructure = \Notion::databases()->find('cdd4befe814144f7b1eecb9c123bd4fb');

$propertyKeys = $databaseStructure->getProperties()->map(fn ($o) => $o->getTitle());

foreach ($propertyKeys as $propertyKey) {
try {

if ($propertyKey == 'Rollup' || $propertyKey == 'Person' || $propertyKey == 'Name') continue;
$id = $databaseStructure->getProperty($propertyKey)->getId();
$property = \Notion::page('f1884dca3885460e93f52bf4da7cce8e')->property($id);

expect($property)->toBeInstanceOf(Property::class);
} catch (\Exception $e) {
// dd($id);
dd($propertyKey, $e->getMessage());
}
}
});

0 comments on commit 8f98375

Please sign in to comment.