Skip to content

Commit

Permalink
Handle paged responses
Browse files Browse the repository at this point in the history
  • Loading branch information
finnvoor committed Jan 30, 2024
1 parent eb49fdf commit 0c646fe
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions Sources/xcc.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,15 @@ import SwiftTUI
let provider = APIProvider(configuration: configuration)

ActivityIndicator.start()
let bundleIDs = try await provider.request(
let bundleIDs = try await provider.requestAll(
APIEndpoint.v1.bundleIDs.get(parameters: .init(fieldsBundleIDs: [.identifier], limit: 200))
).data
).flatMap(\.data)

var products = try await provider.request(
var products = try await provider.requestAll(
APIEndpoint.v1.ciProducts.get(parameters: .init(
include: [.bundleID]
))
).data
).flatMap(\.data)
ActivityIndicator.stop()

// Some products have the internal ASC bundleID ID
Expand All @@ -79,9 +79,9 @@ import SwiftTUI
}

ActivityIndicator.start()
let workflows = try await provider.request(
let workflows = try await provider.requestAll(
APIEndpoint.v1.ciProducts.id(selectedProduct.id).workflows.get()
).data
).flatMap(\.data)
ActivityIndicator.stop()

let selectedWorkflow = if let workflow {
Expand All @@ -98,9 +98,9 @@ import SwiftTUI
APIEndpoint.v1.ciWorkflows.id(selectedWorkflow.id).repository.get()
).data

let gitReferences = try await provider.request(
let gitReferences = try await provider.requestAll(
APIEndpoint.v1.scmRepositories.id(repository.id).gitReferences.get()
).data
).flatMap(\.data)
ActivityIndicator.stop()

let selectedGitReference = if let reference {
Expand Down Expand Up @@ -197,3 +197,13 @@ extension ScmGitReference: CustomStringConvertible {
}
}
}

extension APIProvider {
func requestAll<T: Decodable>(_ endpoint: Request<T>) async throws -> [T] {
var results: [T] = []
for try await pagedResult in paged(endpoint) {
results.append(pagedResult)
}
return results
}
}

0 comments on commit 0c646fe

Please sign in to comment.