Skip to content

Commit 4a9b7da

Browse files
committed
added pagination to users, databases and block children
1 parent 64d08c7 commit 4a9b7da

File tree

6 files changed

+44
-39
lines changed

6 files changed

+44
-39
lines changed

src/Endpoints/Block.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,12 @@ public function __construct(Notion $notion, string $blockId){
2121
* url: https://api.notion.com/{version}/blocks/{block_id}/children
2222
* notion-api-docs: https://developers.notion.com/reference/get-block-children
2323
*
24-
* @param string $blockId
2524
* @return BlockCollection
2625
*/
2726
public function children(): BlockCollection
2827
{
2928
$response = $this->get(
30-
$this->url(Endpoint::BLOCKS . "/" . $this->blockId . "/children")
29+
$this->url(Endpoint::BLOCKS . "/" . $this->blockId . "/children" . "?{$this->buildPaginationQuery()}")
3130
);
3231

3332
if (!$response->ok())

src/Endpoints/Database.php

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use FiveamCode\LaravelNotionApi\Query\Sorting;
99
use FiveamCode\LaravelNotionApi\Query\StartCursor;
1010
use FiveamCode\LaravelNotionApi\Exceptions\WrapperException;
11+
use Symfony\Component\VarDumper\Cloner\Data;
1112

1213
class Database extends Endpoint
1314
{
@@ -16,9 +17,6 @@ class Database extends Endpoint
1617
private Collection $filter;
1718
private Collection $sorts;
1819

19-
private ?StartCursor $startCursor = null;
20-
private ?int $pageSize = null;
21-
2220

2321
public function __construct(string $databaseId, Notion $notion)
2422
{
@@ -56,8 +54,6 @@ public function query(): array
5654
if($this->pageSize !== null)
5755
$postData["page_size"] = $this->pageSize;
5856

59-
60-
6157
$response = $this->post(
6258
$this->url(Endpoint::DATABASES . "/{$this->databaseId}/query"),
6359
$postData
@@ -80,20 +76,4 @@ public function sortBy(Collection $sortings)
8076

8177
return $this;
8278
}
83-
84-
public function limit(int $limit)
85-
{
86-
$this->pageSize = min($limit, 100);
87-
88-
return $this;
89-
}
90-
91-
public function offset(StartCursor $startCursor)
92-
{
93-
// toDo
94-
throw WrapperException::instance("Not implemented yet.");
95-
96-
$this->startCursor = $startCursor;
97-
return $this;
98-
}
9979
}

src/Endpoints/Databases.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use FiveamCode\LaravelNotionApi\Entities\DatabaseCollection;
77
use FiveamCode\LaravelNotionApi\Exceptions\WrapperException;
88
use FiveamCode\LaravelNotionApi\Notion;
9+
use FiveamCode\LaravelNotionApi\Query\StartCursor;
910

1011
class Databases extends Endpoint implements EndpointInterface
1112
{
@@ -20,7 +21,7 @@ class Databases extends Endpoint implements EndpointInterface
2021
*/
2122
public function all(): DatabaseCollection
2223
{
23-
$resultData = $this->getJson($this->url(Endpoint::DATABASES));
24+
$resultData = $this->getJson($this->url(Endpoint::DATABASES) . "?{$this->buildPaginationQuery()}");
2425
return new DatabaseCollection($resultData);
2526
}
2627

@@ -30,7 +31,8 @@ public function all(): DatabaseCollection
3031
* notion-api-docs: https://developers.notion.com/reference/get-database
3132
*
3233
* @param string $databaseId
33-
* @return array
34+
* @return Database
35+
* @throws WrapperException
3436
*/
3537
public function find(string $databaseId): Database
3638
{

src/Endpoints/Endpoint.php

Lines changed: 35 additions & 0 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,6 +19,10 @@ class Endpoint
1819
public Notion $notion;
1920
private Collection $validVersions;
2021

22+
23+
protected ?StartCursor $startCursor = null;
24+
protected ?int $pageSize = null;
25+
2126
public function __construct(Notion $notion)
2227
{
2328
$this->validVersions = collect(["v1"]);
@@ -74,4 +79,34 @@ protected function post(string $url, array $body)
7479
return $this->notion->getConnection()->post($url, $body);
7580
}
7681

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+
77112
}

src/Endpoints/Users.php

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use FiveamCode\LaravelNotionApi\Entities\User;
66
use FiveamCode\LaravelNotionApi\Exceptions\WrapperException;
77
use FiveamCode\LaravelNotionApi\Notion;
8+
use FiveamCode\LaravelNotionApi\Query\StartCursor;
89

910
class Users extends Endpoint implements EndpointInterface
1011
{
@@ -18,8 +19,7 @@ class Users extends Endpoint implements EndpointInterface
1819
*/
1920
public function all(): array
2021
{
21-
// toDo: Limit & offset, rename to get() like in eloquent?
22-
return $this->getJson($this->url(Endpoint::USERS));
22+
return $this->getJson($this->url(Endpoint::USERS . "?{$this->buildPaginationQuery()}"));
2323
}
2424

2525
/**
@@ -38,15 +38,4 @@ public function find(string $userId): User
3838
return new User($jsonArray);
3939
}
4040

41-
public function limit()
42-
{
43-
//toDo
44-
throw new WrapperException("not implemented yet");
45-
}
46-
47-
public function offset()
48-
{
49-
//toDo
50-
throw new WrapperException("not implemented yet");
51-
}
5241
}

src/Notion.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ public function pages(): Pages
117117
}
118118

119119
/**
120-
* @return Blocks
120+
* @return Block
121121
*/
122122
public function block(string $blockId): Block
123123
{

0 commit comments

Comments
 (0)