Skip to content

Commit 0437277

Browse files
authored
Merge pull request #6 from 5am-code/main
pull current state of main
2 parents c081399 + b9454e7 commit 0437277

29 files changed

+1100
-199
lines changed

README.md

Lines changed: 70 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
1-
# Laravel Notion API
1+
<h1 align="center"> Laravel Notion API</h1>
2+
<h2 align="center"> Effortless Notion integrations with Laravel</h2>
3+
4+
<p align="center">
5+
<img src="https://5amco.de/images/5am.png" width="200" height="200">
6+
</p>
27

38
[![Latest Version on Packagist](https://img.shields.io/packagist/v/fiveam-code/laravel-notion-api.svg?style=flat-square)](https://packagist.org/packages/fiveam-code/laravel-notion-api)
49
[![Total Downloads](https://img.shields.io/packagist/dt/fiveam-code/laravel-notion-api.svg?style=flat-square)](https://packagist.org/packages/fiveam-code/laravel-notion-api)
5-
![GitHub Actions](https://github.com/fiveam-code/laravel-notion-api/actions/workflows/main.yml/badge.svg)
10+
11+
[comment]: <> (![GitHub Actions]&#40;https://github.com/fiveam-code/laravel-notion-api/actions/workflows/main.yml/badge.svg&#41;)
12+
13+
This package provides a simple and crisp way to access the Notion API endpoints, query data and update existing entries.
614

715
## Installation
816

@@ -12,12 +20,68 @@ You can install the package via composer:
1220
composer require fiveam-code/laravel-notion-api
1321
```
1422

23+
### Authorization
24+
25+
The Notion API requires an access token and a Notion integration, [the Notion documentation](https://developers.notion.com/docs/getting-started#before-we-begin) explains how this works. It's important to grant access to the integration within your Notion account to enable the API access.
26+
27+
Add your Notion API token to your `.env` file:
28+
29+
```
30+
NOTION_API_TOKEN="$YOUR_ACCESS_TOKEN"
31+
```
32+
1533
## Usage
1634

35+
Head over to the [Documentation](https://5amco.de/docs) of this package.
36+
37+
### 🔥 Code Examples to jumpstart your Notion API Project
38+
39+
#### Basic Setup
1740
```php
18-
// Usage description here
41+
use FiveamCode\LaravelNotionApi\Notion;
42+
use Illuminate\Support\Collection;
43+
use FiveamCode\LaravelNotionApi\Query\Sorting;
44+
use FiveamCode\LaravelNotionApi\Query\Filter;
45+
46+
// Setup basic API connection
47+
$notion = new Notion();
48+
$notion->v1();
1949
```
2050

51+
#### Fetch Page Information
52+
```php
53+
// Returns a specific page
54+
$notion->pages()->find($yourPageId);
55+
```
56+
57+
#### Query Database
58+
```php
59+
// Queries a specific database and returns a collection of pages (= database entries)
60+
$sortings = new Collection();
61+
$filters = new Collection();
62+
63+
$sortings
64+
->add(Sorting::propertySort("Ordered", "ascending"));
65+
$sortings
66+
->add(Sorting::timestampSort("created_time", "ascending"));
67+
68+
$filters
69+
->add(Filter::textFilter("title", ["contains" => "new"]));
70+
// or
71+
$filters
72+
->add(Filter::rawFilter("Tags", ["multi_select" => ["contains" => "great"]]));
73+
74+
$notion
75+
->database($yourDatabaseId)
76+
->filterBy($filters) // filters are optional
77+
->sortBy($sortings) // sorts are optional
78+
->limit(5) // limit is optional
79+
->query();
80+
```
81+
82+
83+
84+
2185
### Testing
2286

2387
```bash
@@ -38,14 +102,13 @@ If you discover any security related issues, please email hello@dianaweb.dev ins
38102

39103
## Credits
40104

41-
- [Diana Scharf](https://github.com/mechelon)
42-
- [Johannes Güntner](https://github.com/johguentner)
105+
- [Diana Scharf](https://github.com/mechelon)
106+
- [Johannes Güntner](https://github.com/johguentner)
43107

44108
## License
45109

46110
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.
47111

48-
49-
50112
## Laravel Package Boilerplate
113+
51114
This package was generated using the [Laravel Package Boilerplate](https://laravelpackageboilerplate.com).

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\Collections\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
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 (as raw json-data)
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\Collections\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\Collections\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
}

0 commit comments

Comments
 (0)