찬찬의 Notion API 정복기 09 - API 집중분석 - search (하)

소찬 (Chan)·2022년 9월 5일
0
post-thumbnail

Reference API URL : Search

query에 대해 알아보았으니, 다음 parameter 값을 알아보도록 합시다.

  • sort (object)
    When supplied, sorts the results based on the provided criteria. Limitation: Currently only a single sort is allowed and is limited to last_edited_time.
    정렬 기능을 지원하는데, Limitation 이라는 문구가 눈에 띄입니다. last_edited_time 옵션만 지원한다는 의미입니다. Notion API 는 계속 업그레이드가 될텐데 아직은 이 옵션만 지원하니 이 옵션 애용해달라, To be continue... 느낌이랄까
    • direction (string)
      The direction to sort. Possible values include ascending and descending.
    • timestamp (string)
      The name of the timestamp to sort against. Possible values include last_edited_time.

그럼 위 sort 옵션을 코드로 어떻게 작성하면 될까요?

db_or_pages = notion.search(sort={"direction": "ascending", "timestamp": "last_edited_time"})

위와 같이 객체(object)는 dictionary {} 형태로 묶어주시면 됩니다.

  • filter (object)
    When supplied, filters the results based on the provided criteria.
    결과 중 조건에 맞는 결과만 가져오겠다는 얘기인데, database 혹은 page 만 가능합니다.
    • value (string)
      The value of the property to filter the results by. Possible values for object type include page or database. Limitation: Currently the only filter allowed is object which will filter by type of object (either page or database)
    • property (string)
      The name of the property to filter by. Currently the only property you can filter by is the object type. Possible values include object. Limitation: Currently the only filter allowed is object which will filter by type of object (either page or database)

위 내용에 맞게 코드를 작성한다면, filter로 가능한 option은 하기 둘중 하나입니다.
notion.search(filter={"property": "object", "value": "page"}
notion.search(filter={"property": "object", "value": "database"}

  • start_cursor (string)
    If supplied, this endpoint will return a page of results starting after the cursor provided. If not supplied, this endpoint will return the first page of results.
    search 명령어로 최대 조회해올 수 있는 건이 총 100건입니다. 100건이 초과되는 경우, 응답값에 "has_more" : True"next_cursor": "{고유 id}"를 보냅니다. next_cursor 를 참조해서 start_cursor를 입력시키면, 101번째부터 그 이후의 결과를 search 해오실 수 있습니다.

  • page_size (int32)
    The number of items from the full list desired in the response. Maximum: 100
    한번에 몇 건을 조회에 올지 정하실 수 있습니다. 입력을 따로 안할시에는 최대 100건을 조회해 옵니다.

마무리할 겸 전부 옵션을 넣어 search를 호출하는 코드를 알아보겠습니다.

next_cursor = 'ff244636-d084-4cfb-93c3-55e6b82f53a0'
cs_block = notion.search(query="고객센터",
                         sort={"direction": "descending", "timestamp": "last_edited_time"},
                         filter={"property": "object","value": "page"},
                         start_cursor=next_cursor,
                         page_size=10)

위의 코드가 길어보인다면, 가독성 향상을 위해, 변수로 깔쌈(요즘말 드립 욕심 심함)하게 정리해서 호출하셔도 좋습니다.

next_cursor = 'ff244636-d084-4cfb-93c3-55e6b82f53a0'
query_word = '고객센터'
sort_option = {"direction": "descending", "timestamp": "last_edited_time"}
filter_option = {"property": "object","value": "page"}
page_size_default = 10

cs_block = notion.search(query=query_word, sort=sort_option, filter=filter_option,
                         start_cursor=next_cursor, page_size=page_size_default)
profile
QA Specialist

0개의 댓글