This PR contains all the work related to setting up this project as required to implement the [Assignment](https://repo.rock-n-code.com/rock-n-code/deep-linking-assignment/wiki/Assignment) on top, as intended. To summarise this work: - [x] created a new **Xcode** project; - [x] cloned the `Wikipedia` app and inserted it into the **Xcode** project; - [x] created the `Locations` app and also, its `Libraries` package; - [x] created the `Shared` package to share dependencies between the apps; - [x] added a `Makefile` file and implemented some **environment** and **help** commands. Co-authored-by: Javier Cicchelli <javier@rock-n-code.com> Reviewed-on: rock-n-code/deep-linking-assignment#1
58 lines
2.2 KiB
Swift
58 lines
2.2 KiB
Swift
import Foundation
|
|
|
|
@objc(WMFRelatedSearchFetcher)
|
|
final class RelatedSearchFetcher: Fetcher {
|
|
private struct RelatedPages: Decodable {
|
|
let pages: [ArticleSummary]?
|
|
}
|
|
|
|
@objc func fetchRelatedArticles(forArticleWithURL articleURL: URL?, completion: @escaping (Error?, [WMFInMemoryURLKey: ArticleSummary]?) -> Void) {
|
|
guard
|
|
let articleURL = articleURL,
|
|
let articleTitle = articleURL.percentEncodedPageTitleForPathComponents
|
|
else {
|
|
completion(Fetcher.invalidParametersError, nil)
|
|
return
|
|
}
|
|
|
|
let pathComponents = ["page", "related", articleTitle]
|
|
guard let taskURL = configuration.pageContentServiceAPIURLForURL(articleURL, appending: pathComponents) else {
|
|
completion(Fetcher.invalidParametersError, nil)
|
|
return
|
|
}
|
|
session.jsonDecodableTask(with: taskURL) { (relatedPages: RelatedPages?, response, error) in
|
|
if let error = error {
|
|
completion(error, nil)
|
|
return
|
|
}
|
|
|
|
guard let response = response as? HTTPURLResponse else {
|
|
completion(Fetcher.unexpectedResponseError, nil)
|
|
return
|
|
}
|
|
|
|
guard response.statusCode == 200 else {
|
|
let error = response.statusCode == 302 ? Fetcher.noNewDataError : Fetcher.unexpectedResponseError
|
|
completion(error, nil)
|
|
return
|
|
}
|
|
|
|
|
|
guard let summaries = relatedPages?.pages, summaries.count > 0 else {
|
|
completion(Fetcher.unexpectedResponseError, nil)
|
|
return
|
|
}
|
|
|
|
let summaryKeysWithValues: [(WMFInMemoryURLKey, ArticleSummary)] = summaries.compactMap { (summary) -> (WMFInMemoryURLKey, ArticleSummary)? in
|
|
summary.languageVariantCode = articleURL.wmf_languageVariantCode
|
|
guard let articleKey = summary.key else {
|
|
return nil
|
|
}
|
|
return (articleKey, summary)
|
|
}
|
|
|
|
completion(nil, Dictionary(uniqueKeysWithValues: summaryKeysWithValues))
|
|
}
|
|
}
|
|
}
|