kjk / notionapi

Unofficial Go API for Notion.so
https://blog.kowalczyk.info/article/c9df78cbeaae4e0cb2848c9964bcfc94/using-notion-api-go-client.html
BSD 2-Clause "Simplified" License
1.82k stars 86 forks source link

Filters #25

Closed tattali closed 4 years ago

tattali commented 4 years ago

Related to #24

I update a bit the types

client := &notionapi.Client{}
client.AuthToken = "token"

collectionID := "collectionID"
collectionViewID := "collectionViewID

// query Pages with "ok" as title
newQuery := &notionapi.Query{
    Filter: &notionapi.QueryFilterWrapper{
        Filters: []*notionapi.QueryFilterGroup{
            {
                Filter: &notionapi.QueryFilter{
                    Operator: "string_contains",
                    Value: struct {
                        Type  string `json:"type"`
                        Value string `json:"value"`
                    }{
                        Type:  "exact",
                        Value: "ok",
                    },
                },
                Property: "title",
            },
        },
        Operator: "and",
    },
}

resp, err := client.QueryCollection(collectionID, collectionViewID, newQuery, &notionapi.User{})
if err != nil {
    log.Fatalf("QueryCollection() failed with %s\n", err)
}

count := 0
for _, record := range resp.RecordMap.Blocks {
    block := record.Block

    // count only Pages
    if len(block.GetProperty("title")) > 0 && block.IsPage() && !block.IsSubPage() && !block.IsLinkToPage() {
        count++
    }
}
log.Print(count) // output 1 for me
tattali commented 4 years ago

Hi @kjk, I am new with golang, and I don't understand this error.

kjk commented 4 years ago

Which error?

tattali commented 4 years ago

Well in smoke tests.

Screenshot 2020-06-08 at 12 07 57
kjk commented 4 years ago

It means that the structure of json and the Go struct don't match. Specifically it tells you that json has an array but your struct definition has just a struct. That leads to decoding error.

tattali commented 4 years ago

Hi @kjk, I think it might be because there is some cache somewhere with still an array sent to Query.filter, but I don't know where. All snapshots have a filter property to null

Thanks for your help

tattali commented 4 years ago

Hi, do you have an advice ? Because I don't understand how to make the QueryFilterWrapper nullable. If this is really the issue.

Thank you very much

tattali commented 4 years ago

Maybe this because in your notion use for tests there is any filters ?

tattali commented 4 years ago

Hi @kjk do you think this is a fixable error? Because it seems to be only in the tests, not when I use the changes locally.

tattali commented 4 years ago

Hi @kjk I am still stuck with this error

kjk commented 4 years ago

Sorry, I don't have the bandwidth to debug other's people code.

TipsyPixie commented 4 years ago

Hi, @tattali You're binding an array to a map. You've changed the type of Query.Filter from []*QueryFilter to *QueryFilterWrapper, but query.filter in the actual JSON response is an array.

ex) from smoke test page

[{id: "4071292f-ac9b-4b6a-b40d-e2d55f3a5003", type: "select", value: "Notion", property: "RA!P",…}]

You can write your own Unmarshal() to bind an array to QueryFilterWrapper.QueryFilterGroup properly.