Код следующий:
Код: Выделить всё
import Foundation
import UIKit
final class HistoryAPICaller {
static let shared = HistoryAPICaller()
struct Constants
{
static let topHeadLinesURL = URL(string:"http://history.muffinlabs.com/date/6/3")
static let searchUrlString = "http://history.muffinlabs.com/date/6/3"
}
private init() {}
public func getTopStories(completion: @escaping (Result) -> Void) {
guard let html = Constants.topHeadLinesURL else {
return
}
let task = URLSession.shared.dataTask(with: html) { data, _, error in
if let error = error {
completion(.failure(error))
}
else if let data = data {
do {
let result = try JSONDecoder().decode(HistoryAPIResponse.self, from: data)
print("Articles: \(result.data.Events.count)")
completion(.success(result.data.Events))
}
catch {
completion(.failure(error))
}
}
}
task.resume()
}
public func search(with query: String, completion: @escaping (Result) -> Void) {
guard !query.trimmingCharacters(in: .whitespaces).isEmpty else {
return
}
let urltring = Constants.searchUrlString + query
guard let html = URL(string:urltring) else {
return
}
let task = URLSession.shared.dataTask(with: html) { data, _, error in
if let error = error {
completion ( .failure(error))
}
else if let data = data {
do {
let result = try JSONDecoder().decode(HistoryAPIResponse.self, from: data)
print("Articles: \(result.data.Events.count)")
completion(.success(result.data.Events))
}
catch {
completion (.failure(error))
}
}
}
task.resume()
}
}
struct HistoryAPIResponse: Codable {
let data: HistoryDataAPIResponse }
struct HistoryDataAPIResponse: Codable {
let Events: [Events]
}
struct Events: Codable {
let year: String
let text: String
let html: String
let no_year_html: String
let links: [Links]
}
struct Links: Codable {
let link: String
}
"http://history.muffinlabs.com/ date/6/3",
это будет что-то вроде
"http://history.muffinlabs.com/date/(cur ... urrentDay)"
Любой помощь будет принята с благодарностью.
Подробнее здесь: https://stackoverflow.com/questions/676 ... -today-app