♻️ Refactor MasStoreSearch to reduce code duplication

This commit is contained in:
Chris Araman 2021-04-16 19:29:05 -07:00
parent 60cc16ab41
commit 877cb62872
No known key found for this signature in database
GPG key ID: BB4499D9E11B61E0

View file

@ -26,13 +26,7 @@ public class MasStoreSearch: StoreSearch {
guard let url = searchURL(for: appName)
else { throw MASError.urlEncoding }
let data = try networkManager.loadDataSync(from: url)
do {
let results = try JSONDecoder().decode(SearchResultList.self, from: data)
return results
} catch {
throw MASError.jsonParsing(error: error as NSError)
}
return try loadSearchResults(url)
}
/// Looks up app details.
@ -44,14 +38,17 @@ public class MasStoreSearch: StoreSearch {
guard let url = lookupURL(forApp: appId)
else { throw MASError.urlEncoding }
let results = try loadSearchResults(url)
guard let searchResult = results.results.first
else { return nil }
return searchResult
}
private func loadSearchResults(_ url: URL) throws -> SearchResultList {
let data = try networkManager.loadDataSync(from: url)
do {
let results = try JSONDecoder().decode(SearchResultList.self, from: data)
guard let searchResult = results.results.first
else { return nil }
return searchResult
return try JSONDecoder().decode(SearchResultList.self, from: data)
} catch {
throw MASError.jsonParsing(error: error as NSError)
}