Skip to content

Commit ed6470e

Browse files
authored
Merge pull request #8584 from woocommerce/feat/8583
REST API: Migrate the data remote for the country list
2 parents b90ca32 + dd154de commit ed6470e

File tree

5 files changed

+378
-3
lines changed

5 files changed

+378
-3
lines changed

Networking/Networking.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -696,6 +696,7 @@
696696
DE42F9632967C8B900D514C2 /* ReportOrderTotalsMapperTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = DE42F9622967C8B900D514C2 /* ReportOrderTotalsMapperTests.swift */; };
697697
DE42F9652967F34400D514C2 /* refund-single-without-data.json in Resources */ = {isa = PBXBuildFile; fileRef = DE42F9642967F34400D514C2 /* refund-single-without-data.json */; };
698698
DE42F9672967F61D00D514C2 /* refunds-all-without-data.json in Resources */ = {isa = PBXBuildFile; fileRef = DE42F9662967F61D00D514C2 /* refunds-all-without-data.json */; };
699+
DE42F96F296BC9A700D514C2 /* countries-without-data.json in Resources */ = {isa = PBXBuildFile; fileRef = DE42F96E296BC9A700D514C2 /* countries-without-data.json */; };
699700
DE50295928C5BD0200551736 /* JetpackUser.swift in Sources */ = {isa = PBXBuildFile; fileRef = DE50295828C5BD0200551736 /* JetpackUser.swift */; };
700701
DE50295B28C5F99700551736 /* DotcomUser.swift in Sources */ = {isa = PBXBuildFile; fileRef = DE50295A28C5F99700551736 /* DotcomUser.swift */; };
701702
DE50295D28C6068B00551736 /* JetpackUserMapper.swift in Sources */ = {isa = PBXBuildFile; fileRef = DE50295C28C6068B00551736 /* JetpackUserMapper.swift */; };
@@ -1507,6 +1508,7 @@
15071508
DE42F9622967C8B900D514C2 /* ReportOrderTotalsMapperTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ReportOrderTotalsMapperTests.swift; sourceTree = "<group>"; };
15081509
DE42F9642967F34400D514C2 /* refund-single-without-data.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = "refund-single-without-data.json"; sourceTree = "<group>"; };
15091510
DE42F9662967F61D00D514C2 /* refunds-all-without-data.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = "refunds-all-without-data.json"; sourceTree = "<group>"; };
1511+
DE42F96E296BC9A700D514C2 /* countries-without-data.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = "countries-without-data.json"; sourceTree = "<group>"; };
15101512
DE50295828C5BD0200551736 /* JetpackUser.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = JetpackUser.swift; sourceTree = "<group>"; };
15111513
DE50295A28C5F99700551736 /* DotcomUser.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DotcomUser.swift; sourceTree = "<group>"; };
15121514
DE50295C28C6068B00551736 /* JetpackUserMapper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = JetpackUserMapper.swift; sourceTree = "<group>"; };
@@ -2127,6 +2129,7 @@
21272129
B559EBA820A0B5B100836CD4 /* Responses */ = {
21282130
isa = PBXGroup;
21292131
children = (
2132+
DE42F96E296BC9A700D514C2 /* countries-without-data.json */,
21302133
DE42F9662967F61D00D514C2 /* refunds-all-without-data.json */,
21312134
DE42F9642967F34400D514C2 /* refund-single-without-data.json */,
21322135
EE80A24B29556F1D003591E4 /* Coupon */,
@@ -2947,6 +2950,7 @@
29472950
DEC51A9B274E3206009F3DF4 /* plugin-inactive.json in Resources */,
29482951
CCF48B382628AEAE0034EA83 /* shipping-label-account-settings-no-payment-methods.json in Resources */,
29492952
31A451D327863A2E00FE81AA /* stripe-account-complete.json in Resources */,
2953+
DE42F96F296BC9A700D514C2 /* countries-without-data.json in Resources */,
29502954
EECB7EE8286555180028C888 /* media-update-product-id.json in Resources */,
29512955
D88D5A43230BC668007B6E01 /* reviews-single.json in Resources */,
29522956
DE5CA111288A3E080077BEF9 /* product-malformed-variations-and-image-alt.json in Resources */,

Networking/Networking/Mapper/CountryListMapper.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ struct CountryListMapper: Mapper {
88
/// (Attempts) to convert an instance of Data into an array of Country Entities.
99
///
1010
func map(response: Data) throws -> [Country] {
11-
return try JSONDecoder().decode(CountryListEnvelope.self, from: response).data
11+
do {
12+
return try JSONDecoder().decode(CountryListEnvelope.self, from: response).data
13+
} catch {
14+
return try JSONDecoder().decode([Country].self, from: response)
15+
}
1216
}
1317
}
1418

Networking/Networking/Remote/DataRemote.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ public final class DataRemote: Remote {
88
/// - siteID: Remote ID of the site that owns the countries.
99
/// - completion: Closure to be executed upon completion.
1010
public func loadCountries(siteID: Int64, completion: @escaping (Result<[Country], Error>) -> Void) {
11-
let request = JetpackRequest(wooApiVersion: .mark3, method: .get, siteID: siteID, path: Path.countries)
11+
let request = JetpackRequest(wooApiVersion: .mark3,
12+
method: .get,
13+
siteID: siteID,
14+
path: Path.countries,
15+
availableAsRESTRequest: true)
1216
let mapper = CountryListMapper()
1317
enqueue(request, mapper: mapper, completion: completion)
1418
}

Networking/NetworkingTests/Mapper/CountryListMapperTests.swift

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,20 @@ class CountryListMapperTests: XCTestCase {
2121
XCTAssertEqual(countries[1].states.count, 18)
2222
XCTAssertEqual(countries[1].states.first, StateOfACountry(code: "PY-ASU", name: "Asunción"))
2323
}
24+
25+
func test_countries_are_properly_parsed_if_the_response_has_no_data_envelope() {
26+
guard let countries = mapCountriesResponseWithoutDataEnvelope() else {
27+
XCTFail()
28+
return
29+
}
30+
31+
XCTAssertEqual(countries.count, 3)
32+
XCTAssertEqual(countries.first?.states.isEmpty, true)
33+
XCTAssertEqual(countries[1].code, "PY")
34+
XCTAssertEqual(countries[1].name, "Paraguay")
35+
XCTAssertEqual(countries[1].states.count, 18)
36+
XCTAssertEqual(countries[1].states.first, StateOfACountry(code: "PY-ASU", name: "Asunción"))
37+
}
2438
}
2539

2640
/// Private Helpers
@@ -37,9 +51,15 @@ private extension CountryListMapperTests {
3751
return try! CountryListMapper().map(response: response)
3852
}
3953

40-
/// Returns the CountryListMapper output upon receiving `countries`
54+
/// Returns the [Country] output upon receiving `countries`
4155
///
4256
func mapCountriesResponse() -> [Country]? {
4357
return mapCountries(from: "countries")
4458
}
59+
60+
/// Returns the [Country] output upon receiving `countries-without-data`
61+
///
62+
func mapCountriesResponseWithoutDataEnvelope() -> [Country]? {
63+
return mapCountries(from: "countries-without-data")
64+
}
4565
}

0 commit comments

Comments
 (0)