Skip to content

Commit

Permalink
add CachingClient.GetPageIDs()
Browse files Browse the repository at this point in the history
  • Loading branch information
kjk committed Jun 23, 2021
1 parent b1b8347 commit 357df06
Show file tree
Hide file tree
Showing 8 changed files with 11,261 additions and 2,445 deletions.
10 changes: 10 additions & 0 deletions caching_client.go
Expand Up @@ -227,6 +227,7 @@ func (c *CachingClient) cacheRequest(method string, uri string, body []byte, res
os.Remove(path)
return
}
c.RequestsWrittenToCache++
}

func (c *CachingClient) doPostMaybeCached(uri string, body []byte) ([]byte, error) {
Expand Down Expand Up @@ -293,3 +294,12 @@ func (c *CachingClient) DownloadPagesRecursively(startPageID string, afterDownlo
}
return pages, nil
}

// GetPageIDs returns ids of pages in the cache
func (c *CachingClient) GetPageIDs() []string {
var res []string
for id := range c.cache.pageIDToEntries {
res = append(res, id)
}
return res
}
68 changes: 22 additions & 46 deletions caching_downloader/testdata_test.go → caching_client_test.go
@@ -1,14 +1,8 @@
package caching_downloader
package notionapi

import (
"testing"

"github.com/kjk/notionapi/tohtml"

"github.com/kjk/notionapi/tomarkdown"

"github.com/kjk/notionapi"

"github.com/stretchr/testify/require"
)

Expand All @@ -19,19 +13,19 @@ To create the file for a page, run: ./do/do.sh -to-html ${pageID}
and copy data/cache/${pageID}.txt to testdata/
*/

func testDownloadFromCache(t *testing.T, pageID string) *notionapi.Page {
cache, err := NewDirectoryCache("testdata")
func testDownloadFromCache(t *testing.T, pageID string) *Page {
client := &Client{}
cclient, err := NewCachingClient("caching_client_testdata", client)
require.NoError(t, err)
client := &notionapi.Client{}
downloader := New(cache, client)
p, err := downloader.DownloadPage(pageID)
p, err := cclient.DownloadPage(pageID)
require.NoError(t, err)
require.Equal(t, 1, downloader.FromCacheCount)
require.Equal(t, 0, downloader.DownloadedCount)
require.True(t, cclient.RequestsFromCache > 0)
require.Equal(t, 0, cclient.RequestsNotFromCache)
return p
}

func convertToMdAndHTML(t *testing.T, page *notionapi.Page) {
/*
func convertToMdAndHTML(t *testing.T, page *Page) {
{
conv := tomarkdown.NewConverter(page)
md := conv.ToMarkdown()
Expand All @@ -45,26 +39,27 @@ func convertToMdAndHTML(t *testing.T, page *notionapi.Page) {
require.NotEmpty(t, html)
}
}
*/

// https://www.notion.so/Test-headers-6682351e44bb4f9ca0e149b703265bdb
// test that ForEachBlock() works
func TestPage6682351e44bb4f9ca0e149b703265bdb(t *testing.T) {
pid := "6682351e44bb4f9ca0e149b703265bdb"
p := testDownloadFromCache(t, pid)
blockTypes := []string{}
cb := func(block *notionapi.Block) {
cb := func(block *Block) {
blockTypes = append(blockTypes, block.Type)
}
blocks := []*notionapi.Block{p.Root()}
notionapi.ForEachBlock(blocks, cb)
blocks := []*Block{p.Root()}
ForEachBlock(blocks, cb)
expected := []string{
notionapi.BlockPage,
notionapi.BlockHeader,
notionapi.BlockSubHeader,
notionapi.BlockText,
notionapi.BlockSubSubHeader,
notionapi.BlockText,
notionapi.BlockText,
BlockPage,
BlockHeader,
BlockSubHeader,
BlockText,
BlockSubSubHeader,
BlockText,
BlockText,
}
require.Equal(t, blockTypes, expected)
}
Expand All @@ -75,7 +70,7 @@ func TestPage94167af6567043279811dc923edd1f04(t *testing.T) {
pid := "94167af6567043279811dc923edd1f04"
p := testDownloadFromCache(t, pid)
require.Equal(t, 2, len(p.TableViews))
convertToMdAndHTML(t, p)
//convertToMdAndHTML(t, p)
}

// https://www.notion.so/Test-table-no-title-44f1a38eefe94336907c7576ef4dd19b
Expand All @@ -85,24 +80,5 @@ func TestPage44f1a38eefe94336907c7576ef4dd19b(t *testing.T) {
pid := "44f1a38eefe94336907c7576ef4dd19b"
p := testDownloadFromCache(t, pid)
require.Equal(t, 1, len(p.TableViews))
convertToMdAndHTML(t, p)
}

func must(err error) {
if err != nil {
panic(err)
}
}

// benchmark JSON decoding of requests
func BenchmarkJSONDecode(b *testing.B) {
pid := "44f1a38eefe94336907c7576ef4dd19b"
for i := 0; i < b.N; i++ {
cache, err := NewDirectoryCache("testdata")
must(err)
client := &notionapi.Client{}
downloader := New(cache, client)
_, err = downloader.DownloadPage(pid)
must(err)
}
//convertToMdAndHTML(t, p)
}

0 comments on commit 357df06

Please sign in to comment.