Skip to content
This repository was archived by the owner on Sep 15, 2025. It is now read-only.

Commit 392d2d3

Browse files
committed
Add summarize param
1 parent 102dc8d commit 392d2d3

File tree

4 files changed

+41
-22
lines changed

4 files changed

+41
-22
lines changed

Package.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ let package = Package(
1111
targets: [
1212
.binaryTarget(
1313
name: "WordPressKit",
14-
url: "https://github.com/user-attachments/files/21350975/WordPressKit.zip",
15-
checksum: "dc40a4c09af565c16eca2ca0cd110c57ed4e8638ea4baeb2cf1bd124b07d3f8e"
14+
url: "https://github.com/user-attachments/files/21352383/WordPressKit.zip",
15+
checksum: "8054b66ecf39b8c23acea6d8c5c6c9f65fda01fcce4da7acf7db4273cb8e9145"
1616
),
1717
]
1818
)

Sources/WordPressKit/Models/Stats/Time Interval/StatsTopAuthorsTimeIntervalData.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,20 @@ public struct StatsTopPost {
4040
}
4141

4242
public let title: String
43+
public var date: String?
4344
public let postID: Int
4445
public let postURL: URL?
4546
public let viewsCount: Int
4647
public let kind: Kind
4748

4849
public init(title: String,
50+
date: String?,
4951
postID: Int,
5052
postURL: URL?,
5153
viewsCount: Int,
5254
kind: Kind) {
5355
self.title = title
56+
self.date = date
5457
self.postID = postID
5558
self.postURL = postURL
5659
self.viewsCount = viewsCount

Sources/WordPressKit/Models/Stats/Time Interval/StatsTopPostsTimeIntervalData.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ private extension StatsTopPost {
5959
}
6060

6161
self.title = title
62+
self.date = jsonDictionary["date"] as? String
6263
self.postID = postID
6364
self.postURL = URL(string: url)
6465
self.viewsCount = viewsCount

Sources/WordPressKit/Services/StatsServiceRemoteV2.swift

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ open class StatsServiceRemoteV2: ServiceRemoteWordPressComREST {
88

99
public enum ResponseError: Error {
1010
case decodingFailure
11+
case emptySummary
1112
}
1213

1314
public enum MarkAsSpamResponseError: Error {
@@ -106,12 +107,16 @@ open class StatsServiceRemoteV2: ServiceRemoteWordPressComREST {
106107
/// e.g. if you want data spanning 11-17 Feb 2019, you should pass in a period of `.week` and an
107108
/// ending date of `Feb 17 2019`.
108109
/// - limit: Limit of how many objects you want returned for your query. Default is `10`. `0` means no limit.
109-
open func getData<TimeStatsType: StatsTimeIntervalData>(for period: StatsPeriodUnit,
110-
unit: StatsPeriodUnit? = nil,
111-
startDate: Date? = nil,
112-
endingOn: Date,
113-
limit: Int = 10,
114-
completion: @escaping ((TimeStatsType?, Error?) -> Void)) {
110+
open func getData<TimeStatsType: StatsTimeIntervalData>(
111+
for period: StatsPeriodUnit,
112+
unit: StatsPeriodUnit? = nil,
113+
startDate: Date? = nil,
114+
endingOn: Date,
115+
limit: Int = 10,
116+
summarize: Bool? = nil,
117+
parameters: [String: String]? = nil,
118+
completion: @escaping ((TimeStatsType?, Error?) -> Void)
119+
) {
115120
let pathComponent = TimeStatsType.pathComponent
116121
let path = self.path(forEndpoint: "sites/\(siteID)/\(pathComponent)/", withVersion: ._1_1)
117122

@@ -124,6 +129,14 @@ open class StatsServiceRemoteV2: ServiceRemoteWordPressComREST {
124129
if let startDate {
125130
staticProperties["start_date"] = dateFormatter.string(from: startDate) as AnyObject
126131
}
132+
if let summarize {
133+
staticProperties["summarize"] = summarize.description as NSString
134+
}
135+
if let parameters {
136+
for (key, value) in parameters {
137+
staticProperties[key] = value as NSString
138+
}
139+
}
127140

128141
let classProperties = TimeStatsType.queryProperties(with: endingOn, period: unit ?? period, maxCount: limit) as [String: AnyObject]
129142

@@ -147,14 +160,15 @@ open class StatsServiceRemoteV2: ServiceRemoteWordPressComREST {
147160
let parsedUnit = unitString.flatMap { StatsPeriodUnit(string: $0) } ?? unit ?? period
148161
// some responses omit this field! not a reason to fail a whole request parsing though.
149162

150-
guard
151-
let timestats = TimeStatsType(date: date,
152-
period: parsedPeriod,
153-
unit: parsedUnit,
154-
jsonDictionary: jsonResponse)
155-
else {
163+
guard let timestats = TimeStatsType(date: date, period: parsedPeriod, unit: parsedUnit, jsonDictionary: jsonResponse) else {
164+
if summarize == true {
165+
// Some responses return `"summary": null` with no good way to
166+
// process it without refactoring every response, hence this workaround.
167+
completion(nil, ResponseError.emptySummary)
168+
} else {
156169
completion(nil, ResponseError.decodingFailure)
157-
return
170+
}
171+
return
158172
}
159173

160174
completion(timestats, nil)
@@ -397,14 +411,15 @@ extension StatsTimeIntervalData {
397411
// Most of the responses for time data come in a unwieldy format, that requires awkwkard unwrapping
398412
// at the call-site — unfortunately not _all of them_, which means we can't just do it at the request level.
399413
static func unwrapDaysDictionary(jsonDictionary: [String: AnyObject]) -> [String: AnyObject]? {
400-
guard
401-
let days = jsonDictionary["days"] as? [String: AnyObject],
402-
let firstKey = days.keys.first,
403-
let firstDay = days[firstKey] as? [String: AnyObject]
404-
else {
405-
return nil
414+
if let summary = jsonDictionary["summary"] as? [String: AnyObject] {
415+
return summary
416+
}
417+
if let days = jsonDictionary["days"] as? [String: AnyObject],
418+
let firstKey = days.keys.first,
419+
let firstDay = days[firstKey] as? [String: AnyObject] {
420+
return firstDay
406421
}
407-
return firstDay
422+
return nil
408423
}
409424

410425
}

0 commit comments

Comments
 (0)