Skip to content

Commit 530eade

Browse files
committed
added single database endpoint, replaced generic exception class, fixed constructors
1 parent d50e2d3 commit 530eade

File tree

10 files changed

+91
-81
lines changed

10 files changed

+91
-81
lines changed

src/Endpoints/Blocks.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,6 @@
77

88
class Blocks extends Endpoint implements EndpointInterface
99
{
10-
public function __construct(Notion $notion)
11-
{
12-
$this->notion = $notion;
13-
}
14-
1510
/**
1611
* Retrieve block children
1712
* url: https://api.notion.com/{version}/blocks/{block_id}/children

src/Endpoints/Database.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
namespace FiveamCode\LaravelNotionApi\Endpoints;
4+
5+
6+
use FiveamCode\LaravelNotionApi\Notion;
7+
8+
class Database extends Endpoint
9+
{
10+
private string $databaseId;
11+
12+
public function __construct(string $databaseId, Notion $notion)
13+
{
14+
$this->databaseId = $databaseId;
15+
parent::__construct($notion);
16+
}
17+
18+
public function query(string $databaseId): array
19+
{
20+
21+
$filterJson = '
22+
{
23+
"property": "Tags",
24+
"multi_select": {
25+
"contains": "great"
26+
}
27+
}';
28+
29+
$filter = json_decode($filterJson);
30+
$postData = ["filter" => $filter];
31+
32+
$response = $this->post(
33+
$this->url(Endpoint::DATABASES . "/{$databaseId}/query"),
34+
$postData
35+
)
36+
->json();
37+
38+
dump($response);
39+
return [];
40+
}
41+
42+
public function filterBy()
43+
{
44+
}
45+
46+
public function sortBy()
47+
{
48+
}
49+
50+
public function limit()
51+
{
52+
}
53+
54+
public function offset()
55+
{
56+
}
57+
}

src/Endpoints/Databases.php

Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,8 @@
77

88
class Databases extends Endpoint implements EndpointInterface
99
{
10-
private string $databaseId;
1110

1211

13-
public function __construct(Notion $notion)
14-
{
15-
$this->notion = $notion;
16-
parent::__construct();
17-
}
18-
1912
/**
2013
* List databases
2114
* url: https://api.notion.com/{version}/databases
@@ -44,43 +37,4 @@ public function find(string $databaseId): Database
4437
return new Database($jsonArray);
4538
}
4639

47-
public function query(string $databaseId): array
48-
{
49-
50-
$filterJson = '
51-
{
52-
"property": "Tags",
53-
"multi_select": {
54-
"contains": "great"
55-
}
56-
}';
57-
58-
$filter = json_decode($filterJson);
59-
$postData = ["filter" => $filter];
60-
61-
$response = $this->post(
62-
$this->url(Endpoint::DATABASES . "/{$databaseId}/query"),
63-
$postData
64-
)
65-
->json();
66-
67-
dump($response);
68-
return [];
69-
}
70-
71-
public function filterBy()
72-
{
73-
}
74-
75-
public function sortBy()
76-
{
77-
}
78-
79-
public function limit()
80-
{
81-
}
82-
83-
public function offset()
84-
{
85-
}
8640
}

src/Endpoints/Endpoint.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@ class Endpoint
1818
public Notion $notion;
1919
private Collection $validVersions;
2020

21-
public function __construct()
21+
public function __construct(Notion $notion)
2222
{
2323
$this->validVersions = collect(["v1"]);
24+
$this->notion = $notion;
2425
}
2526

2627
/**

src/Endpoints/Pages.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,12 @@
33
namespace FiveamCode\LaravelNotionApi\Endpoints;
44

55
use FiveamCode\LaravelNotionApi\Entities\Page;
6+
use FiveamCode\LaravelNotionApi\Exceptions\WrapperException;
67
use FiveamCode\LaravelNotionApi\Notion;
78

89
class Pages extends Endpoint implements EndpointInterface
910
{
1011

11-
public function __construct(Notion $notion)
12-
{
13-
$this->notion = $notion;
14-
}
15-
1612
/**
1713
* Retrieve a page
1814
* url: https://api.notion.com/{version}/pages/{page_id}
@@ -23,10 +19,14 @@ public function __construct(Notion $notion)
2319
*/
2420
public function find(string $pageId): Page
2521
{
26-
$jsonArray = $this->getJson(
22+
$response = $this->get(
2723
$this->url(Endpoint::PAGES . "/" . $pageId)
2824
);
29-
return new Page($jsonArray);
25+
26+
if(!$response->ok())
27+
throw WrapperException::instance("Page not found.", ["pageId" => $pageId]);
28+
29+
return new Page($response->json());
3030
}
3131

3232
public function create(): array{

src/Endpoints/Search.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,5 @@
77
class Search extends Endpoint
88
{
99

10-
public function __construct(Notion $notion)
11-
{
12-
$this->notion = $notion;
13-
}
14-
1510
// toDo: Think about it. 🧐
1611
}

src/Endpoints/Users.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,12 @@
33
namespace FiveamCode\LaravelNotionApi\Endpoints;
44

55
use FiveamCode\LaravelNotionApi\Entities\User;
6+
use FiveamCode\LaravelNotionApi\Exceptions\WrapperException;
67
use FiveamCode\LaravelNotionApi\Notion;
78

89
class Users extends Endpoint implements EndpointInterface
910
{
1011

11-
public function __construct(Notion $notion)
12-
{
13-
$this->notion = $notion;
14-
}
15-
1612
/**
1713
* List users
1814
* url: https://api.notion.com/{version}/users
@@ -45,12 +41,12 @@ public function find(string $userId): User
4541
public function limit()
4642
{
4743
//toDo
48-
throw new \Exception("not implemented yet");
44+
throw new WrapperException("not implemented yet");
4945
}
5046

5147
public function offset()
5248
{
5349
//toDo
54-
throw new \Exception("not implemented yet");
50+
throw new WrapperException("not implemented yet");
5551
}
5652
}

src/Notion.php

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22

33
namespace FiveamCode\LaravelNotionApi;
44

5-
use Illuminate\Http\Client\PendingRequest;
65
use Illuminate\Support\Facades\Http;
7-
use FiveamCode\LaravelNotionApi\Endpoints\Blocks;
8-
use FiveamCode\LaravelNotionApi\Endpoints\Databases;
9-
use FiveamCode\LaravelNotionApi\Endpoints\Endpoint;
6+
use Illuminate\Http\Client\PendingRequest;
107
use FiveamCode\LaravelNotionApi\Endpoints\Pages;
8+
use FiveamCode\LaravelNotionApi\Endpoints\Blocks;
119
use FiveamCode\LaravelNotionApi\Endpoints\Search;
1210
use FiveamCode\LaravelNotionApi\Endpoints\Users;
11+
use FiveamCode\LaravelNotionApi\Endpoints\Endpoint;
12+
use FiveamCode\LaravelNotionApi\Endpoints\Database;
13+
use FiveamCode\LaravelNotionApi\Endpoints\Databases;
1314

1415

1516
class Notion
@@ -27,17 +28,17 @@ class Notion
2728
*/
2829
public function __construct(string $version = null, string $token = null)
2930
{
30-
$this->endpoint = new Endpoint();
31+
if ($token !== null) {
32+
$this->setToken($token);
33+
}
34+
35+
$this->endpoint = new Endpoint($this);
3136

3237
if ($version !== null) {
3338
$this->setVersion($version);
3439
} else {
3540
$this->v1();
3641
}
37-
38-
if ($token !== null) {
39-
$this->setToken($token);
40-
}
4142
}
4243

4344
/**
@@ -96,6 +97,14 @@ public function databases(): Databases
9697
return new Databases($this);
9798
}
9899

100+
/**
101+
* @return Database
102+
*/
103+
public function database(string $databaseId): Database
104+
{
105+
return new Database($this, $databaseId);
106+
}
107+
99108
/**
100109
* @return Pages
101110
*/

src/Query/QueryHelper.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22

33
namespace FiveamCode\LaravelNotionApi\Query;
44

5+
use Illuminate\Http\Resources\Json\JsonResource;
56
use Illuminate\Support\Collection;
67

7-
class QueryHelper
8+
class QueryHelper extends JsonResource
89
{
910
/**
1011
* Contains the property name the query helper works with.

src/Query/Sorting.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,6 @@ public function __construct(string $timestamp, string $direction)
2929
}
3030

3131

32+
33+
3234
}

0 commit comments

Comments
 (0)