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

Commit bee91b4

Browse files
authored
Make deviceId optional in DashboardServiceRemote` method (#676)
This is to ensure backwards compatibility
2 parents b3f35a9 + eab6275 commit bee91b4

File tree

4 files changed

+42
-7
lines changed

4 files changed

+42
-7
lines changed

CHANGELOG.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@ _None._
3434

3535
### Breaking Changes
3636

37-
- Add `deviceId` param to `DashboardServiceRemote.fetch` method. [#674]
37+
_None._
3838

3939
### New Features
4040

41-
_None._
41+
- Add `deviceId` param to `DashboardServiceRemote.fetch` method. [#674]
4242

4343
### Bug Fixes
4444

WordPressKit/DashboardServiceRemote.swift

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ open class DashboardServiceRemote: ServiceRemoteWordPressComREST {
44
open func fetch(
55
cards: [String],
66
forBlogID blogID: Int,
7-
deviceId: String,
7+
deviceId: String? = nil,
88
success: @escaping (NSDictionary) -> Void,
99
failure: @escaping (Error) -> Void
1010
) {
@@ -32,11 +32,16 @@ open class DashboardServiceRemote: ServiceRemoteWordPressComREST {
3232
})
3333
}
3434

35-
private func makeQueryParams(cards: [String], deviceId: String) throws -> [String: AnyObject] {
35+
private func makeQueryParams(cards: [String], deviceId: String?) throws -> [String: AnyObject] {
3636
let cardsParams: [String: AnyObject] = [
3737
"cards": cards.joined(separator: ",") as NSString
3838
]
39-
let featureFlagParams = try SessionDetails(deviceId: deviceId).dictionaryRepresentation()
39+
let featureFlagParams: [String: AnyObject]? = try {
40+
guard let deviceId else {
41+
return nil
42+
}
43+
return try SessionDetails(deviceId: deviceId).dictionaryRepresentation()
44+
}()
4045
return cardsParams.merging(featureFlagParams ?? [:]) { first, second in
4146
return first
4247
}

WordPressKitTests/DashboardServiceRemoteTests.swift

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,36 @@ class DashboardServiceRemoteTests: RemoteTestCase, RESTTestable {
4949
waitForExpectations(timeout: timeout, handler: nil)
5050
}
5151

52+
// Validates the request's path and query items when the `deviceId` param is `nil`.
53+
//
54+
func testRequestCardsParamWithoutDeviceId() {
55+
let expect = expectation(description: "Dashboard endpoint should contain query params")
56+
let expectedPath = "/wpcom/v2/sites/165243437/dashboard/cards-data"
57+
let expectedQueryParams: Set<String> = ["cards", "locale"]
58+
59+
stubRemoteResponse({ req in
60+
let url = req.url?.absoluteString ?? ""
61+
let containsQueryParams = self.queryParams(expectedQueryParams, containedInRequest: req)
62+
let matchesPath = isPath(expectedPath)(req)
63+
XCTAssertTrue(matchesPath, "The URL '\(url)' doesn't match the expected path.")
64+
XCTAssertTrue(containsQueryParams, "The URL '\(url)' doesn't contain the expected query params.")
65+
return containsQueryParams && matchesPath
66+
}, filename: "dashboard-200-with-drafts-and-scheduled-posts.json", contentType: .ApplicationJSON)
67+
68+
dashboardServiceRemote.fetch(
69+
cards: ["posts", "todays_stats"],
70+
forBlogID: 165243437,
71+
deviceId: nil
72+
) { _ in
73+
expect.fulfill()
74+
} failure: { error in
75+
XCTFail("Dashboard cards request failed: \(error.localizedDescription)")
76+
expect.fulfill()
77+
}
78+
79+
waitForExpectations(timeout: timeout, handler: nil)
80+
}
81+
5282
// Return the cards when the request succeeds
5383
//
5484
func testRequestCards() {

WordPressKitTests/RemoteTestCase.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ extension RemoteTestCase {
197197
guard let url = request.url else {
198198
return false
199199
}
200-
return queryParamsContained(queryParams, containedInURL: url)
200+
return self.queryParams(queryParams, containedInURL: url)
201201
}
202202

203203
/// Checks if the specified set of query parameter names are all present in a given `URL`.
@@ -207,7 +207,7 @@ extension RemoteTestCase {
207207
/// - queryParams: A set of query parameter names to check for in the URL.
208208
/// - url: The `URL` to inspect for the presence of query parameter names.
209209
/// - Returns: A Boolean value indicating whether all specified query parameter names are present in the URL's query string.
210-
func queryParamsContained(_ queryParams: Set<String>, containedInURL url: URL) -> Bool {
210+
func queryParams(_ queryParams: Set<String>, containedInURL url: URL) -> Bool {
211211
guard let components = URLComponents(url: url, resolvingAgainstBaseURL: true),
212212
let queryItems = components.queryItems?.map({ $0.name })
213213
else {

0 commit comments

Comments
 (0)