api-swift / Sources / IrdAPI / Flows / PaginationFlows.swift
334 words
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import Foundation

// Companions that collect every page. Cursor listings follow `next_cursor`; offset
// listings walk `limit`/`offset` (default 100) until a short page.

extension RegistrationsAPI {
    public func listAll(query: RegistrationListQuery = RegistrationListQuery()) async throws -> [Registration] {
        try await Paginator.cursor(fetch: { (cursor: String?) in
            var q = query
            if let cursor { q.cursor = cursor }
            return try await list(query: q)
        }, items: \.registrations, next: \.nextCursor)
    }

    public func listAllActions(query: ListActionsQuery = ListActionsQuery()) async throws -> [RegistrationAction] {
        try await Paginator.cursor(fetch: { (cursor: Int?) in
            var q = query
            if let cursor { q.cursor = cursor }
            return try await listActions(query: q)
        }, items: \.actions, next: \.nextCursor)
    }
}

extension CreditsAPI {
    public func ledgerAll(query: LedgerQuery = LedgerQuery()) async throws -> [LedgerEntry] {
        try await Paginator.cursor(fetch: { (cursor: Int?) in
            var q = query
            if let cursor { q.cursor = cursor }
            return try await ledger(query: q)
        }, items: \.entries, next: \.nextCursor)
    }
}

extension DomainsAPI {
    public func directoryAll(query: DirectoryQuery = DirectoryQuery()) async throws -> [DirectoryEntry] {
        try await Paginator.cursor(fetch: { (cursor: String?) in
            var q = query
            if let cursor { q.cursor = cursor }
            return try await directory(query: q)
        }, items: \.entries, next: \.nextCursor)
    }
}

extension SupportAPI {
    public func listAllThreads(query: ListThreadsQuery = ListThreadsQuery()) async throws -> [SupportThread] {
        try await Paginator.cursor(fetch: { (cursor: String?) in
            var q = query
            if let cursor { q.cursor = cursor }
            return try await listThreads(query: q)
        }, items: \.threads, next: \.nextCursor)
    }
}

extension IPFSAPI {
    public func listAllPins(query: ListPinsQuery = ListPinsQuery()) async throws -> [Pin] {
        try await Paginator.offset(limit: query.limit, offset: query.offset, fetch: { limit, offset in
            var q = query
            q.limit = limit
            q.offset = offset
            return try await listPins(query: q)
        }, items: \.pins)
    }

    public func listAllDomainPins(id: String, query: ListDomainPinsQuery = ListDomainPinsQuery()) async throws -> [DomainPin] {
        try await Paginator.offset(limit: query.limit, offset: query.offset, fetch: { limit, offset in
            var q = query
            q.limit = limit
            q.offset = offset
            return try await listDomainPins(id: id, query: q)
        }, items: \.pins)
    }
}

static mirror of HEAD · about · clone: git clone https://ird.cod-sursa.ro/api-swift.git