22
33namespace FiveamCode \LaravelNotionApi \Endpoints ;
44
5- use FiveamCode \LaravelNotionApi \Notion ;
6- use FiveamCode \LaravelNotionApi \Exceptions \NotionException ;
7- use FiveamCode \LaravelNotionApi \Exceptions \HandlingException ;
5+ use FiveamCode \LaravelNotionApi \Entities \Blocks \Block as BlockEntity ;
86use FiveamCode \LaravelNotionApi \Entities \Collections \BlockCollection ;
7+ use FiveamCode \LaravelNotionApi \Exceptions \HandlingException ;
8+ use FiveamCode \LaravelNotionApi \Exceptions \NotionException ;
9+ use FiveamCode \LaravelNotionApi \Notion ;
910
1011/**
1112 * Class Block
@@ -31,9 +32,29 @@ public function __construct(Notion $notion, string $blockId)
3132 $ this ->blockId = $ blockId ;
3233 }
3334
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+
48+ $ response = $ this ->get (
49+ $ this ->url (Endpoint::BLOCKS . '/ ' . $ this ->blockId )
50+ );
51+
52+ return BlockEntity::fromResponse ($ response ->json ());
53+ }
54+
3455 /**
3556 * Retrieve block children
36- * url: https://api.notion.com/{version}/blocks/{block_id}/children
57+ * url: https://api.notion.com/{version}/blocks/{block_id}/children [get]
3758 * notion-api-docs: https://developers.notion.com/reference/get-block-children
3859 *
3960 * @return BlockCollection
@@ -50,11 +71,37 @@ public function children(): BlockCollection
5071 }
5172
5273 /**
53- * @return array
74+ * Append one Block or an array of Blocks
75+ * url: https://api.notion.com/{version}/blocks/{block_id}/children [patch]
76+ * notion-api-docs: https://developers.notion.com/reference/patch-block-children
77+ *
78+ * @return FiveamCode\LaravelNotionApi\Entities\Blocks\Block
5479 * @throws HandlingException
5580 */
56- public function create ( ): array
81+ public function append ( array | BlockEntity $ appendices ): BlockEntity
5782 {
58- throw new HandlingException ('Not implemented ' );
83+ if (!is_array ($ appendices )) {
84+ $ appendices = [$ appendices ];
85+ }
86+ $ children = [];
87+
88+ foreach ($ appendices as $ block ) {
89+ $ children [] = [
90+ "object " => "block " ,
91+ "type " => $ block ->getType (),
92+ $ block ->getType () => $ block ->getRawContent ()
93+ ];
94+ }
95+
96+ $ body = [
97+ "children " => $ children
98+ ];
99+
100+ $ response = $ this ->patch (
101+ $ this ->url (Endpoint::BLOCKS . '/ ' . $ this ->blockId . '/children ' . "" ),
102+ $ body
103+ );
104+
105+ return new BlockEntity ($ response ->json ());
59106 }
60107}
0 commit comments