Skip to content

Commit 536ef86

Browse files
authored
Merge pull request 5am-code#16 from 5am-code/dev
Dev
2 parents c79ace0 + f0775d5 commit 536ef86

File tree

5 files changed

+73
-26
lines changed

5 files changed

+73
-26
lines changed

CHANGELOG.md

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
11
# Changelog
22

3-
All notable changes to `laravel-notion-api` will be documented in this file
4-
5-
## 1.0.0 - 201X-XX-XX
6-
7-
- initial release
3+
Please check out the recent changelog in the [documentation](https://5amco.de/docs).

src/Endpoints/Pages.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public function find(string $pageId): Page
3939
public function create(): array
4040
{
4141
//toDo
42-
throw new HandlingException('Not implemented');
42+
throw new HandlingException( 'Not implemented');
4343
}
4444

4545

src/Notion.php

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ class Notion
5050
* Notion constructor.
5151
* @param string|null $version
5252
* @param string|null $token
53+
* @throws HandlingException
5354
*/
5455
public function __construct(string $token, string $version = 'v1')
5556
{
@@ -58,16 +59,20 @@ public function __construct(string $token, string $version = 'v1')
5859
$this->validVersions = collect(['v1']);
5960

6061
$this->setVersion($version);
62+
$this->connect();
6163

6264
}
6365

6466
/**
6567
*
6668
* @return Notion
69+
* @throws HandlingException
6770
*/
6871
private function connect(): Notion
6972
{
70-
$this->connection = Http::withToken($this->token);
73+
$this->connection = Http
74+
::withHeaders($this->buildRequestHeader())
75+
->withToken($this->token);
7176
return $this;
7277
}
7378

@@ -89,6 +94,7 @@ public function setVersion(string $version): Notion
8994
* Wrapper function to set version to v1.
9095
*
9196
* @return $this
97+
* @throws HandlingException
9298
*/
9399
public function v1(): Notion
94100
{
@@ -101,11 +107,11 @@ public function v1(): Notion
101107
*
102108
* @param string $token
103109
* @return Notion
110+
* @deprecated for public usage; will be set to private in 0.4.0!
104111
*/
105112
public function setToken(string $token): Notion
106113
{
107114
$this->token = $token;
108-
$this->connect();
109115
return $this;
110116
}
111117

@@ -195,7 +201,37 @@ public function getConnection(): ?PendingRequest
195201
public function checkValidVersion(string $version): void
196202
{
197203
if (!$this->validVersions->contains($version)) {
198-
throw HandlingException::instance('invalid version for notion-api', ['invalidVersion' => $version]);
204+
throw HandlingException::instance('Invalid version for Notion-API endpoint', ['invalidVersion' => $version]);
205+
}
206+
}
207+
208+
/**
209+
* @return string[]
210+
*
211+
* @throws HandlingException
212+
*/
213+
private function buildRequestHeader(): array
214+
{
215+
return [
216+
'Notion-Version' => $this->mapVersionToHeaderVersion()
217+
];
218+
}
219+
220+
/**
221+
* Due to the inconsistency of the Notion API requiring a endpoint url
222+
* with v* as well as a dated version in the request header, this method
223+
* maps the given version (e.g. v1) to the version date Notion requires
224+
* in the header (e.g. "2021-05-13").
225+
* @return string
226+
* @throws HandlingException
227+
*/
228+
private function mapVersionToHeaderVersion(): string
229+
{
230+
switch ($this->version) {
231+
case 'v1':
232+
return '2021-05-13';
233+
default:
234+
throw new HandlingException('Invalid version.');
199235
}
200236
}
201237
}

tests/NotionApiTest.php

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -37,24 +37,9 @@ protected function getPackageAliases($app): array
3737
];
3838
}
3939

40-
41-
/** @test */
42-
public function it_returns_notion_instance_with_set_token_and_connection()
43-
{
44-
$notion = new Notion('secret_*');
45-
$notion->v1()->setToken('secret_*');
46-
47-
$this->assertInstanceOf(Notion::class, $notion);
48-
$this->assertNotEmpty($notion->getConnection());
49-
}
50-
51-
5240
/** @test */
53-
public function it_throws_a_handling_exception_invalid_version()
41+
public function it_asserts_true()
5442
{
55-
$this->expectException(HandlingException::class);
56-
$this->expectExceptionMessage('invalid version for notion-api');
57-
58-
new Notion('secret_*', 'v-does-not-exist');
43+
$this->assertTrue(true);
5944
}
6045
}

tests/NotionTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace FiveamCode\LaravelNotionApi\Tests;
4+
5+
6+
use FiveamCode\LaravelNotionApi\Exceptions\HandlingException;
7+
use FiveamCode\LaravelNotionApi\Notion;
8+
9+
class NotionTest extends NotionApiTest
10+
{
11+
12+
/** @test */
13+
public function it_returns_notion_instance_with_set_token_and_connection()
14+
{
15+
$notion = new Notion('secret_*');
16+
$notion->v1()->setToken('secret_*');
17+
18+
$this->assertInstanceOf(Notion::class, $notion);
19+
$this->assertNotEmpty($notion->getConnection());
20+
}
21+
22+
/** @test */
23+
public function it_throws_a_handling_exception_invalid_version()
24+
{
25+
$this->expectException(HandlingException::class);
26+
$this->expectExceptionMessage('Invalid version for Notion-API endpoint');
27+
28+
new Notion('secret_*', 'v-does-not-exist');
29+
}
30+
}

0 commit comments

Comments
 (0)