Skip to content

Commit a9ea638

Browse files
authored
Merge pull request #3 from 5am-code/feature/db-query
Feature/db query
2 parents 64d9181 + aa1f1a6 commit a9ea638

24 files changed

+821
-155
lines changed

config/config.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@
44
* You can place your custom package configuration in here.
55
*/
66
return [
7-
7+
'notion-api-token' => env('NOTION_API_TOKEN', '')
88
];

src/Endpoints/Block.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
namespace FiveamCode\LaravelNotionApi\Endpoints;
4+
5+
use FiveamCode\LaravelNotionApi\Entities\BlockCollection;
6+
use FiveamCode\LaravelNotionApi\Exceptions\WrapperException;
7+
use FiveamCode\LaravelNotionApi\Notion;
8+
use Illuminate\Support\Collection;
9+
10+
class Block extends Endpoint
11+
{
12+
private string $blockId;
13+
14+
public function __construct(Notion $notion, string $blockId)
15+
{
16+
parent::__construct($notion);
17+
$this->blockId = $blockId;
18+
}
19+
20+
/**
21+
* Retrieve block children (as raw json-data)
22+
* url: https://api.notion.com/{version}/blocks/{block_id}/children
23+
* notion-api-docs: https://developers.notion.com/reference/get-block-children
24+
*
25+
* @return BlockCollection
26+
*/
27+
public function children(): Collection
28+
{
29+
return $this->collectChildren()->getResults();
30+
}
31+
32+
/**
33+
* Retrieve block children
34+
* url: https://api.notion.com/{version}/blocks/{block_id}/children
35+
* notion-api-docs: https://developers.notion.com/reference/get-block-children
36+
*
37+
* @return array
38+
*/
39+
public function childrenRaw(): array
40+
{
41+
return $this->collectChildren()->getRawResults();
42+
}
43+
44+
private function collectChildren(): BlockCollection
45+
{
46+
$response = $this->get(
47+
$this->url(Endpoint::BLOCKS . "/" . $this->blockId . "/children" . "?{$this->buildPaginationQuery()}")
48+
);
49+
50+
if (!$response->ok())
51+
throw WrapperException::instance("Block not found.", ["blockId" => $this->blockId]);
52+
53+
54+
$blockCollection = new BlockCollection($response->json());
55+
return $blockCollection;
56+
}
57+
58+
public function create(): array
59+
{
60+
//toDo
61+
throw new \Exception("not implemented yet");
62+
}
63+
}

src/Endpoints/Blocks.php

Lines changed: 0 additions & 35 deletions
This file was deleted.

src/Endpoints/Database.php

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
3+
namespace FiveamCode\LaravelNotionApi\Endpoints;
4+
5+
use FiveamCode\LaravelNotionApi\Entities\PageCollection;
6+
use Illuminate\Support\Collection;
7+
use FiveamCode\LaravelNotionApi\Notion;
8+
use FiveamCode\LaravelNotionApi\Query\Filter;
9+
use FiveamCode\LaravelNotionApi\Query\Sorting;
10+
use FiveamCode\LaravelNotionApi\Query\StartCursor;
11+
use FiveamCode\LaravelNotionApi\Exceptions\WrapperException;
12+
use Symfony\Component\VarDumper\Cloner\Data;
13+
14+
class Database extends Endpoint
15+
{
16+
private string $databaseId;
17+
18+
private Collection $filter;
19+
private Collection $sorts;
20+
21+
22+
public function __construct(string $databaseId, Notion $notion)
23+
{
24+
$this->databaseId = $databaseId;
25+
26+
$this->sorts = new Collection();
27+
$this->filter = new Collection();
28+
29+
parent::__construct($notion);
30+
}
31+
32+
public function query(): Collection
33+
{
34+
$postData = [];
35+
36+
if ($this->sorts->isNotEmpty())
37+
$postData["sorts"] = Sorting::sortQuery($this->sorts);
38+
39+
if ($this->filter->isNotEmpty())
40+
$postData["filter"]["or"] = Filter::filterQuery($this->filter); // TODO Compound filters!
41+
42+
if ($this->startCursor !== null)
43+
$postData["start_cursor"] = $this->startCursor;
44+
45+
if ($this->pageSize !== null)
46+
$postData["page_size"] = $this->pageSize;
47+
48+
49+
$response = $this
50+
->post(
51+
$this->url(Endpoint::DATABASES . "/{$this->databaseId}/query"),
52+
$postData
53+
)
54+
55+
->json();
56+
57+
$pageCollection = new PageCollection($response);
58+
return $pageCollection->getResults();
59+
}
60+
61+
public function filterBy(Collection $filter)
62+
{
63+
$this->filter = $filter;
64+
return $this;
65+
}
66+
67+
public function sortBy(Collection $sorts)
68+
{
69+
$this->sorts = $sorts;
70+
return $this;
71+
}
72+
}

src/Endpoints/Databases.php

Lines changed: 45 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,46 +3,74 @@
33
namespace FiveamCode\LaravelNotionApi\Endpoints;
44

55
use FiveamCode\LaravelNotionApi\Entities\Database;
6+
use FiveamCode\LaravelNotionApi\Entities\DatabaseCollection;
7+
use FiveamCode\LaravelNotionApi\Exceptions\WrapperException;
68
use FiveamCode\LaravelNotionApi\Notion;
9+
use FiveamCode\LaravelNotionApi\Query\StartCursor;
10+
use Illuminate\Support\Collection;
711

12+
13+
/**
14+
* Class Databases
15+
*
16+
* This endpoint is not recommended by Notion anymore.
17+
* Use the search() endpoint instead.
18+
*
19+
* @package FiveamCode\LaravelNotionApi\Endpoints
20+
*/
821
class Databases extends Endpoint implements EndpointInterface
922
{
10-
public function __construct(Notion $notion)
23+
24+
25+
/**
26+
* List databases
27+
* url: https://api.notion.com/{version}/databases
28+
* notion-api-docs: https://developers.notion.com/reference/get-databases
29+
*
30+
* @return DatabaseCollection
31+
*/
32+
public function all(): Collection
1133
{
12-
$this->notion = $notion;
34+
return $this->collect()->getResults();
1335
}
1436

15-
/**
16-
* List databases
37+
/**
38+
* List databases (raw json-data)
1739
* url: https://api.notion.com/{version}/databases
1840
* notion-api-docs: https://developers.notion.com/reference/get-databases
19-
*
41+
*
2042
* @return array
2143
*/
22-
public function all(): array
44+
public function allRaw(): array
45+
{
46+
return $this->collect()->getRawResults();
47+
}
48+
49+
private function collect(): DatabaseCollection
2350
{
24-
return $this->getJson($this->url(Endpoint::DATABASES));
51+
$resultData = $this->getJson($this->url(Endpoint::DATABASES) . "?{$this->buildPaginationQuery()}");
52+
$databaseCollection = new DatabaseCollection($resultData);
53+
return $databaseCollection;
2554
}
2655

2756
/**
2857
* Retrieve a database
2958
* url: https://api.notion.com/{version}/databases/{database_id}
3059
* notion-api-docs: https://developers.notion.com/reference/get-database
31-
*
60+
*
3261
* @param string $databaseId
33-
* @return array
62+
* @return Database
63+
* @throws WrapperException
3464
*/
3565
public function find(string $databaseId): Database
3666
{
37-
$jsonArray = $this->getJson(
38-
$this->url(Endpoint::DATABASES . "/" . $databaseId)
67+
$response = $this->get(
68+
$this->url(Endpoint::DATABASES . "/{$databaseId}")
3969
);
40-
return new Database($jsonArray);
41-
}
4270

43-
public function query(): array
44-
{
45-
//toDo
46-
throw new \Exception("not implemented yet");
71+
if (!$response->ok())
72+
throw WrapperException::instance("Database not found.", ["databaseId" => $databaseId]);
73+
74+
return new Database($response->json());
4775
}
4876
}

src/Endpoints/Endpoint.php

Lines changed: 52 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace FiveamCode\LaravelNotionApi\Endpoints;
44

5+
use FiveamCode\LaravelNotionApi\Query\StartCursor;
56
use Illuminate\Support\Collection;
67
use FiveamCode\LaravelNotionApi\Notion;
78
use FiveamCode\LaravelNotionApi\Exceptions\WrapperException;
@@ -18,14 +19,19 @@ class Endpoint
1819
public Notion $notion;
1920
private Collection $validVersions;
2021

21-
public function __construct()
22+
23+
protected ?StartCursor $startCursor = null;
24+
protected int $pageSize = 100;
25+
26+
public function __construct(Notion $notion)
2227
{
2328
$this->validVersions = collect(["v1"]);
29+
$this->notion = $notion;
2430
}
2531

2632
/**
2733
* Checks if given version for notion-api is valid
28-
*
34+
*
2935
* @param string $version
3036
*/
3137
public function checkValidVersion(string $version): void
@@ -35,9 +41,9 @@ public function checkValidVersion(string $version): void
3541
}
3642
}
3743

38-
44+
3945
/**
40-
*
46+
*
4147
* @param string $endpoint
4248
* @return string
4349
*/
@@ -46,9 +52,9 @@ protected function url(string $endpoint): string
4652
return Endpoint::BASE_URL . "{$this->notion->getVersion()}/{$endpoint}";
4753
}
4854

49-
55+
5056
/**
51-
*
57+
*
5258
* @param string $url
5359
* @return array
5460
*/
@@ -58,10 +64,49 @@ protected function getJson(string $url): array
5864
}
5965

6066
/**
61-
*
67+
*
6268
*/
6369
protected function get(string $url)
6470
{
6571
return $this->notion->getConnection()->get($url);
6672
}
73+
74+
/**
75+
*
76+
*/
77+
protected function post(string $url, array $body)
78+
{
79+
return $this->notion->getConnection()->post($url, $body);
80+
}
81+
82+
83+
protected function buildPaginationQuery(): string
84+
{
85+
$paginationQuery = "";
86+
87+
if ($this->pageSize !== null)
88+
$paginationQuery = "page_size={$this->pageSize}&";
89+
90+
if ($this->startCursor !== null)
91+
$paginationQuery .= "start_cursor={$this->startCursor}";
92+
93+
return $paginationQuery;
94+
}
95+
96+
public function limit(int $limit): Endpoint
97+
{
98+
$this->pageSize = min($limit, 100);
99+
100+
return $this;
101+
}
102+
103+
public function offset(StartCursor $startCursor): Endpoint
104+
{
105+
// toDo
106+
throw WrapperException::instance("Not implemented yet.");
107+
108+
$this->startCursor = $startCursor;
109+
return $this;
110+
}
111+
67112
}

0 commit comments

Comments
 (0)