Skip to content

Commit 45292f1

Browse files
authored
Merge branch 'dev' into feature/block-creation
2 parents 77098ac + 30c4cec commit 45292f1

File tree

7 files changed

+125
-8
lines changed

7 files changed

+125
-8
lines changed

src/Endpoints/Block.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,25 @@ public function __construct(Notion $notion, string $blockId)
3232
$this->blockId = $blockId;
3333
}
3434

35+
/**
36+
* Retrieve a block
37+
* url: https://api.notion.com/{version}/blocks/{block_id}
38+
* notion-api-docs: https://developers.notion.com/reference/retrieve-a-block
39+
*
40+
* @param string $blockId
41+
* @return BlockEntity
42+
* @throws HandlingException
43+
* @throws NotionException
44+
*/
45+
public function retrieve(): BlockEntity {
46+
47+
$response = $this->get(
48+
$this->url(Endpoint::BLOCKS . '/' . $this->blockId)
49+
);
50+
51+
return BlockEntity::fromResponse($response->json());
52+
}
53+
3554
/**
3655
* Retrieve block children
3756
* url: https://api.notion.com/{version}/blocks/{block_id}/children [get]

src/Exceptions/NotionException.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace FiveamCode\LaravelNotionApi\Exceptions;
44

55
use Illuminate\Http\Client\Response;
6+
use Throwable;
67

78
/**
89
* Class NotionException
@@ -33,8 +34,22 @@ public static function instance(string $message, array $payload = []): NotionExc
3334
*/
3435
public static function fromResponse(Response $response): NotionException
3536
{
37+
$responseBody = json_decode($response->getBody()->getContents(), true);
38+
39+
$errorCode = $errorMessage = "";
40+
if (array_key_exists("code", $responseBody)) {
41+
$errorCode = "({$responseBody["code"]})";
42+
}
43+
44+
if (array_key_exists("code", $responseBody)) {
45+
$errorMessage = "({$responseBody["message"]})";
46+
}
47+
48+
$message = "{$response->getReasonPhrase()}: {$errorCode} {$errorMessage}";
49+
3650
return new NotionException(
37-
$response->getReasonPhrase(), 0,
51+
$message,
52+
0,
3853
$response->toException()
3954
);
4055
}

tests/EndpointBlocksTest.php

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

33
namespace FiveamCode\LaravelNotionApi\Tests;
44

5+
use FiveamCode\LaravelNotionApi\Entities\PropertyItems\RichText;
56
use Notion;
67
use Illuminate\Support\Facades\Http;
78
use FiveamCode\LaravelNotionApi\Entities\Blocks\Block;
@@ -59,7 +60,7 @@ public function it_returns_block_collection_with_children()
5960
Http::fake([
6061
'https://api.notion.com/v1/blocks/b55c9c91-384d-452b-81db-d1ef79372b76/children*'
6162
=> Http::response(
62-
json_decode(file_get_contents('tests/stubs/endpoints/blocks/response_specific_200.json'), true),
63+
json_decode(file_get_contents('tests/stubs/endpoints/blocks/response_specific_block_children_200.json'), true),
6364
200,
6465
['Headers']
6566
)
@@ -299,4 +300,24 @@ public function it_returns_parent_block_in_which_new_blocks_have_been_successful
299300
$parentBlock = Notion::block('1d719dd1-563b-4387-b74f-20da92b827fb')->append([$paragraph, $bulletedListItem, $headingOne, $headingTwo, $headingThree, $numberedListItem, $toDo, $toggle, $embed, $image, $video, $pdf]);
300301
$this->assertInstanceOf(Block::class, $parentBlock);
301302
}
303+
304+
/** @test */
305+
public function it_retrieves_a_single_block()
306+
{
307+
// successful /v1/blocks/BLOCK_DOES_EXIST
308+
Http::fake([
309+
'https://api.notion.com/v1/blocks/a6f8ebe8d5df4ffab543bcd54d1c3bad'
310+
=> Http::response(
311+
json_decode(file_get_contents('tests/stubs/endpoints/blocks/response_specific_block_200.json'), true),
312+
200,
313+
['Headers']
314+
)
315+
]);
316+
317+
$block = \Notion::block("a6f8ebe8d5df4ffab543bcd54d1c3bad")->retrieve();
318+
319+
$this->assertInstanceOf(Block::class, $block);
320+
$this->assertInstanceOf(Paragraph::class, $block);
321+
}
322+
302323
}

tests/NotionExceptionTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace FiveamCode\LaravelNotionApi\Tests;
4+
5+
use FiveamCode\LaravelNotionApi\Exceptions\NotionException;
6+
use Notion;
7+
use Illuminate\Support\Facades\Http;
8+
9+
/**
10+
* Class HandlingExceptionTest
11+
* @package FiveamCode\LaravelNotionApi\Tests
12+
*/
13+
class NotionExceptionTest extends NotionApiTest
14+
{
15+
16+
/** @test */
17+
public function it_throws_a_notion_exception_with_detailed_message_from_response()
18+
{
19+
20+
Http::fake([
21+
'https://api.notion.com/v1/blocks/d092140ce4e549bf9915fb8ad43d1699d/children*'
22+
=> Http::response(
23+
json_decode(file_get_contents("tests/stubs/endpoints/blocks/response_children_invalid_uuid_400.json"), true),
24+
400,
25+
['Headers']
26+
)
27+
]);
28+
29+
$this->expectException(NotionException::class);
30+
$this->expectExceptionMessage("Bad Request: (validation_error) (path failed validation: path.id should be a valid uuid, instead was");
31+
32+
\Notion::block("d092140ce4e549bf9915fb8ad43d1699d")->children()->asCollection();
33+
}
34+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"object": "error",
3+
"status": 400,
4+
"code": "validation_error",
5+
"message": "path failed validation: path.id should be a valid uuid, instead was `\"d092140ce4e549bf9915fb8ad43d1699d\"`."
6+
}
Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,30 @@
11
{
2-
"object": "block",
3-
"id": "1d719dd1-563b-4387-b74f-20da92b827fb",
4-
"created_time": "2021-07-17T16:51:00.000Z",
5-
"last_edited_time": "2021-07-17T16:54:00.000Z",
6-
"has_children": false,
7-
"type": "page"
2+
"object": "block",
3+
"id": "a6f8ebe8-d5df-4ffa-b543-bcd54d1c3bad",
4+
"created_time": "2021-05-17T13:51:00.000Z",
5+
"last_edited_time": "2021-06-10T17:40:00.000Z",
6+
"has_children": false,
7+
"archived": false,
8+
"type": "paragraph",
9+
"paragraph": {
10+
"text": [
11+
{
12+
"type": "text",
13+
"text": {
14+
"content": "C:\\xampp\\php",
15+
"link": null
16+
},
17+
"annotations": {
18+
"bold": false,
19+
"italic": false,
20+
"strikethrough": false,
21+
"underline": false,
22+
"code": false,
23+
"color": "default"
24+
},
25+
"plain_text": "C:\\xampp\\php",
26+
"href": null
27+
}
28+
]
29+
}
830
}

0 commit comments

Comments
 (0)