packages feed

hstratus-notes-0.1.2.0 / src-internal / Network / HStratus / Internal / Notes / Endpoints.hs

{-# LANGUAGE NamedFieldPuns #-}{-# LANGUAGE OverloadedStrings #-}{- |Module      : Network.HStratus.Internal.Notes.EndpointsCopyright   : (c) 2026 Tim EmiolaMaintainer  : Tim Emiola <adetokunbo@emio.la>SPDX-License-Identifier: BSD-3-ClauseConstructs HTTP requests and JSON request bodies for the iCloud NotesCloudKit API.-}module Network.HStratus.Internal.Notes.Endpoints  ( NotesEndpoints  , mkNotesEndpoints  , queryReq  , lookupReq  , changesReq  , foldersBody  , recentsBody  , lookupBody  , changesBody  )whereimport Data.Aeson (Value, encode, object, (.=))import qualified Data.ByteString.Char8 as BS8import qualified Data.ByteString.Lazy as LBSimport Data.Text (Text)import qualified Data.Text as Textimport Network.HStratus.Http.Common  ( icloudBrowserHeaders  , lookupWebservice  , stripTrailingSlash  , withHeaders  )import Network.HStratus.Session (AccountData (..), Session (..))import Network.HTTP.Client  ( Request (..)  )import Network.HTTP.Types (methodPost)-- | Base request for the iCloud Notes CloudKit database API.data NotesEndpoints = NotesEndpoints  { neBaseReq :: !Request  -- ^ base request targeting the Notes CloudKit database path  , neQueryString :: !BS8.ByteString  -- ^ shared query string appended to every Notes API request  }{- | Construct 'NotesEndpoints' from the account data returned after login.Fails if the @ckdatabasews@ service URL is absent from the account data.-}mkNotesEndpoints :: AccountData -> Session -> IO NotesEndpointsmkNotesEndpoints ad sess = do  svcReq <- lookupWebservice "ckdatabasews" (adWebservices ad)  let baseReq =        withHeaders icloudBrowserHeaders $          svcReq            { path =                stripTrailingSlash (path svcReq)                  <> "/database/1/com.apple.notes/production/private"            }      qs =        "remapEnums=true&getCurrentSyncToken=true&clientId="          <> BS8.pack (Text.unpack (sessionClientId sess))  pure NotesEndpoints{neBaseReq = baseReq, neQueryString = qs}-- | Build the @POST /records/query@ request.queryReq :: NotesEndpoints -> RequestqueryReq = notesReq "/records/query"-- | Build the @POST /records/lookup@ request.lookupReq :: NotesEndpoints -> RequestlookupReq = notesReq "/records/lookup"-- | Build the @POST /changes/zone@ request.changesReq :: NotesEndpoints -> RequestchangesReq = notesReq "/changes/zone"notesReq :: BS8.ByteString -> NotesEndpoints -> RequestnotesReq suffix ep =  (neBaseReq ep)    { path = path (neBaseReq ep) <> suffix    , method = methodPost    , queryString = neQueryString ep    }{- | Build the JSON body for a folders query.  Pass the previous response's@continuationMarker@ to page through results.-}foldersBody :: Int -> Maybe Value -> LBS.ByteStringfoldersBody limit marker = encode $ object $ base <> cont where  base =    [ "query"        .= object          [ "recordType" .= ("SearchIndexes" :: Text)          , "filterBy" .= [indexFilter "parentless"]          ]    , "zoneID" .= notesZoneId    , "resultsLimit" .= min notesMaxResults limit    ]  cont = maybe [] (\m -> ["continuationMarker" .= m]) marker{- | Build the JSON body for a recent-notes query.  Pass the previousresponse's @continuationMarker@ to page through results.-}recentsBody :: Int -> Maybe Value -> LBS.ByteStringrecentsBody limit marker = encode $ object $ base <> cont where  base =    [ "query"        .= object          [ "recordType" .= ("SearchIndexes" :: Text)          , "filterBy" .= [indexFilter "recents"]          , "sortBy"              .= [ object                     [ "fieldName" .= ("modTime" :: Text)                     , "ascending" .= False                     ]                 ]          ]    , "zoneID" .= notesZoneId    , "resultsLimit" .= min notesMaxResults limit    ]  cont = maybe [] (\m -> ["continuationMarker" .= m]) marker-- | Build the JSON body for a record lookup by name.lookupBody :: [Text] -> LBS.ByteStringlookupBody names =  encode $    object      [ "records" .= map (\n -> object ["recordName" .= n]) names      , "zoneID" .= notesZoneId      ]{- | Build the JSON body for a zone-changes request.  Pass the previous@syncToken@ to fetch only changes since that token.-}changesBody :: Maybe Text -> LBS.ByteStringchangesBody syncToken =  encode $    object      ["zones" .= [object $ zoneBase <> syncPart]] where  zoneBase =    [ "zoneID" .= notesZoneId    , "desiredRecordTypes" .= [noteRecordType]    ]  syncPart = maybe [] (\t -> ["syncToken" .= t]) syncToken-- HelpersnoteRecordType :: TextnoteRecordType = "Note"notesMaxResults :: IntnotesMaxResults = 200notesZoneId :: ValuenotesZoneId =  object    [ "zoneName" .= ("Notes" :: Text)    , "zoneType" .= ("REGULAR_CUSTOM_ZONE" :: Text)    ]indexFilter :: Text -> ValueindexFilter val =  object    [ "comparator" .= ("EQUALS" :: Text)    , "fieldName" .= ("indexName" :: Text)    , "fieldValue" .= object ["type" .= ("STRING" :: Text), "value" .= val]    ]