Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Networking/Networking.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -696,6 +696,7 @@
DE42F9632967C8B900D514C2 /* ReportOrderTotalsMapperTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = DE42F9622967C8B900D514C2 /* ReportOrderTotalsMapperTests.swift */; };
DE42F9652967F34400D514C2 /* refund-single-without-data.json in Resources */ = {isa = PBXBuildFile; fileRef = DE42F9642967F34400D514C2 /* refund-single-without-data.json */; };
DE42F9672967F61D00D514C2 /* refunds-all-without-data.json in Resources */ = {isa = PBXBuildFile; fileRef = DE42F9662967F61D00D514C2 /* refunds-all-without-data.json */; };
DE42F96F296BC9A700D514C2 /* countries-without-data.json in Resources */ = {isa = PBXBuildFile; fileRef = DE42F96E296BC9A700D514C2 /* countries-without-data.json */; };
DE50295928C5BD0200551736 /* JetpackUser.swift in Sources */ = {isa = PBXBuildFile; fileRef = DE50295828C5BD0200551736 /* JetpackUser.swift */; };
DE50295B28C5F99700551736 /* DotcomUser.swift in Sources */ = {isa = PBXBuildFile; fileRef = DE50295A28C5F99700551736 /* DotcomUser.swift */; };
DE50295D28C6068B00551736 /* JetpackUserMapper.swift in Sources */ = {isa = PBXBuildFile; fileRef = DE50295C28C6068B00551736 /* JetpackUserMapper.swift */; };
Expand Down Expand Up @@ -1507,6 +1508,7 @@
DE42F9622967C8B900D514C2 /* ReportOrderTotalsMapperTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ReportOrderTotalsMapperTests.swift; sourceTree = "<group>"; };
DE42F9642967F34400D514C2 /* refund-single-without-data.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = "refund-single-without-data.json"; sourceTree = "<group>"; };
DE42F9662967F61D00D514C2 /* refunds-all-without-data.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = "refunds-all-without-data.json"; sourceTree = "<group>"; };
DE42F96E296BC9A700D514C2 /* countries-without-data.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = "countries-without-data.json"; sourceTree = "<group>"; };
DE50295828C5BD0200551736 /* JetpackUser.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = JetpackUser.swift; sourceTree = "<group>"; };
DE50295A28C5F99700551736 /* DotcomUser.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DotcomUser.swift; sourceTree = "<group>"; };
DE50295C28C6068B00551736 /* JetpackUserMapper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = JetpackUserMapper.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -2127,6 +2129,7 @@
B559EBA820A0B5B100836CD4 /* Responses */ = {
isa = PBXGroup;
children = (
DE42F96E296BC9A700D514C2 /* countries-without-data.json */,
DE42F9662967F61D00D514C2 /* refunds-all-without-data.json */,
DE42F9642967F34400D514C2 /* refund-single-without-data.json */,
EE80A24B29556F1D003591E4 /* Coupon */,
Expand Down Expand Up @@ -2947,6 +2950,7 @@
DEC51A9B274E3206009F3DF4 /* plugin-inactive.json in Resources */,
CCF48B382628AEAE0034EA83 /* shipping-label-account-settings-no-payment-methods.json in Resources */,
31A451D327863A2E00FE81AA /* stripe-account-complete.json in Resources */,
DE42F96F296BC9A700D514C2 /* countries-without-data.json in Resources */,
EECB7EE8286555180028C888 /* media-update-product-id.json in Resources */,
D88D5A43230BC668007B6E01 /* reviews-single.json in Resources */,
DE5CA111288A3E080077BEF9 /* product-malformed-variations-and-image-alt.json in Resources */,
Expand Down
6 changes: 5 additions & 1 deletion Networking/Networking/Mapper/CountryListMapper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ struct CountryListMapper: Mapper {
/// (Attempts) to convert an instance of Data into an array of Country Entities.
///
func map(response: Data) throws -> [Country] {
return try JSONDecoder().decode(CountryListEnvelope.self, from: response).data
do {
return try JSONDecoder().decode(CountryListEnvelope.self, from: response).data
} catch {
return try JSONDecoder().decode([Country].self, from: response)
}
}
}

Expand Down
6 changes: 5 additions & 1 deletion Networking/Networking/Remote/DataRemote.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ public final class DataRemote: Remote {
/// - siteID: Remote ID of the site that owns the countries.
/// - completion: Closure to be executed upon completion.
public func loadCountries(siteID: Int64, completion: @escaping (Result<[Country], Error>) -> Void) {
let request = JetpackRequest(wooApiVersion: .mark3, method: .get, siteID: siteID, path: Path.countries)
let request = JetpackRequest(wooApiVersion: .mark3,
method: .get,
siteID: siteID,
path: Path.countries,
availableAsRESTRequest: true)
let mapper = CountryListMapper()
enqueue(request, mapper: mapper, completion: completion)
}
Expand Down
22 changes: 21 additions & 1 deletion Networking/NetworkingTests/Mapper/CountryListMapperTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,20 @@ class CountryListMapperTests: XCTestCase {
XCTAssertEqual(countries[1].states.count, 18)
XCTAssertEqual(countries[1].states.first, StateOfACountry(code: "PY-ASU", name: "Asunción"))
}

func test_countries_are_properly_parsed_if_the_response_has_no_data_envelope() {
guard let countries = mapCountriesResponseWithoutDataEnvelope() else {
XCTFail()
return
}

XCTAssertEqual(countries.count, 3)
XCTAssertEqual(countries.first?.states.isEmpty, true)
XCTAssertEqual(countries[1].code, "PY")
XCTAssertEqual(countries[1].name, "Paraguay")
XCTAssertEqual(countries[1].states.count, 18)
XCTAssertEqual(countries[1].states.first, StateOfACountry(code: "PY-ASU", name: "Asunción"))
}
}

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

/// Returns the CountryListMapper output upon receiving `countries`
/// Returns the [Country] output upon receiving `countries`
///
func mapCountriesResponse() -> [Country]? {
return mapCountries(from: "countries")
}

/// Returns the [Country] output upon receiving `countries-without-data`
///
func mapCountriesResponseWithoutDataEnvelope() -> [Country]? {
return mapCountries(from: "countries-without-data")
}
}
Loading