NetNewsWire/Shared/Article Extractor/ArticleExtractor.swift

105 lines
2.7 KiB
Swift
Raw Normal View History

2019-09-19 01:15:55 +02:00
//
// ArticleExtractor.swift
// NetNewsWire
//
// Created by Maurice Parker on 9/18/19.
// Copyright © 2019 Ranchero Software. All rights reserved.
//
import Foundation
public enum ArticleExtractorState {
case ready
case processing
case failedToParse
case complete
case cancelled
2019-09-19 01:15:55 +02:00
}
protocol ArticleExtractorDelegate {
func articleExtractionDidFail(with: Error)
func articleExtractionDidComplete(extractedArticle: ExtractedArticle)
}
class ArticleExtractor {
private var dataTask: URLSessionDataTask? = nil
2019-09-19 01:15:55 +02:00
var state: ArticleExtractorState!
var article: ExtractedArticle?
var delegate: ArticleExtractorDelegate?
var articleLink: String?
private var url: URL!
public init?(_ articleLink: String) {
self.articleLink = articleLink
2019-09-24 15:17:02 +02:00
let clientURL = ArticleExtractorConfig.clientURL
let username = ArticleExtractorConfig.clientId
let signiture = articleLink.hmacUsingSHA1(key: ArticleExtractorConfig.clientSecret)
2019-09-19 01:15:55 +02:00
if let base64URL = articleLink.data(using: .utf8)?.base64EncodedString() {
let fullURL = "\(clientURL)/\(username)/\(signiture)?base64_url=\(base64URL)"
if let url = URL(string: fullURL) {
self.url = url
return
}
}
return nil
}
public func process() {
state = .processing
dataTask = URLSession.shared.dataTask(with: url) { [weak self] data, response, error in
2019-09-19 01:15:55 +02:00
guard let self = self else { return }
if let error = error {
self.state = .failedToParse
DispatchQueue.main.async {
self.delegate?.articleExtractionDidFail(with: error)
}
return
}
guard let data = data else {
self.state = .failedToParse
DispatchQueue.main.async {
self.delegate?.articleExtractionDidFail(with: URLError(.cannotDecodeContentData))
2019-09-19 01:15:55 +02:00
}
return
}
do {
let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .iso8601
self.article = try decoder.decode(ExtractedArticle.self, from: data)
self.state = .complete
DispatchQueue.main.async {
self.delegate?.articleExtractionDidComplete(extractedArticle: self.article!)
}
} catch {
self.state = .failedToParse
DispatchQueue.main.async {
self.delegate?.articleExtractionDidFail(with: error)
}
}
}
dataTask!.resume()
2019-09-19 01:15:55 +02:00
}
public func cancel() {
state = .cancelled
dataTask?.cancel()
}
2019-09-19 01:15:55 +02:00
}