mirror of
https://github.com/mas-cli/mas
synced 2025-02-16 12:38:30 +00:00
♻️ Refactor MasStoreSearch to reduce code duplication
This commit is contained in:
parent
60cc16ab41
commit
877cb62872
1 changed files with 10 additions and 13 deletions
|
@ -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)
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue