@@ -50,6 +50,7 @@ class Notion
50
50
* Notion constructor.
51
51
* @param string|null $version
52
52
* @param string|null $token
53
+ * @throws HandlingException
53
54
*/
54
55
public function __construct (string $ token , string $ version = 'v1 ' )
55
56
{
@@ -58,16 +59,20 @@ public function __construct(string $token, string $version = 'v1')
58
59
$ this ->validVersions = collect (['v1 ' ]);
59
60
60
61
$ this ->setVersion ($ version );
62
+ $ this ->connect ();
61
63
62
64
}
63
65
64
66
/**
65
67
*
66
68
* @return Notion
69
+ * @throws HandlingException
67
70
*/
68
71
private function connect (): Notion
69
72
{
70
- $ this ->connection = Http::withToken ($ this ->token );
73
+ $ this ->connection = Http
74
+ ::withHeaders ($ this ->buildRequestHeader ())
75
+ ->withToken ($ this ->token );
71
76
return $ this ;
72
77
}
73
78
@@ -89,6 +94,7 @@ public function setVersion(string $version): Notion
89
94
* Wrapper function to set version to v1.
90
95
*
91
96
* @return $this
97
+ * @throws HandlingException
92
98
*/
93
99
public function v1 (): Notion
94
100
{
@@ -101,11 +107,11 @@ public function v1(): Notion
101
107
*
102
108
* @param string $token
103
109
* @return Notion
110
+ * @deprecated for public usage; will be set to private in 0.4.0!
104
111
*/
105
112
public function setToken (string $ token ): Notion
106
113
{
107
114
$ this ->token = $ token ;
108
- $ this ->connect ();
109
115
return $ this ;
110
116
}
111
117
@@ -195,7 +201,37 @@ public function getConnection(): ?PendingRequest
195
201
public function checkValidVersion (string $ version ): void
196
202
{
197
203
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. ' );
199
235
}
200
236
}
201
237
}
0 commit comments