This repository was archived by the owner on Sep 15, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
Extend SubscriberServiceRemote #837
Merged
Merged
Changes from 15 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
00c78cb
Add search to subscribers
kean 92c48fc
Add search to initializer
kean bb34b0f
Fix missing query
kean 40342e8
Simplify how filters are passed
kean 8ffb0d4
Add CaseIterable
kean 0bd3c27
Add @frozen
kean dc27315
Extract SubscribersServiceRemote
kean 5ecc194
Add getSubsciberStats
kean e8e8652
Add getSubsciberDetails
kean a128446
Add support for plans
kean 34f8857
Add SubsciberBasicInfoResponse
kean f20b956
Add type parameter
kean 0454d46
Safer decoding
kean 9b22001
Cleanup the test data
kean c0ccd26
Make filter public
kean dfd1877
Add importSubscribers
kean cc7fd8a
Fix download link
kean File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
257 changes: 257 additions & 0 deletions
257
Sources/WordPressKit/Services/SubscribersServiceRemote.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,257 @@ | ||
| import Foundation | ||
|
|
||
| public class SubscribersServiceRemote: ServiceRemoteWordPressComREST { | ||
|
|
||
| // MARK: GET Subscribers (Paginated List) | ||
|
|
||
| public struct GetSubscribersParameters: Hashable { | ||
| public var sortField: SortField? | ||
| public var sortOrder: SortOrder? | ||
| public var subscriptionTypeFilter: FilterSubscriptionType? | ||
| public var paymentTypeFilter: FilterPaymentType? | ||
|
|
||
| @frozen public enum SortField: String, CaseIterable { | ||
| case dateSubscribed = "date_subscribed" | ||
| case email = "email" | ||
| case name = "name" | ||
| case plan = "plan" | ||
| case subscriptionStatus = "subscription_status" | ||
| } | ||
|
|
||
| @frozen public enum SortOrder: String, CaseIterable { | ||
| case ascending = "asc" | ||
| case descending = "dsc" | ||
| } | ||
|
|
||
| @frozen public enum FilterSubscriptionType: String, CaseIterable { | ||
| case email = "email_subscriber" | ||
| case reader = "reader_subscriber" | ||
| case unconfirmed = "unconfirmed_subscriber" | ||
| case blocked = "blocked_subscriber" | ||
| } | ||
|
|
||
| @frozen public enum FilterPaymentType: String, CaseIterable { | ||
| case free | ||
| case paid | ||
| } | ||
|
|
||
| public var filters: [String] { | ||
| [subscriptionTypeFilter?.rawValue, paymentTypeFilter?.rawValue].compactMap { $0 } | ||
| } | ||
|
|
||
| public init(sortField: SortField? = nil, sortOrder: SortOrder? = nil, subscriptionTypeFilter: FilterSubscriptionType? = nil, paymentTypeFilter: FilterPaymentType? = nil) { | ||
| self.sortField = sortField | ||
| self.sortOrder = sortOrder | ||
| self.subscriptionTypeFilter = subscriptionTypeFilter | ||
| self.paymentTypeFilter = paymentTypeFilter | ||
| } | ||
| } | ||
|
|
||
| public struct GetSubscribersResponse: Decodable { | ||
| public var total: Int | ||
| public var pages: Int | ||
| public var page: Int | ||
| public var subscribers: [Subscriber] | ||
|
|
||
| public struct Subscriber: Decodable, SubsciberBasicInfoResponse { | ||
| public let subscriberID: Int | ||
| public let dotComUserID: Int | ||
| public let displayName: String? | ||
| public let avatar: String? | ||
| public let emailAddress: String? | ||
| public let dateSubscribed: Date | ||
| public let isEmailSubscriptionEnabled: Bool | ||
| public let subscriptionStatus: String? | ||
|
|
||
| public init(from decoder: any Decoder) throws { | ||
| let container = try decoder.container(keyedBy: StringCodingKey.self) | ||
| subscriberID = try container.decode(Int.self, forKey: "subscription_id") | ||
| dotComUserID = try container.decode(Int.self, forKey: "user_id") | ||
| displayName = try? container.decodeIfPresent(String.self, forKey: "display_name") | ||
| avatar = try? container.decodeIfPresent(String.self, forKey: "avatar") | ||
| emailAddress = try? container.decodeIfPresent(String.self, forKey: "email_address") | ||
| dateSubscribed = try container.decode(Date.self, forKey: "date_subscribed") | ||
| isEmailSubscriptionEnabled = try container.decode(Bool.self, forKey: "is_email_subscriber") | ||
| subscriptionStatus = try? container.decodeIfPresent(String.self, forKey: "subscription_status") | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// Gets the list of the site subscribers, including WordPress.com users and | ||
| /// email subscribers. | ||
| public func getSubscribers( | ||
| siteID: Int, | ||
| page: Int? = nil, | ||
| perPage: Int? = 25, | ||
| parameters: GetSubscribersParameters = .init(), | ||
| search: String? = nil, | ||
| ) async throws -> GetSubscribersResponse { | ||
| let url = self.path(forEndpoint: "sites/\(siteID)/subscribers", withVersion: ._2_0) | ||
| var query: [String: Any] = [:] | ||
| if let page { | ||
| query["page"] = page | ||
| } | ||
| if let perPage { | ||
| query["per_page"] = perPage | ||
| } | ||
| if let sortField = parameters.sortField { | ||
| query["sort"] = sortField.rawValue | ||
| } | ||
| if let sortOrder = parameters.sortOrder { | ||
| query["sort_order"] = sortOrder.rawValue | ||
| } | ||
| if !parameters.filters.isEmpty { | ||
| query["filters"] = parameters.filters | ||
| } | ||
| if let search, !search.isEmpty { | ||
| query["search"] = search | ||
| } | ||
|
|
||
| let decoder = JSONDecoder() | ||
| decoder.dateDecodingStrategy = JSONDecoder.DateDecodingStrategy.supportMultipleDateFormats | ||
|
|
||
| return try await wordPressComRestApi.perform( | ||
| .get, | ||
| URLString: url, | ||
| parameters: query, | ||
| jsonDecoder: decoder, | ||
| type: GetSubscribersResponse.self | ||
| ).get().body | ||
| } | ||
|
|
||
| // MARK: GET Subscriber (Individual Details) | ||
|
|
||
| public protocol SubsciberBasicInfoResponse { | ||
| var dotComUserID: Int { get } | ||
| var subscriberID: Int { get } | ||
| var displayName: String? { get } | ||
| var emailAddress: String? { get } | ||
| var avatar: String? { get } | ||
| var dateSubscribed: Date { get } | ||
| } | ||
|
|
||
| public final class GetSubscriberDetailsResponse: Decodable, SubsciberBasicInfoResponse { | ||
| public let subscriberID: Int | ||
| public let dotComUserID: Int | ||
| public let displayName: String? | ||
| public let avatar: String? | ||
| public let emailAddress: String? | ||
| public let siteURL: String? | ||
| public let dateSubscribed: Date | ||
| public let isEmailSubscriptionEnabled: Bool | ||
| public let subscriptionStatus: String? | ||
| public let country: Country? | ||
| public let plans: [Plan]? | ||
|
|
||
| public struct Country: Decodable { | ||
| public var code: String? | ||
| public var name: String? | ||
| } | ||
|
|
||
| public struct Plan: Decodable { | ||
| public let isGift: Bool | ||
| public let giftId: Int? | ||
| public let paidSubscriptionId: String? | ||
| public let status: String | ||
| public let title: String | ||
| public let currency: String? | ||
| public let renewInterval: String? | ||
| public let inactiveRenewInterval: String? | ||
| public let renewalPrice: Decimal | ||
| public let startDate: Date | ||
| public let endDate: Date | ||
|
|
||
| public init(from decoder: Decoder) throws { | ||
| let container = try decoder.container(keyedBy: StringCodingKey.self) | ||
| isGift = try container.decode(Bool.self, forKey: "is_gift") | ||
| giftId = try container.decodeIfPresent(Int.self, forKey: "gift_id") | ||
| paidSubscriptionId = try container.decodeIfPresent(String.self, forKey: "paid_subscription_id") | ||
| status = try container.decode(String.self, forKey: "status") | ||
| title = try container.decode(String.self, forKey: "title") | ||
| currency = try container.decodeIfPresent(String.self, forKey: "currency") | ||
| renewInterval = try? container.decodeIfPresent(String.self, forKey: "renew_interval") | ||
| inactiveRenewInterval = try? container.decodeIfPresent(String.self, forKey: "inactive_renew_interval") | ||
| renewalPrice = try container.decode(Decimal.self, forKey: "renewal_price") | ||
| startDate = try container.decode(Date.self, forKey: "start_date") | ||
| endDate = try container.decode(Date.self, forKey: "end_date") | ||
| } | ||
| } | ||
|
|
||
| public init(from decoder: any Decoder) throws { | ||
| let container = try decoder.container(keyedBy: StringCodingKey.self) | ||
| subscriberID = try container.decode(Int.self, forKey: "subscription_id") | ||
| dotComUserID = try container.decode(Int.self, forKey: "user_id") | ||
| displayName = try? container.decodeIfPresent(String.self, forKey: "display_name") | ||
| avatar = try? container.decodeIfPresent(String.self, forKey: "avatar") | ||
| emailAddress = try? container.decodeIfPresent(String.self, forKey: "email_address") | ||
| siteURL = try? container.decodeIfPresent(String.self, forKey: "url") | ||
| dateSubscribed = try container.decode(Date.self, forKey: "date_subscribed") | ||
| isEmailSubscriptionEnabled = try container.decode(Bool.self, forKey: "is_email_subscriber") | ||
| subscriptionStatus = try? container.decodeIfPresent(String.self, forKey: "subscription_status") | ||
| country = try? container.decodeIfPresent(Country.self, forKey: "country") | ||
| plans = try container.decodeIfPresent([Plan].self, forKey: "plans") | ||
| } | ||
| } | ||
|
|
||
| /// Gets stats for the given subscriber. | ||
| /// | ||
| /// Example: https://public-api.wordpress.com/wpcom/v2/sites/239619264/subscribers/individual?subscription_id=907116368 | ||
| public func getSubsciberDetails( | ||
| siteID: Int, | ||
| subscriberID: Int, | ||
| type: String = "email" | ||
| ) async throws -> GetSubscriberDetailsResponse { | ||
| let url = self.path(forEndpoint: "sites/\(siteID)/subscribers/individual", withVersion: ._2_0) | ||
| let query: [String: Any] = [ | ||
| "subscription_id": subscriberID, | ||
| "type": type | ||
| ] | ||
|
|
||
| let decoder = JSONDecoder() | ||
| decoder.dateDecodingStrategy = JSONDecoder.DateDecodingStrategy.supportMultipleDateFormats | ||
|
|
||
| return try await wordPressComRestApi.perform( | ||
| .get, | ||
| URLString: url, | ||
| parameters: query, | ||
| jsonDecoder: decoder, | ||
| type: GetSubscriberDetailsResponse.self | ||
| ).get().body | ||
| } | ||
|
|
||
| public struct GetSubscriberStatsResponse: Decodable { | ||
| public var emailsSent: Int | ||
| public var uniqueOpens: Int | ||
| public var uniqueClicks: Int | ||
| } | ||
|
|
||
| /// Gets stats for the given subscriber. | ||
| /// | ||
| /// Example: https://public-api.wordpress.com/wpcom/v2/sites/239619264/individual-subscriber-stats?subscription_id=907116368 | ||
| public func getSubsciberStats( | ||
| siteID: Int, | ||
| subscriberID: Int | ||
| ) async throws -> GetSubscriberStatsResponse { | ||
| let url = self.path(forEndpoint: "sites/\(siteID)/individual-subscriber-stats", withVersion: ._2_0) | ||
| let query: [String: Any] = [ | ||
| "subscription_id": subscriberID | ||
| ] | ||
| return try await wordPressComRestApi.perform( | ||
| .get, | ||
| URLString: url, | ||
| parameters: query, | ||
| jsonDecoder: JSONDecoder.apiDecoder, | ||
| type: GetSubscriberStatsResponse.self | ||
| ).get().body | ||
| } | ||
| } | ||
|
|
||
| extension SubscribersServiceRemote.SubsciberBasicInfoResponse { | ||
| public var avatarURL: URL? { | ||
| avatar.flatMap(URL.init) | ||
| } | ||
|
|
||
| public var isDotComUser: Bool { | ||
| dotComUserID > 0 | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if I missed anything, but it looks like this part can be simplified by having a
CodingKey-conformingenum, and let Swift synthesis theDecodableimplementation for us?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I started with it, until I discovered that a bunch of these
Stringfields can containfalsewhen empty, e.g.country: false. So I replaced parsing for these withtry? container.decodeIfPresent(String.self. I'm not sure if there is a generalized solution in the framework – couldn't find it after a brief search.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right. Yeah, that caused some headaches in wordpress-rs too, which has been solved. We can potentially start using the WP.com API client in wordpress-rs, once that's implemented.