Skip to content

Commit c07fa39

Browse files
authored
Merge pull request #8546 from woocommerce/feat/8544-migrate-leaderboard
REST API: Migrate leaderboards endpoint
2 parents 1e55456 + c76f163 commit c07fa39

File tree

5 files changed

+126
-3
lines changed

5 files changed

+126
-3
lines changed

Networking/Networking.xcodeproj/project.pbxproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -722,6 +722,8 @@
722722
DEC51AFB2769C66B009F3DF4 /* SystemStatusMapperTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = DEC51AFA2769C66B009F3DF4 /* SystemStatusMapperTests.swift */; };
723723
DEC51B02276AFB35009F3DF4 /* SystemStatus+DropinMustUsePlugin.swift in Sources */ = {isa = PBXBuildFile; fileRef = DEC51B01276AFB34009F3DF4 /* SystemStatus+DropinMustUsePlugin.swift */; };
724724
DEF13C5029629EEA0024A02B /* user-complete-without-data.json in Resources */ = {isa = PBXBuildFile; fileRef = DEF13C4F29629EEA0024A02B /* user-complete-without-data.json */; };
725+
DEF13C562965689F0024A02B /* LeaderboardListMapperTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = DEF13C552965689F0024A02B /* LeaderboardListMapperTests.swift */; };
726+
DEF13C5A296571150024A02B /* leaderboards-year-without-data.json in Resources */ = {isa = PBXBuildFile; fileRef = DEF13C59296571150024A02B /* leaderboards-year-without-data.json */; };
725727
DEFBA74E29485A7600C35BA9 /* RESTRequest.swift in Sources */ = {isa = PBXBuildFile; fileRef = DEFBA74D29485A7600C35BA9 /* RESTRequest.swift */; };
726728
DEFBA7542949CE6600C35BA9 /* RequestProcessor.swift in Sources */ = {isa = PBXBuildFile; fileRef = DEFBA7532949CE6600C35BA9 /* RequestProcessor.swift */; };
727729
DEFBA7562949D17400C35BA9 /* DefaultRequestAuthenticatorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = DEFBA7552949D17300C35BA9 /* DefaultRequestAuthenticatorTests.swift */; };
@@ -1515,6 +1517,8 @@
15151517
DEC51AFA2769C66B009F3DF4 /* SystemStatusMapperTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SystemStatusMapperTests.swift; sourceTree = "<group>"; };
15161518
DEC51B01276AFB34009F3DF4 /* SystemStatus+DropinMustUsePlugin.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "SystemStatus+DropinMustUsePlugin.swift"; sourceTree = "<group>"; };
15171519
DEF13C4F29629EEA0024A02B /* user-complete-without-data.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = "user-complete-without-data.json"; sourceTree = "<group>"; };
1520+
DEF13C552965689F0024A02B /* LeaderboardListMapperTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LeaderboardListMapperTests.swift; sourceTree = "<group>"; };
1521+
DEF13C59296571150024A02B /* leaderboards-year-without-data.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = "leaderboards-year-without-data.json"; sourceTree = "<group>"; };
15181522
DEFBA74D29485A7600C35BA9 /* RESTRequest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RESTRequest.swift; sourceTree = "<group>"; };
15191523
DEFBA7532949CE6600C35BA9 /* RequestProcessor.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RequestProcessor.swift; sourceTree = "<group>"; };
15201524
DEFBA7552949D17300C35BA9 /* DefaultRequestAuthenticatorTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DefaultRequestAuthenticatorTests.swift; sourceTree = "<group>"; };
@@ -2167,6 +2171,7 @@
21672171
E16C59B828F927CA007D55BB /* iap-order-create.json */,
21682172
26B2F74624C55A6E0065CCC8 /* leaderboards-year.json */,
21692173
268B68FC24C87E37007EBF1D /* leaderboards-year-alt.json */,
2174+
DEF13C59296571150024A02B /* leaderboards-year-without-data.json */,
21702175
02EF1671292F0D1900D90AD6 /* load-plan-success.json */,
21712176
B505F6D420BEE4E600BB1B69 /* me.json */,
21722177
93D8BBFE226BC1DA00AD2EB3 /* me-settings.json */,
@@ -2521,6 +2526,7 @@
25212526
isa = PBXGroup;
25222527
children = (
25232528
B505F6D220BEE3A500BB1B69 /* AccountMapperTests.swift */,
2529+
DEF13C552965689F0024A02B /* LeaderboardListMapperTests.swift */,
25242530
DE2E8EAA2954170D002E4B14 /* WordPressSiteMapperTests.swift */,
25252531
9387A6EF226E3F15001B53D7 /* AccountSettingsMapperTests.swift */,
25262532
077F39DB26A58F4800ABEADC /* SystemPluginMapperTests.swift */,
@@ -2938,6 +2944,7 @@
29382944
265BCA02243056E3004E53EE /* categories-all.json in Resources */,
29392945
D8FBFF2422D52815006E3336 /* order-stats-v4-daily.json in Resources */,
29402946
CEC4BF91234E40B5008D9195 /* refund-single.json in Resources */,
2947+
DEF13C5A296571150024A02B /* leaderboards-year-without-data.json in Resources */,
29412948
2683D70E24456DB8002A1589 /* categories-empty.json in Resources */,
29422949
D865CE67278CA225002C8520 /* stripe-payment-intent-succeeded.json in Resources */,
29432950
DE74F2A027E3137F0002FE59 /* setting-analytics.json in Resources */,
@@ -3525,6 +3532,7 @@
35253532
isa = PBXSourcesBuildPhase;
35263533
buildActionMask = 2147483647;
35273534
files = (
3535+
DEF13C562965689F0024A02B /* LeaderboardListMapperTests.swift in Sources */,
35283536
45551F142523E7FF007EF104 /* UserAgentTests.swift in Sources */,
35293537
03EB998A2906AB0C00F06A39 /* JustInTimeMessagesRemoteTests.swift in Sources */,
35303538
DE34051D28BDF1C900CF0D97 /* JetpackConnectionURLMapperTests.swift in Sources */,

Networking/Networking/Mapper/LeaderboardListMapper.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ struct LeaderboardListMapper: Mapper {
88
///
99
func map(response: Data) throws -> [Leaderboard] {
1010
let decoder = JSONDecoder()
11-
return try decoder.decode(LeaderboardsEnvelope.self, from: response).data
11+
do {
12+
return try decoder.decode(LeaderboardsEnvelope.self, from: response).data
13+
} catch {
14+
return try decoder.decode([Leaderboard].self, from: response)
15+
}
1216
}
1317
}
1418

Networking/Networking/Remote/LeaderboardsRemote.swift

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,12 @@ public class LeaderboardsRemote: Remote {
3030
ParameterKeys.forceRefresh: forceRefresh
3131
]
3232

33-
let request = JetpackRequest(wooApiVersion: .wcAnalytics, method: .get, siteID: siteID, path: Constants.path, parameters: parameters)
33+
let request = JetpackRequest(wooApiVersion: .wcAnalytics,
34+
method: .get,
35+
siteID: siteID,
36+
path: Constants.path,
37+
parameters: parameters,
38+
availableAsRESTRequest: true)
3439
let mapper = LeaderboardListMapper()
3540
enqueue(request, mapper: mapper, completion: completion)
3641
}
@@ -62,7 +67,12 @@ public class LeaderboardsRemote: Remote {
6267
ParameterKeys.forceRefresh: forceRefresh
6368
]
6469

65-
let request = JetpackRequest(wooApiVersion: .wcAnalytics, method: .get, siteID: siteID, path: Constants.pathDeprecated, parameters: parameters)
70+
let request = JetpackRequest(wooApiVersion: .wcAnalytics,
71+
method: .get,
72+
siteID: siteID,
73+
path: Constants.pathDeprecated,
74+
parameters: parameters,
75+
availableAsRESTRequest: true)
6676
let mapper = LeaderboardListMapper()
6777
enqueue(request, mapper: mapper, completion: completion)
6878
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import XCTest
2+
@testable import Networking
3+
4+
final class LeaderboardListMapperTests: XCTestCase {
5+
6+
/// Verifies that leaderboard list is parsed.
7+
///
8+
func test_mapper_parses_leaderboard_list_in_response_with_data_envelope() throws {
9+
// Given
10+
let list = try mapLeaderboardListResponse()
11+
12+
// Then
13+
XCTAssertFalse(list.isEmpty)
14+
}
15+
16+
/// Verifies that the leaderboard list is parsed when the response has no data envelope.
17+
///
18+
func test_mapper_parses_leaderboard_list_in_response_without_data_envelope() throws {
19+
// Given
20+
let list = try mapLeaderboardListResponseWithoutDataEnvelope()
21+
22+
// Then
23+
XCTAssertFalse(list.isEmpty)
24+
}
25+
}
26+
27+
// MARK: - Test Helpers
28+
///
29+
private extension LeaderboardListMapperTests {
30+
31+
/// Returns the LeaderboardListMapper output upon receiving `filename` (Data Encoded)
32+
///
33+
func mapLeaderboardList(from filename: String) throws -> [Leaderboard] {
34+
guard let response = Loader.contentsOf(filename) else {
35+
throw FileNotFoundError()
36+
}
37+
38+
return try LeaderboardListMapper().map(response: response)
39+
}
40+
41+
/// Returns the LeaderboardListMapper output from `products.json`
42+
///
43+
func mapLeaderboardListResponse() throws -> [Leaderboard] {
44+
return try mapLeaderboardList(from: "leaderboards-year")
45+
}
46+
47+
/// Returns the LeaderboardListMapper output from `leaderboards-products-without-data.json`
48+
///
49+
func mapLeaderboardListResponseWithoutDataEnvelope() throws -> [Leaderboard] {
50+
return try mapLeaderboardList(from: "leaderboards-year-without-data")
51+
}
52+
53+
struct FileNotFoundError: Error {}
54+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
[
2+
{
3+
"id": "products",
4+
"label": "Top Products - Items Sold",
5+
"headers": [
6+
{
7+
"label": "Product"
8+
},
9+
{
10+
"label": "Items Sold"
11+
},
12+
{
13+
"label": "Net Sales"
14+
}
15+
],
16+
"rows": [
17+
[
18+
{
19+
"display": "<a href='https:\/\/test.com\/wp-admin\/admin.php?page=wc-admin&path=\/analytics\/products&filter=single_product&products=9'>Aljafor<\/a>",
20+
"value": "Aljafor"
21+
},
22+
{
23+
"display": "4",
24+
"value": 4
25+
},
26+
{
27+
"display": "<span class=\"woocommerce-Price-amount amount\"><span class=\"woocommerce-Price-currencySymbol\">&#36;<\/span>20,000.00<\/span>",
28+
"value": 20000
29+
}
30+
],
31+
[
32+
{
33+
"display": "<a href='https:\/\/test.com\/wp-admin\/admin.php?page=wc-admin&path=\/analytics\/products&filter=single_product&products=29'>Album<\/a>",
34+
"value": "Album"
35+
},
36+
{
37+
"display": "1",
38+
"value": 1
39+
},
40+
{
41+
"display": "<span class=\"woocommerce-Price-amount amount\"><span class=\"woocommerce-Price-currencySymbol\">&#36;<\/span>15.00<\/span>",
42+
"value": 15.99
43+
}
44+
]
45+
]
46+
}
47+
]

0 commit comments

Comments
 (0)