@@ -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