Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions native/swift/Example/Example/ExampleApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ struct ExampleApp: App {
.map { $0.asListViewData }
}),
RootListData(name: "Users", callback: {
try await WordPressAPI.globalInstance.users.listWithEditContext(params: .init())
.data
try await WordPressAPI.globalInstance.users.paginatedWithEditContext(params: UserListParams(perPage: 100))
.map { $0.asListViewData }
}),
RootListData(name: "Plugins", callback: {
Expand All @@ -28,6 +27,10 @@ struct ExampleApp: App {
value.asListViewData
}
}),
RootListData(name: "Posts", callback: {
try await WordPressAPI.globalInstance.posts.paginatedWithEditContext(params: PostListParams(perPage: 100))
.map { $0.asListViewData }
}),
RootListData(name: "Site Health Tests", callback: {
let items: [any ListViewDataConvertable] = [
try await WordPressAPI.globalInstance.siteHealthTests.authorizationHeader().data,
Expand Down
10 changes: 10 additions & 0 deletions native/swift/Example/Example/ListViewData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,13 @@ extension SiteSettingsWithEditContext {
}
}
}

extension PostWithEditContext: ListViewDataConvertable {
public var id: String {
self.slug
}

var asListViewData: ListViewData {
ListViewData(id: self.id, title: self.title.raw, subtitle: self.slug, fields: [:])
}
}
7 changes: 7 additions & 0 deletions native/swift/Sources/wordpress-api/Exports.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,13 @@ public typealias PostTypeDetailsWithEditContext = WordPressAPIInternal.PostTypeD
public typealias PostTypeDetailsWithViewContext = WordPressAPIInternal.PostTypeDetailsWithViewContext
public typealias PostTypeDetailsWithEmbedContext = WordPressAPIInternal.PostTypeDetailsWithEmbedContext

// MARK: - Posts
public typealias SparsePost = WordPressAPIInternal.SparsePost
public typealias PostWithEditContext = WordPressAPIInternal.PostWithEditContext
public typealias PostWithViewContext = WordPressAPIInternal.PostWithViewContext
public typealias PostWithEmbedContext = WordPressAPIInternal.PostWithEmbedContext
public typealias PostListParams = WordPressAPIInternal.PostListParams

// MARK: – Site Settings
public typealias SparseSiteSettings = WordPressAPIInternal.SparseSiteSettings
public typealias SiteSettingsWithEditContext = WordPressAPIInternal.SiteSettingsWithEditContext
Expand Down
153 changes: 153 additions & 0 deletions native/swift/Sources/wordpress-api/Pagination.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
import WordPressAPIInternal

public protocol PaginatableResponse {
associatedtype ParamsType
associatedtype DataType

var nextPageParams: ParamsType? { get }
var prevPageParams: ParamsType? { get }

var data: [DataType] { get }
}

public protocol PaginationAwareExecutor {
associatedtype EditContextResponseType: PaginatableResponse
associatedtype ViewContextResponseType: PaginatableResponse
associatedtype EmbedContextResponseType: PaginatableResponse

/// Known function signatures for Request Executors
func listWithEditContext(params: EditContextResponseType.ParamsType) async throws -> EditContextResponseType
func listWithViewContext(params: ViewContextResponseType.ParamsType) async throws -> ViewContextResponseType
func listWithEmbedContext(params: EmbedContextResponseType.ParamsType) async throws -> EmbedContextResponseType

/// Generated implementation
func paginatedWithEditContext(
params: EditContextResponseType.ParamsType
) async throws -> [EditContextResponseType.DataType]

func paginatedWithViewContext(
params: ViewContextResponseType.ParamsType
) async throws -> [ViewContextResponseType.DataType]

func paginatedWithEmbedContext(
params: EmbedContextResponseType.ParamsType
) async throws -> [EmbedContextResponseType.DataType]
}

extension PaginationAwareExecutor {
/// Fetches all objects from all pages
///
/// This method waits until all objects have been downloaded then returns the results. This can have
/// unexpected memory and time implications.
public func paginatedWithEditContext(
params: EditContextResponseType.ParamsType
) async throws -> [EditContextResponseType.DataType] {
var allObjects: [EditContextResponseType.DataType] = []
var mutableParams: EditContextResponseType.ParamsType = params

repeat {
let response = try await self.listWithEditContext(params: mutableParams)
allObjects.append(contentsOf: response.data)

guard let newParams = response.nextPageParams else {
break
}

mutableParams = newParams
} while true

return allObjects
}

/// Fetches all objects from all pages
///
/// This method waits until all objects have been downloaded then returns the results. This can have
/// unexpected memory and time implications.
public func paginatedWithViewContext(
params: ViewContextResponseType.ParamsType
) async throws -> [ViewContextResponseType.DataType] {
var allObjects: [ViewContextResponseType.DataType] = []
var mutableParams: ViewContextResponseType.ParamsType = params

repeat {
let response = try await self.listWithViewContext(params: mutableParams)
allObjects.append(contentsOf: response.data)

guard let newParams = response.nextPageParams else {
break
}

mutableParams = newParams
} while true

return allObjects
}

/// Fetches all objects from all pages
///
/// This method waits until all objects have been downloaded then returns the results. This can have
/// unexpected memory and time implications.
public func paginatedWithEmbedContext(
params: EmbedContextResponseType.ParamsType
) async throws -> [EmbedContextResponseType.DataType] {
var allObjects: [EmbedContextResponseType.DataType] = []
var mutableParams: EmbedContextResponseType.ParamsType = params

repeat {
let response = try await self.listWithEmbedContext(params: mutableParams)
allObjects.append(contentsOf: response.data)

guard let newParams = response.nextPageParams else {
break
}

mutableParams = newParams
} while true

return allObjects
}
}

// MARK: - Posts
extension PostsRequestListWithEditContextResponse: PaginatableResponse {
public typealias ParamsType = PostListParams
public typealias DataType = PostWithEditContext
}

extension PostsRequestListWithViewContextResponse: PaginatableResponse {
public typealias ParamsType = PostListParams
public typealias DataType = PostWithViewContext
}

extension PostsRequestListWithEmbedContextResponse: PaginatableResponse {
public typealias ParamsType = PostListParams
public typealias DataType = PostWithEmbedContext
}

extension PostsRequestExecutor: PaginationAwareExecutor {
public typealias EditContextResponseType = PostsRequestListWithEditContextResponse
public typealias ViewContextResponseType = PostsRequestListWithViewContextResponse
public typealias EmbedContextResponseType = PostsRequestListWithEmbedContextResponse
}

// MARK: - Users
extension UsersRequestListWithEditContextResponse: PaginatableResponse {
public typealias ParamsType = UserListParams
public typealias DataType = UserWithEditContext
}

extension UsersRequestListWithViewContextResponse: PaginatableResponse {
public typealias ParamsType = UserListParams
public typealias DataType = UserWithViewContext
}

extension UsersRequestListWithEmbedContextResponse: PaginatableResponse {
public typealias ParamsType = UserListParams
public typealias DataType = UserWithEmbedContext
}

extension UsersRequestExecutor: PaginationAwareExecutor {
public typealias EditContextResponseType = UsersRequestListWithEditContextResponse
public typealias ViewContextResponseType = UsersRequestListWithViewContextResponse
public typealias EmbedContextResponseType = UsersRequestListWithEmbedContextResponse
}
4 changes: 4 additions & 0 deletions native/swift/Sources/wordpress-api/WordPressAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ public struct WordPressAPI {
self.requestBuilder.postTypes()
}

public var posts: PostsRequestExecutor {
self.requestBuilder.posts()
}

public var siteSettings: SiteSettingsRequestExecutor {
self.requestBuilder.siteSettings()
}
Expand Down
Loading