Skip to content

Commit

Permalink
Add support for file-based blocks and embeds/bookmarks (#24)
Browse files Browse the repository at this point in the history
* Add support for file-based blocks and embeds/bookmarks

* Change omitempty and FileObject reference on new structs
  • Loading branch information
duanejeffers authored Sep 22, 2021
1 parent 162126a commit 5e047c0
Show file tree
Hide file tree
Showing 3 changed files with 162 additions and 3 deletions.
140 changes: 140 additions & 0 deletions block.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,128 @@ func (b ChildPageBlock) GetType() BlockType {
return b.Type
}

type EmbedBlock struct {
Object ObjectType `json:"object"`
ID BlockID `json:"id,omitempty"`
Type BlockType `json:"type"`
CreatedTime *time.Time `json:"created_time,omitempty"`
LastEditedTime *time.Time `json:"last_edited_time,omitempty"`
HasChildren bool `json:"has_children,omitempty"`
Embed Embed `json:"embed"`
}

func (b EmbedBlock) GetType() BlockType {
return b.Type
}

type Embed struct {
Caption []RichText `json:"caption,omitempty"`
URL string `json:"url"`
}

type ImageBlock struct {
Object ObjectType `json:"object"`
ID BlockID `json:"id,omitempty"`
Type BlockType `json:"type"`
CreatedTime *time.Time `json:"created_time,omitempty"`
LastEditedTime *time.Time `json:"last_edited_time,omitempty"`
HasChildren bool `json:"has_children,omitempty"`
Image Image `json:"image"`
}

func (b ImageBlock) GetType() BlockType {
return b.Type
}

type Image struct {
Caption []RichText `json:"caption,omitempty"`
Type FileType `json:"type"`
File *FileObject `json:"file,omitempty"`
External *FileObject `json:"external,omitempty"`
}

type VideoBlock struct {
Object ObjectType `json:"object"`
ID BlockID `json:"id,omitempty"`
Type BlockType `json:"type"`
CreatedTime *time.Time `json:"created_time,omitempty"`
LastEditedTime *time.Time `json:"last_edited_time,omitempty"`
HasChildren bool `json:"has_children,omitempty"`
Video Video `json:"video"`
}

func (b VideoBlock) GetType() BlockType {
return b.Type
}

type Video struct {
Caption []RichText `json:"caption,omitempty"`
Type FileType `json:"type"`
File *FileObject `json:"file,omitempty"`
External *FileObject `json:"external,omitempty"`
}

type FileBlock struct {
Object ObjectType `json:"object"`
ID BlockID `json:"id,omitempty"`
Type BlockType `json:"type"`
CreatedTime *time.Time `json:"created_time,omitempty"`
LastEditedTime *time.Time `json:"last_edited_time,omitempty"`
HasChildren bool `json:"has_children,omitempty"`
File BlockFile `json:"file"`
}

func (b FileBlock) GetType() BlockType {
return b.Type
}

type BlockFile struct {
Caption []RichText `json:"caption,omitempty"`
Type FileType `json:"type"`
File *FileObject `json:"file,omitempty"`
External *FileObject `json:"external,omitempty"`
}

type PdfBlock struct {
Object ObjectType `json:"object"`
ID BlockID `json:"id,omitempty"`
Type BlockType `json:"type"`
CreatedTime *time.Time `json:"created_time,omitempty"`
LastEditedTime *time.Time `json:"last_edited_time,omitempty"`
HasChildren bool `json:"has_children,omitempty"`
Pdf Pdf `json:"pdf"`
}

func (b PdfBlock) GetType() BlockType {
return b.Type
}

type Pdf struct {
Caption []RichText `json:"caption,omitempty"`
Type FileType `json:"type"`
File *FileObject `json:"file,omitempty"`
External *FileObject `json:"external,omitempty"`
}

type BookmarkBlock struct {
Object ObjectType `json:"object"`
ID BlockID `json:"id,omitempty"`
Type BlockType `json:"type"`
CreatedTime *time.Time `json:"created_time,omitempty"`
LastEditedTime *time.Time `json:"last_edited_time,omitempty"`
HasChildren bool `json:"has_children,omitempty"`
Bookmark Bookmark `json:"bookmark"`
}

func (b BookmarkBlock) GetType() BlockType {
return b.Type
}

type Bookmark struct {
Caption []RichText `json:"caption,omitempty"`
URL string `json:"url"`
}

func decodeBlock(raw map[string]interface{}) (Block, error) {
var b Block
switch BlockType(raw["type"].(string)) {
Expand All @@ -294,6 +416,18 @@ func decodeBlock(raw map[string]interface{}) (Block, error) {
b = &ToggleBlock{}
case BlockTypeChildPage:
b = &ChildPageBlock{}
case BlockTypeEmbed:
b = &EmbedBlock{}
case BlockTypeImage:
b = &ImageBlock{}
case BlockTypeVideo:
b = &VideoBlock{}
case BlockTypeFile:
b = &FileBlock{}
case BlockTypePdf:
b = &PdfBlock{}
case BlockTypeBookmark:
b = &BookmarkBlock{}
default:
return nil, fmt.Errorf("unsupported block type: %s", raw["type"].(string))
}
Expand All @@ -315,4 +449,10 @@ type BlockUpdateRequest struct {
NumberedListItem *ListItem `json:"numbered_list_item,omitempty"`
ToDo *ToDo `json:"to_do,omitempty"`
Toggle *Toggle `json:"toggle,omtiempty"`
Embed *Embed `json:"embed,omitempty"`
Image *Image `json:"image,omitempty"`
Video *Video `json:"video,omitempty"`
File *BlockFile `json:"file,omitempty"`
Pdf *Pdf `json:"pdf,omitempty"`
Bookmark *Bookmark `json:"bookmark,omitempty"`
}
18 changes: 15 additions & 3 deletions const.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,12 +183,24 @@ const (
BlockTypeBulletedListItem BlockType = "bulleted_list_item"
BlockTypeNumberedListItem BlockType = "numbered_list_item"

BlockTypeToDo BlockType = "to_do"
BlockTypeToggle BlockType = "toggle"
BlockTypeChildPage BlockType = "child_page"
BlockTypeToDo BlockType = "to_do"
BlockTypeToggle BlockType = "toggle"
BlockTypeChildPage BlockType = "child_page"

BlockTypeEmbed BlockType = "embed"
BlockTypeImage BlockType = "image"
BlockTypeVideo BlockType = "video"
BlockTypeFile BlockType = "file"
BlockTypePdf BlockType = "pdf"
BlockTypeBookmark BlockType = "bookmark"
BlockTypeUnsupported BlockType = "unsupported"
)

const (
FileTypeFile FileType = "file"
FileTypeExternal FileType = "external"
)

const (
FormulaTypeString FormulaType = "string"
FormulaTypeNumber FormulaType = "number"
Expand Down
7 changes: 7 additions & 0 deletions object.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,17 @@ func (d *Date) UnmarshalText(data []byte) error {
return nil
}

type FileType string

type File struct {
Name string `json:"name"`
}

type FileObject struct {
URL string `json:"url,omitempty"`
ExpiryTime *time.Time `json:"expiry_time,omitempty"`
}

type PropertyID string

func (pID PropertyID) String() string {
Expand Down

0 comments on commit 5e047c0

Please sign in to comment.