Skip to content

add the ability to resolve users and parents #116

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 29 commits into from
Jun 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
c3d4b48
add the ability to resolve users and parents
johguentner Feb 6, 2023
31bd869
Apply fixes from StyleCI (#115)
mechelon Feb 6, 2023
27f4fe9
Merge branch 'dev' into feature/resolve
johguentner Feb 6, 2023
9a3dbb3
fix: modify changed method within `NotionParent`
johguentner Feb 6, 2023
3c9c845
add prototypical relation resolving
johguentner Feb 16, 2023
29c991a
Apply fixes from StyleCI (#124)
mechelon Feb 16, 2023
68cb62f
Merge branch 'dev' into feature/resolve
johguentner Apr 30, 2023
1e4f0ff
Merge branch 'dev' into feature/resolve
johguentner May 2, 2023
246396f
polish phpdocs of comment endpoint
johguentner May 2, 2023
d27f4ff
Apply fixes from StyleCI (#145)
mechelon May 2, 2023
9a3730b
polish: add newline to improve readability
johguentner May 2, 2023
69e15bd
Merge branch 'feature/resolve' of https://github.com/5am-code/laravel…
johguentner May 2, 2023
55fe958
Apply fixes from StyleCI (#146)
mechelon May 2, 2023
2b11b70
add `parentOf` to access parents easily
johguentner May 2, 2023
a82ebeb
Apply fixes from StyleCI (#147)
mechelon May 2, 2023
eb7b39b
polish `PestHttpRecorder::class`
johguentner Apr 30, 2023
c83c9b1
Merge branch 'feature/resolve' of https://github.com/5am-code/laravel…
johguentner May 3, 2023
cd8116d
Apply fixes from StyleCI (#148)
mechelon May 3, 2023
e73207a
add tests for `Notion::resolve()`; polish record
johguentner Jun 9, 2023
13a560d
build snapshots for resolve and
johguentner Jun 9, 2023
1c27736
Merge branch 'feature/resolve' of https://github.com/5am-code/laravel…
johguentner Jun 9, 2023
44626bb
Apply fixes from StyleCI (#152)
mechelon Jun 9, 2023
b01fc99
add simple phpdbg code coverage (cmd)
johguentner Jun 10, 2023
0cbd9de
add additional tests for resolve endpoint
johguentner Jun 10, 2023
1f6aa11
Apply fixes from StyleCI (#153)
mechelon Jun 10, 2023
f3aa549
fix type for workspace in `NotionParent::class`
johguentner Jun 10, 2023
7b7395b
add resolve/parent tests regarding NotionParent
johguentner Jun 10, 2023
016009e
add snapshot for parent testing
johguentner Jun 10, 2023
f945728
Apply fixes from StyleCI (#155)
mechelon Jun 10, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ vendor
.phpunit.result.cache
coverage/
.phpunit.cache/
.env*
.env*
coverage-report
7 changes: 5 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,13 @@
},
"scripts": {
"test": "vendor/bin/pest",
"test-coverage": "vendor/bin/pest --coverage-html coverage"
"test-coverage": "phpdbg -qrr ./vendor/bin/pest --coverage-html ./coverage-report"
},
"config": {
"sort-packages": true
"sort-packages": true,
"allow-plugins": {
"pestphp/pest-plugin": true
}
},
"extra": {
"laravel": {
Expand Down
18 changes: 10 additions & 8 deletions src/Endpoints/Comments.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,15 @@ public function __construct(Notion $notion)
}

/**
* Retrieve a list of comments
* url: https://api.notion.com/{version}/comments?block_id=* [get]
* notion-api-docs: https://developers.notion.com/reference/retrieve-a-comment.
* Retrieve a list of comments.
*
* @url https://api.notion.com/{version}/comments?block_id=* [get]
*
* @reference https://developers.notion.com/reference/retrieve-a-comment.
*
* @param string $blockId
* @return CommentCollection
*
* @throws HandlingException
* @throws NotionException
*/
public function ofBlock(string $blockId): CommentCollection
Expand Down Expand Up @@ -88,14 +89,15 @@ public function onPage(string $pageId): self
}

/**
* Create a comment
* url: https://api.notion.com/{version}/comments [post]
* notion-api-docs: https://developers.notion.com/reference/create-a-comment.
* Create a comment.
*
* @url https://api.notion.com/{version}/comments [post]
*
* @reference https://developers.notion.com/reference/create-a-comment.
*
* @param CommentEntity $comment
* @return CommentEntity
*
* @throws HandlingException
* @throws NotionException
*/
public function create($comment): CommentEntity
Expand Down
120 changes: 120 additions & 0 deletions src/Endpoints/Resolve.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
<?php

namespace FiveamCode\LaravelNotionApi\Endpoints;

use FiveamCode\LaravelNotionApi\Entities\Blocks\Block;
use FiveamCode\LaravelNotionApi\Entities\Database;
use FiveamCode\LaravelNotionApi\Entities\Entity;
use FiveamCode\LaravelNotionApi\Entities\NotionParent;
use FiveamCode\LaravelNotionApi\Entities\Page;
use FiveamCode\LaravelNotionApi\Entities\Properties\Relation;
use FiveamCode\LaravelNotionApi\Entities\User;
use FiveamCode\LaravelNotionApi\Exceptions\HandlingException;
use FiveamCode\LaravelNotionApi\Exceptions\NotionException;
use FiveamCode\LaravelNotionApi\Notion;
use FiveamCode\LaravelNotionApi\Traits\HasParent;
use Illuminate\Support\Collection;

/**
* Class Resolve.
*/
class Resolve extends Endpoint
{
/**
* Block constructor.
*
* @param Notion $notion
*
* @throws HandlingException
* @throws \FiveamCode\LaravelNotionApi\Exceptions\LaravelNotionAPIException
*/
public function __construct(Notion $notion)
{
parent::__construct($notion);
}

/**
* Resolve User.
*
* @param User $user
* @return User
*
* @throws HandlingException
* @throws NotionException
*/
public function user(User $user): User
{
return $this->notion->users()->find($user->getId());
}

/**
* Resolve Parent of an entity.
*
* @param Entity $entity
* @return Page|Database|Block
*
* @throws HandlingException
* @throws NotionException
*/
public function parentOf(Entity $entity)
{
if (! in_array(HasParent::class, class_uses_recursive(get_class($entity)))) {
throw new HandlingException("The given entity '{$entity->getObjectType()}' does not have a parent.");
}

return $this->parent($entity->getParent());
}

/**
* Resolve Parent.
*
* @param NotionParent $parent
* @return Page|Database|Block
*
* @throws HandlingException
* @throws NotionException
*/
public function parent(NotionParent $parent): Page|Database|Block|NotionParent
{
switch ($parent->getObjectType()) {
case 'page_id':
return $this->notion->pages()->find($parent->getId());
case 'database_id':
return $this->notion->databases()->find($parent->getId());
case 'block_id':
return $this->notion->block($parent->getId())->retrieve();
case 'workspace':
return $parent;
// throw new HandlingException('A Notion Workspace cannot be resolved by the Notion API.');
default:
throw new HandlingException('Unknown parent type while resolving the notion parent');
}
}

/**
* Resolve Relations.
*
* @param Relation $relation
* @return Collection<Page>
*
* @throws HandlingException
* @throws NotionException
*/
public function relations(Relation $relation, bool $onlyTitles = false): Collection
{
$pages = collect();
$relationIds = $relation->getRelation()->map(function ($o) {
return $o['id'];
});

foreach ($relationIds as $relationId) {
if ($onlyTitles) {
$pages->add($this->notion->pages()->find($relationId)->getTitle());
} else {
$pages->add($this->notion->pages()->find($relationId));
}
}

return $pages;
}
}
70 changes: 70 additions & 0 deletions src/Entities/NotionParent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

namespace FiveamCode\LaravelNotionApi\Entities;

use FiveamCode\LaravelNotionApi\Exceptions\HandlingException;

/**
* Class NotionParent.
*/
class NotionParent extends Entity
{
/**
* @param array $responseData
*
* @throws HandlingException
* @throws \FiveamCode\LaravelNotionApi\Exceptions\NotionException
*/
protected function setResponseData(array $responseData): void
{
parent::setResponseData($responseData);

if (
$responseData['object'] !== 'page_id'
&& $responseData['object'] !== 'database_id'
&& $responseData['object'] !== 'workspace'
&& $responseData['object'] !== 'block_id'
) {
throw HandlingException::instance('invalid json-array: the given object is not a valid parent');
}

$this->fillFromRaw();
}

private function fillFromRaw(): void
{
parent::fillEssentials();
}

/**
* @return bool
*/
public function isBlock(): bool
{
return $this->getObjectType() === 'block_id';
}

/**
* @return bool
*/
public function isPage(): bool
{
return $this->getObjectType() === 'page_id';
}

/**
* @return bool
*/
public function isDatabase(): bool
{
return $this->getObjectType() === 'database_id';
}

/**
* @return bool
*/
public function isWorkspace(): bool
{
return $this->getObjectType() === 'workspace';
}
}
21 changes: 19 additions & 2 deletions src/Macros/PestHttpRecorder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use GuzzleHttp\Client;
use Illuminate\Http\Client\Request;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Str;
Expand Down Expand Up @@ -37,6 +38,8 @@ class HttpRecorder

private $usePrettyJson = true;

private $requestNames = [];

public function storeIn($directory)
{
$this->snapshotDirectory = $directory;
Expand All @@ -51,21 +54,32 @@ public function minifyJson()
return $this;
}

public function nameForNextRequest($name)
{
array_push($this->requestNames, $name);
}

public function handle(Request $request)
{
$forceRecording = in_array('--force-recording', $_SERVER['argv']);

$urlInfo = parse_url($request->url());
$payload = null;

// create specific filename for storing snapshots
$header = $request->headers();
$method = Str::lower($request->method());
$name = Str::slug(Str::replace('/', '-', $urlInfo['path']));
$query = Str::slug(Str::replace('&', '_', Str::replace('=', '-', $urlInfo['query'])));
$payload = ($method === 'get') ? ($urlInfo['query'] ?? null) : $request->body();
$queryName = array_pop($this->requestNames) ?? hash('adler32', $payload);

$fileName = "{$method}_{$name}_{$query}.json";
$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)) {
File::makeDirectory($directoryPath, 0744, true, true);

Expand All @@ -77,7 +91,10 @@ public function handle(Request $request)
]);

$recordedResponse = [
'header' => $header,
'method' => $method,
'status' => $response->getStatusCode(),
'payload' => ($method === 'get') ? $payload : json_decode($payload, true),
'data' => json_decode($response->getBody()->getContents(), true),
];

Expand Down
6 changes: 6 additions & 0 deletions src/Notion.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use FiveamCode\LaravelNotionApi\Endpoints\Databases;
use FiveamCode\LaravelNotionApi\Endpoints\Endpoint;
use FiveamCode\LaravelNotionApi\Endpoints\Pages;
use FiveamCode\LaravelNotionApi\Endpoints\Resolve;
use FiveamCode\LaravelNotionApi\Endpoints\Search;
use FiveamCode\LaravelNotionApi\Endpoints\Users;
use FiveamCode\LaravelNotionApi\Exceptions\HandlingException;
Expand Down Expand Up @@ -196,6 +197,11 @@ public function comments(): Comments
return new Comments($this);
}

public function resolve(): Resolve
{
return new Resolve($this);
}

/**
* @return string
*/
Expand Down
12 changes: 12 additions & 0 deletions src/Traits/HasParent.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace FiveamCode\LaravelNotionApi\Traits;

use FiveamCode\LaravelNotionApi\Entities\NotionParent;
use Illuminate\Support\Arr;

/**
Expand Down Expand Up @@ -54,4 +55,15 @@ public function getParentType(): string
{
return $this->parentType;
}

/**
* @return NotionParent
*/
public function getParent()
{
return new NotionParent([
'id' => $this->getParentId(),
'object' => $this->getParentType(),
]);
}
}
Loading