Move local modules into a folder named Modules.

This commit is contained in:
Brent Simmons
2024-07-06 21:07:05 -07:00
parent 14bcef0f9a
commit d50b5818ac
491 changed files with 76 additions and 52 deletions

8
Modules/ReaderAPI/.gitignore vendored Normal file
View File

@@ -0,0 +1,8 @@
.DS_Store
/.build
/Packages
xcuserdata/
DerivedData/
.swiftpm/configuration/registries.json
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
.netrc

View File

@@ -0,0 +1,36 @@
// swift-tools-version: 5.10
import PackageDescription
let package = Package(
name: "ReaderAPI",
platforms: [.macOS(.v14), .iOS(.v17)],
products: [
.library(
name: "ReaderAPI",
targets: ["ReaderAPI"]),
],
dependencies: [
.package(path: "../FoundationExtras"),
.package(path: "../Web"),
.package(path: "../Secrets"),
.package(path: "../CommonErrors"),
],
targets: [
.target(
name: "ReaderAPI",
dependencies: [
"FoundationExtras",
"Web",
"Secrets",
"CommonErrors"
],
swiftSettings: [
.enableExperimentalFeature("StrictConcurrency")
]
),
.testTarget(
name: "ReaderAPITests",
dependencies: ["ReaderAPI"]),
]
)

View File

@@ -0,0 +1,582 @@
//
// ReaderAPICaller.swift
// Account
//
// Created by Jeremy Beker on 5/28/19.
// Copyright © 2019 Ranchero Software, LLC. All rights reserved.
//
import Foundation
import Web
import Secrets
import CommonErrors
public protocol ReaderAPICallerDelegate: AnyObject {
@MainActor var endpointURL: URL? { get }
@MainActor var lastArticleFetchStartTime: Date? { get set }
@MainActor var lastArticleFetchEndTime: Date? { get set }
}
public enum CreateReaderAPISubscriptionResult: Sendable {
case created(ReaderAPISubscription)
case notFound
}
@MainActor public final class ReaderAPICaller {
public enum ItemIDType {
case unread
case starred
case allForAccount
case allForFeed
}
public weak var delegate: ReaderAPICallerDelegate?
public var variant: ReaderAPIVariant = .generic
public var credentials: Credentials?
public var server: String? {
get {
return apiBaseURL?.host
}
}
private enum ReaderState: String {
case read = "user/-/state/com.google/read"
case starred = "user/-/state/com.google/starred"
}
private enum ReaderStreams: String {
case readingList = "user/-/state/com.google/reading-list"
}
private enum ReaderAPIEndpoints: String {
case login = "/accounts/ClientLogin"
case token = "/reader/api/0/token"
case disableTag = "/reader/api/0/disable-tag"
case renameTag = "/reader/api/0/rename-tag"
case tagList = "/reader/api/0/tag/list"
case subscriptionList = "/reader/api/0/subscription/list"
case subscriptionEdit = "/reader/api/0/subscription/edit"
case subscriptionAdd = "/reader/api/0/subscription/quickadd"
case contents = "/reader/api/0/stream/items/contents"
case itemIDs = "/reader/api/0/stream/items/ids"
case editTag = "/reader/api/0/edit-tag"
}
private var transport: Transport!
private let secretsProvider: SecretsProvider
private let uriComponentAllowed: CharacterSet
private var accessToken: String?
private var apiBaseURL: URL? {
get {
switch variant {
case .generic, .freshRSS:
return delegate?.endpointURL
default:
return URL(string: variant.host)
}
}
}
/// The delegate should be set in a subsequent call.
public init(transport: Transport, secretsProvider: SecretsProvider) {
self.transport = transport
self.secretsProvider = secretsProvider
var urlHostAllowed = CharacterSet.urlHostAllowed
urlHostAllowed.remove("+")
urlHostAllowed.remove("&")
self.uriComponentAllowed = urlHostAllowed
}
public func cancelAll() {
transport.cancelAll()
}
public func validateCredentials(endpoint: URL) async throws -> Credentials? {
guard let credentials else {
throw CredentialsError.incompleteCredentials
}
var request = URLRequest(url: endpoint.appendingPathComponent(ReaderAPIEndpoints.login.rawValue), readerAPICredentials: credentials)
addVariantHeaders(&request)
do {
let (_, data) = try await transport.send(request: request)
guard let data else {
throw TransportError.noData
}
// Convert the return data to UTF8 and then parse out the Auth token
guard let rawData = String(data: data, encoding: .utf8) else {
throw TransportError.noData
}
var authData: [String: String] = [:]
for line in rawData.split(separator: "\n") {
let items = line.split(separator: "=").map { String($0) }
if items.count == 2 {
authData[items[0]] = items[1]
}
}
guard let authString = authData["Auth"] else {
throw CredentialsError.incompleteCredentials
}
// Save Auth Token for later use
self.credentials = Credentials(type: .readerAPIKey, username: credentials.username, secret: authString)
return self.credentials
} catch {
if let transportError = error as? TransportError, case .httpError(let code) = transportError, code == 404 {
throw ReaderAPIError.urlNotFound
} else {
throw error
}
}
}
func requestAuthorizationToken(endpoint: URL) async throws -> String {
// If we have a token already, use it
if let accessToken {
return accessToken
}
// Otherwise request one.
guard let credentials else {
throw CredentialsError.incompleteCredentials
}
var request = URLRequest(url: endpoint.appendingPathComponent(ReaderAPIEndpoints.token.rawValue), readerAPICredentials: credentials)
addVariantHeaders(&request)
let (_, data) = try await transport.send(request: request)
// Convert the return data to UTF8 and then parse out the Auth token
guard let data, let accessToken = String(data: data, encoding: .utf8) else {
throw TransportError.noData
}
self.accessToken = accessToken
return accessToken
}
public func retrieveTags() async throws -> [ReaderAPITag]? {
guard let baseURL = apiBaseURL else {
throw CredentialsError.incompleteCredentials
}
var url = baseURL
.appendingPathComponent(ReaderAPIEndpoints.tagList.rawValue)
.appendingQueryItem(URLQueryItem(name: "output", value: "json"))
if variant == .inoreader {
url = url?.appendingQueryItem(URLQueryItem(name: "types", value: "1"))
}
guard let callURL = url else {
throw TransportError.noURL
}
var request = URLRequest(url: callURL, readerAPICredentials: credentials)
addVariantHeaders(&request)
let (_, wrapper) = try await transport.send(request: request, resultType: ReaderAPITagContainer.self)
return wrapper?.tags
}
public func renameTag(oldName: String, newName: String) async throws {
guard let baseURL = apiBaseURL else {
throw CredentialsError.incompleteCredentials
}
let token = try await requestAuthorizationToken(endpoint: baseURL)
var request = URLRequest(url: baseURL.appendingPathComponent(ReaderAPIEndpoints.renameTag.rawValue), readerAPICredentials: self.credentials)
self.addVariantHeaders(&request)
request.setValue(MimeType.formURLEncoded, forHTTPHeaderField: "Content-Type")
request.httpMethod = "POST"
guard let encodedOldName = self.encodeForURLPath(oldName), let encodedNewName = self.encodeForURLPath(newName) else {
throw ReaderAPIError.invalidParameter
}
let oldTagName = "user/-/label/\(encodedOldName)"
let newTagName = "user/-/label/\(encodedNewName)"
let postData = "T=\(token)&s=\(oldTagName)&dest=\(newTagName)".data(using: String.Encoding.utf8)
try await transport.send(request: request, method: HTTPMethod.post, payload: postData!)
}
public func deleteTag(folderExternalID: String) async throws {
guard let baseURL = apiBaseURL else {
throw CredentialsError.incompleteCredentials
}
let token = try await self.requestAuthorizationToken(endpoint: baseURL)
var request = URLRequest(url: baseURL.appendingPathComponent(ReaderAPIEndpoints.disableTag.rawValue), readerAPICredentials: self.credentials)
self.addVariantHeaders(&request)
request.setValue(MimeType.formURLEncoded, forHTTPHeaderField: "Content-Type")
request.httpMethod = "POST"
let postData = "T=\(token)&s=\(folderExternalID)".data(using: String.Encoding.utf8)
try await self.transport.send(request: request, method: HTTPMethod.post, payload: postData!)
}
public func retrieveSubscriptions() async throws -> [ReaderAPISubscription]? {
guard let baseURL = apiBaseURL else {
throw CredentialsError.incompleteCredentials
}
let url = baseURL
.appendingPathComponent(ReaderAPIEndpoints.subscriptionList.rawValue)
.appendingQueryItem(URLQueryItem(name: "output", value: "json"))
guard let callURL = url else {
throw TransportError.noURL
}
var request = URLRequest(url: callURL, readerAPICredentials: credentials)
addVariantHeaders(&request)
let (_, container) = try await transport.send(request: request, resultType: ReaderAPISubscriptionContainer.self)
return container?.subscriptions
}
public func createSubscription(url: String, name: String?) async throws -> CreateReaderAPISubscriptionResult {
guard let baseURL = apiBaseURL else {
throw CredentialsError.incompleteCredentials
}
let token = try await self.requestAuthorizationToken(endpoint: baseURL)
let callURL = baseURL
.appendingPathComponent(ReaderAPIEndpoints.subscriptionAdd.rawValue)
var request = URLRequest(url: callURL, readerAPICredentials: self.credentials)
self.addVariantHeaders(&request)
request.setValue(MimeType.formURLEncoded, forHTTPHeaderField: "Content-Type")
request.httpMethod = "POST"
guard let encodedFeedURL = self.encodeForURLPath(url) else {
throw ReaderAPIError.invalidParameter
}
let postData = "T=\(token)&quickadd=\(encodedFeedURL)".data(using: String.Encoding.utf8)
let (_, subResult) = try await self.transport.send(request: request, method: HTTPMethod.post, data: postData!, resultType: ReaderAPIQuickAddResult.self)
guard let subResult else {
return .notFound
}
if subResult.numResults == 0 {
return .notFound
}
// There is no call to get a single subscription entry, so we get them all,
// look up the one we just subscribed to and return that
guard let subscriptions = try await retrieveSubscriptions() else {
throw AccountError.createErrorNotFound
}
guard let subscription = subscriptions.first(where: { $0.feedID == subResult.streamID }) else {
throw AccountError.createErrorNotFound
}
return .created(subscription)
}
public func renameSubscription(subscriptionID: String, newName: String) async throws {
try await changeSubscription(subscriptionID: subscriptionID, title: newName)
}
public func deleteSubscription(subscriptionID: String) async throws {
guard let baseURL = apiBaseURL else {
throw CredentialsError.incompleteCredentials
}
let token = try await self.requestAuthorizationToken(endpoint: baseURL)
var request = URLRequest(url: baseURL.appendingPathComponent(ReaderAPIEndpoints.subscriptionEdit.rawValue), readerAPICredentials: self.credentials)
self.addVariantHeaders(&request)
request.setValue(MimeType.formURLEncoded, forHTTPHeaderField: "Content-Type")
request.httpMethod = "POST"
let postData = "T=\(token)&s=\(subscriptionID)&ac=unsubscribe".data(using: String.Encoding.utf8)
try await self.transport.send(request: request, method: HTTPMethod.post, payload: postData!)
}
public func createTagging(subscriptionID: String, tagName: String) async throws {
try await changeSubscription(subscriptionID: subscriptionID, addTagName: tagName)
}
public func deleteTagging(subscriptionID: String, tagName: String) async throws {
try await changeSubscription(subscriptionID: subscriptionID, removeTagName: tagName)
}
public func moveSubscription(subscriptionID: String, sourceTag: String, destinationTag: String) async throws {
try await changeSubscription(subscriptionID: subscriptionID, removeTagName: sourceTag, addTagName: destinationTag)
}
private func changeSubscription(subscriptionID: String, removeTagName: String? = nil, addTagName: String? = nil, title: String? = nil) async throws {
guard removeTagName != nil || addTagName != nil || title != nil else {
throw ReaderAPIError.invalidParameter
}
guard let baseURL = apiBaseURL else {
throw CredentialsError.incompleteCredentials
}
let token = try await requestAuthorizationToken(endpoint: baseURL)
var request = URLRequest(url: baseURL.appendingPathComponent(ReaderAPIEndpoints.subscriptionEdit.rawValue), readerAPICredentials: self.credentials)
self.addVariantHeaders(&request)
request.setValue(MimeType.formURLEncoded, forHTTPHeaderField: "Content-Type")
request.httpMethod = "POST"
var postString = "T=\(token)&s=\(subscriptionID)&ac=edit"
if let fromLabel = self.encodeForURLPath(removeTagName) {
postString += "&r=user/-/label/\(fromLabel)"
}
if let toLabel = self.encodeForURLPath(addTagName) {
postString += "&a=user/-/label/\(toLabel)"
}
if let encodedTitle = self.encodeForURLPath(title) {
postString += "&t=\(encodedTitle)"
}
let postData = postString.data(using: String.Encoding.utf8)
try await transport.send(request: request, method: HTTPMethod.post, payload: postData!)
}
public func retrieveEntries(articleIDs: [String]) async throws -> [ReaderAPIEntry]? {
guard !articleIDs.isEmpty else {
return [ReaderAPIEntry]()
}
guard let baseURL = apiBaseURL else {
throw CredentialsError.incompleteCredentials
}
let token = try await requestAuthorizationToken(endpoint: baseURL)
var request = URLRequest(url: baseURL.appendingPathComponent(ReaderAPIEndpoints.contents.rawValue), readerAPICredentials: self.credentials)
self.addVariantHeaders(&request)
request.setValue(MimeType.formURLEncoded, forHTTPHeaderField: "Content-Type")
request.httpMethod = "POST"
// Get ids from above into hex representation of value
let idsToFetch = articleIDs.map({ articleID -> String in
if self.variant == .theOldReader {
return "i=tag:google.com,2005:reader/item/\(articleID)"
} else {
let idValue = Int(articleID)!
let idHexString = String(idValue, radix: 16, uppercase: false)
return "i=tag:google.com,2005:reader/item/\(idHexString)"
}
}).joined(separator:"&")
let postData = "T=\(token)&output=json&\(idsToFetch)".data(using: String.Encoding.utf8)
let (_, entryWrapper) = try await transport.send(request: request, method: HTTPMethod.post, data: postData!, resultType: ReaderAPIEntryWrapper.self)
guard let entryWrapper else {
throw ReaderAPIError.invalidResponse
}
return entryWrapper.entries
}
public func retrieveItemIDs(type: ItemIDType, feedID: String? = nil) async throws -> [String] {
guard let baseURL = apiBaseURL else {
throw CredentialsError.incompleteCredentials
}
var queryItems = [
URLQueryItem(name: "n", value: "1000"),
URLQueryItem(name: "output", value: "json")
]
switch type {
case .allForAccount:
let since: Date = {
if let lastArticleFetch = delegate?.lastArticleFetchStartTime {
return lastArticleFetch
} else {
return Calendar.current.date(byAdding: .month, value: -3, to: Date()) ?? Date()
}
}()
let sinceTimeInterval = since.timeIntervalSince1970
queryItems.append(URLQueryItem(name: "ot", value: String(Int(sinceTimeInterval))))
queryItems.append(URLQueryItem(name: "s", value: ReaderStreams.readingList.rawValue))
case .allForFeed:
guard let feedID else {
throw ReaderAPIError.invalidParameter
}
let sinceTimeInterval = (Calendar.current.date(byAdding: .month, value: -3, to: Date()) ?? Date()).timeIntervalSince1970
queryItems.append(URLQueryItem(name: "ot", value: String(Int(sinceTimeInterval))))
queryItems.append(URLQueryItem(name: "s", value: feedID))
case .unread:
queryItems.append(URLQueryItem(name: "s", value: ReaderStreams.readingList.rawValue))
queryItems.append(URLQueryItem(name: "xt", value: ReaderState.read.rawValue))
case .starred:
queryItems.append(URLQueryItem(name: "s", value: ReaderState.starred.rawValue))
}
let url = baseURL
.appendingPathComponent(ReaderAPIEndpoints.itemIDs.rawValue)
.appendingQueryItems(queryItems)
guard let callURL = url else {
throw TransportError.noURL
}
var request: URLRequest = URLRequest(url: callURL, readerAPICredentials: credentials)
addVariantHeaders(&request)
let (response, entries) = try await transport.send(request: request, resultType: ReaderAPIReferenceWrapper.self)
guard let entriesItemRefs = entries?.itemRefs, entriesItemRefs.count > 0 else {
return [String]()
}
let dateInfo = HTTPDateInfo(urlResponse: response)
let itemIDs = entriesItemRefs.compactMap { $0.itemID }
return try await retrieveItemIDs(type: type, url: callURL, dateInfo: dateInfo, itemIDs: itemIDs, continuation: entries?.continuation)
}
func retrieveItemIDs(type: ItemIDType, url: URL, dateInfo: HTTPDateInfo?, itemIDs: [String], continuation: String?) async throws -> [String] {
guard let continuation else {
if type == .allForAccount {
delegate?.lastArticleFetchStartTime = dateInfo?.date
delegate?.lastArticleFetchEndTime = Date()
}
return itemIDs
}
guard var urlComponents = URLComponents(url: url, resolvingAgainstBaseURL: false) else {
throw ReaderAPIError.invalidParameter
}
var queryItems = urlComponents.queryItems!.filter({ $0.name != "c" })
queryItems.append(URLQueryItem(name: "c", value: continuation))
urlComponents.queryItems = queryItems
guard let callURL = urlComponents.url else {
throw TransportError.noURL
}
var request: URLRequest = URLRequest(url: callURL, readerAPICredentials: credentials)
addVariantHeaders(&request)
let (_, entries) = try await self.transport.send(request: request, resultType: ReaderAPIReferenceWrapper.self)
guard let entriesItemRefs = entries?.itemRefs, entriesItemRefs.count > 0 else {
return try await retrieveItemIDs(type: type, url: callURL, dateInfo: dateInfo, itemIDs: itemIDs, continuation: entries?.continuation)
}
var totalItemIDs = itemIDs
totalItemIDs.append(contentsOf: entriesItemRefs.compactMap { $0.itemID })
return try await retrieveItemIDs(type: type, url: callURL, dateInfo: dateInfo, itemIDs: totalItemIDs, continuation: entries?.continuation)
}
public func createUnreadEntries(entries: [String]) async throws {
try await updateStateToEntries(entries: entries, state: .read, add: false)
}
public func deleteUnreadEntries(entries: [String]) async throws {
try await updateStateToEntries(entries: entries, state: .read, add: true)
}
public func createStarredEntries(entries: [String]) async throws {
try await updateStateToEntries(entries: entries, state: .starred, add: true)
}
public func deleteStarredEntries(entries: [String]) async throws {
try await updateStateToEntries(entries: entries, state: .starred, add: false)
}
}
// MARK: Private
private extension ReaderAPICaller {
func encodeForURLPath(_ pathComponent: String?) -> String? {
guard let pathComponent = pathComponent else { return nil }
return pathComponent.addingPercentEncoding(withAllowedCharacters: uriComponentAllowed)
}
func addVariantHeaders(_ request: inout URLRequest) {
if variant == .inoreader {
request.addValue(secretsProvider.inoreaderAppId, forHTTPHeaderField: "AppId")
request.addValue(secretsProvider.inoreaderAppKey, forHTTPHeaderField: "AppKey")
}
}
private func updateStateToEntries(entries: [String], state: ReaderState, add: Bool) async throws {
guard let baseURL = apiBaseURL else {
throw CredentialsError.incompleteCredentials
}
let token = try await requestAuthorizationToken(endpoint: baseURL)
// Do POST asking for data about all the new articles
var request = URLRequest(url: baseURL.appendingPathComponent(ReaderAPIEndpoints.editTag.rawValue), readerAPICredentials: self.credentials)
self.addVariantHeaders(&request)
request.setValue(MimeType.formURLEncoded, forHTTPHeaderField: "Content-Type")
request.httpMethod = "POST"
// Get ids from above into hex representation of value
let idsToFetch = entries.compactMap({ idValue -> String? in
if self.variant == .theOldReader {
return "i=tag:google.com,2005:reader/item/\(idValue)"
} else {
guard let intValue = Int(idValue) else { return nil }
let idHexString = String(format: "%.16llx", intValue)
return "i=tag:google.com,2005:reader/item/\(idHexString)"
}
}).joined(separator:"&")
let actionIndicator = add ? "a" : "r"
let postData = "T=\(token)&\(idsToFetch)&\(actionIndicator)=\(state.rawValue)".data(using: String.Encoding.utf8)
try await transport.send(request: request, method: HTTPMethod.post, payload: postData!)
}
}

View File

@@ -0,0 +1,132 @@
//
// ReaderAPIArticle.swift
// Account
//
// Created by Jeremy Beker on 5/28/19.
// Copyright © 2017 Ranchero Software, LLC. All rights reserved.
//
import Foundation
public struct ReaderAPIEntryWrapper: Codable, Sendable {
public let id: String
public let updated: Int
public let entries: [ReaderAPIEntry]
enum CodingKeys: String, CodingKey {
case id = "id"
case updated = "updated"
case entries = "items"
}
}
/* {
"id": "tag:google.com,2005:reader/item/00058a3b5197197b",
"crawlTimeMsec": "1559362260113",
"timestampUsec": "1559362260113787",
"published": 1554845280,
"title": "",
"summary": {
"content": "\n<p>Found an old screenshot of NetNewsWire 1.0 for iPhone!</p>\n\n<p><img src=\"https://nnw.ranchero.com/uploads/2019/c07c0574b1.jpg\" alt=\"Netnewswire 1.0 for iPhone screenshot showing the list of feeds.\" title=\"NewsGator got renamed to Sitrion, years later, and then renamed again as Limeade.\" border=\"0\" width=\"260\" height=\"320\"></p>\n"
},
"alternate": [
{
"href": "https://nnw.ranchero.com/2019/04/09/found-an-old.html"
}
],
"categories": [
"user/-/state/com.google/reading-list",
"user/-/label/Uncategorized"
],
"origin": {
"streamId": "feed/130",
"title": "NetNewsWire"
}
}
*/
public struct ReaderAPIEntry: Codable, Sendable {
public let articleID: String
public let title: String?
public let author: String?
public let publishedTimestamp: Double?
public let crawledTimestamp: String?
public let timestampUsec: String?
public let summary: ReaderAPIArticleSummary
public let alternates: [ReaderAPIAlternateLocation]?
public let categories: [String]
public let origin: ReaderAPIEntryOrigin
enum CodingKeys: String, CodingKey {
case articleID = "id"
case title = "title"
case author = "author"
case summary = "summary"
case alternates = "alternate"
case categories = "categories"
case publishedTimestamp = "published"
case crawledTimestamp = "crawlTimeMsec"
case origin = "origin"
case timestampUsec = "timestampUsec"
}
public func parseDatePublished() -> Date? {
guard let unixTime = publishedTimestamp else {
return nil
}
return Date(timeIntervalSince1970: unixTime)
}
public func uniqueID(variant: ReaderAPIVariant) -> String {
// Should look something like "tag:google.com,2005:reader/item/00058b10ce338909"
// REGEX feels heavy, I should be able to just split on / and take the last element
guard let idPart = articleID.components(separatedBy: "/").last else {
return articleID
}
guard variant != .theOldReader else {
return idPart
}
// Convert hex representation back to integer and then a string representation
guard let idNumber = Int(idPart, radix: 16) else {
return articleID
}
return String(idNumber, radix: 10, uppercase: false)
}
}
public struct ReaderAPIArticleSummary: Codable, Sendable {
public let content: String?
enum CodingKeys: String, CodingKey {
case content = "content"
}
}
public struct ReaderAPIAlternateLocation: Codable, Sendable {
public let url: String?
enum CodingKeys: String, CodingKey {
case url = "href"
}
}
public struct ReaderAPIEntryOrigin: Codable, Sendable {
public let streamID: String?
public let title: String?
enum CodingKeys: String, CodingKey {
case streamID = "streamId"
case title = "title"
}
}

View File

@@ -0,0 +1,29 @@
//
// ReaderAPIError.swift
//
//
// Created by Brent Simmons on 4/6/24.
//
import Foundation
public enum ReaderAPIError: LocalizedError {
case unknown
case invalidParameter
case invalidResponse
case urlNotFound
public var errorDescription: String? {
switch self {
case .unknown:
return NSLocalizedString("An unexpected error occurred.", comment: "An unexpected error occurred.")
case .invalidParameter:
return NSLocalizedString("An invalid parameter was passed.", comment: "An invalid parameter was passed.")
case .invalidResponse:
return NSLocalizedString("There was an invalid response from the server.", comment: "There was an invalid response from the server.")
case .urlNotFound:
return NSLocalizedString("The API URL wasn't found.", comment: "The API URL wasn't found.")
}
}
}

View File

@@ -0,0 +1,115 @@
//
// ReaderAPISubscription.swift
// Account
//
// Created by Jeremy Beker on 5/28/19.
// Copyright © 2017 Ranchero Software, LLC. All rights reserved.
//
import Foundation
import FoundationExtras
/*
{
"numResults":0,
"error": "Already subscribed! https://inessential.com/xml/rss.xml
}
*/
public struct ReaderAPIQuickAddResult: Codable, Sendable {
public let numResults: Int
public let error: String?
public let streamID: String?
enum CodingKeys: String, CodingKey {
case numResults = "numResults"
case error = "error"
case streamID = "streamId"
}
}
public struct ReaderAPISubscriptionContainer: Codable, Sendable {
public let subscriptions: [ReaderAPISubscription]
enum CodingKeys: String, CodingKey {
case subscriptions = "subscriptions"
}
}
/*
{
"id": "feed/1",
"title": "Questionable Content",
"categories": [
{
"id": "user/-/label/Comics",
"label": "Comics"
}
],
"url": "http://www.questionablecontent.net/QCRSS.xml",
"htmlUrl": "http://www.questionablecontent.net",
"iconUrl": "https://rss.confusticate.com/f.php?24decabc"
}
*/
public struct ReaderAPISubscription: Codable, Sendable {
public let feedID: String
public let name: String?
public let categories: [ReaderAPICategory]
public let feedURL: String?
public let homePageURL: String?
public let iconURL: String?
enum CodingKeys: String, CodingKey {
case feedID = "id"
case name = "title"
case categories = "categories"
case feedURL = "url"
case homePageURL = "htmlUrl"
case iconURL = "iconUrl"
}
public var url: String {
if let feedURL = feedURL {
return feedURL
} else {
return feedID.stripping(prefix: "feed/")
}
}
}
public struct ReaderAPICategory: Codable, Sendable {
public let categoryID: String
public let categoryLabel: String
enum CodingKeys: String, CodingKey {
case categoryID = "id"
case categoryLabel = "label"
}
}
public struct ReaderAPICreateSubscription: Codable, Sendable {
public let feedURL: String
enum CodingKeys: String, CodingKey {
case feedURL = "feed_url"
}
}
public struct ReaderAPISubscriptionChoice: Codable, Sendable {
public let name: String?
public let url: String
enum CodingKeys: String, CodingKey {
case name = "title"
case url = "feed_url"
}
}

View File

@@ -0,0 +1,36 @@
//
// ReaderAPICompatibleTag.swift
// Account
//
// Created by Jeremy Beker on 5/28/19.
// Copyright © 2019 Ranchero Software, LLC. All rights reserved.
//
import Foundation
public struct ReaderAPITagContainer: Codable, Sendable {
public let tags: [ReaderAPITag]
enum CodingKeys: String, CodingKey {
case tags = "tags"
}
}
public struct ReaderAPITag: Codable, Sendable {
public let tagID: String
public let type: String?
enum CodingKeys: String, CodingKey {
case tagID = "id"
case type = "type"
}
public var folderName: String? {
guard let range = tagID.range(of: "/label/") else {
return nil
}
return String(tagID.suffix(from: range.upperBound))
}
}

View File

@@ -0,0 +1,35 @@
//
// ReaderAPICompatibleTagging.swift
// Account
//
// Created by Jeremy Beker on 5/28/19.
// Copyright © 2018 Ranchero Software, LLC. All rights reserved.
//
import Foundation
public struct ReaderAPITagging: Codable, Sendable {
public let taggingID: Int
public let feedID: Int
public let name: String
enum CodingKeys: String, CodingKey {
case taggingID = "id"
case feedID = "feed_id"
case name = "name"
}
}
public struct ReaderAPICreateTagging: Codable, Sendable {
public let feedID: Int
public let name: String
enum CodingKeys: String, CodingKey {
case feedID = "feed_id"
case name = "name"
}
}

View File

@@ -0,0 +1,29 @@
//
// ReaderAPIUnreadEntry.swift
// Account
//
// Created by Jeremy Beker on 5/28/19.
// Copyright © 2019 Ranchero Software, LLC. All rights reserved.
//
import Foundation
public struct ReaderAPIReferenceWrapper: Codable, Sendable {
public let itemRefs: [ReaderAPIReference]?
public let continuation: String?
enum CodingKeys: String, CodingKey {
case itemRefs = "itemRefs"
case continuation = "continuation"
}
}
public struct ReaderAPIReference: Codable, Sendable {
public let itemID: String?
enum CodingKeys: String, CodingKey {
case itemID = "id"
}
}

View File

@@ -0,0 +1,30 @@
//
// ReaderAPIVariant.swift
//
//
// Created by Maurice Parker on 10/23/20.
//
import Foundation
public enum ReaderAPIVariant: Sendable {
case generic
case freshRSS
case inoreader
case bazQux
case theOldReader
public var host: String {
switch self {
case .inoreader:
return "https://www.inoreader.com"
case .bazQux:
return "https://bazqux.com"
case .theOldReader:
return "https://theoldreader.com"
default:
return ""
}
}
}

View File

@@ -0,0 +1,44 @@
//
// URLRequest+ReaderAPI.swift
//
//
// Created by Brent Simmons on 4/6/24.
//
import Foundation
import Secrets
import Web
extension URLRequest {
init(url: URL, readerAPICredentials: Credentials?, conditionalGet: HTTPConditionalGetInfo? = nil) {
self.init(url: url)
guard let credentials = readerAPICredentials else {
return
}
let credentialsType = credentials.type
precondition(credentialsType == .readerBasic || credentialsType == .readerAPIKey)
if credentialsType == .readerBasic {
setValue(MimeType.formURLEncoded, forHTTPHeaderField: "Content-Type")
httpMethod = "POST"
var postData = URLComponents()
postData.queryItems = [
URLQueryItem(name: "Email", value: credentials.username),
URLQueryItem(name: "Passwd", value: credentials.secret)
]
httpBody = postData.enhancedPercentEncodedQuery?.data(using: .utf8)
} else if credentialsType == .readerAPIKey {
let auth = "GoogleLogin auth=\(credentials.secret)"
setValue(auth, forHTTPHeaderField: HTTPRequestHeader.authorization)
}
conditionalGet?.addRequestHeadersToURLRequest(&self)
}
}

View File

@@ -0,0 +1,12 @@
import XCTest
@testable import ReaderAPI
final class ReaderAPITests: XCTestCase {
func testExample() throws {
// XCTest Documentation
// https://developer.apple.com/documentation/xctest
// Defining Test Cases and Test Methods
// https://developer.apple.com/documentation/xctest/defining_test_cases_and_test_methods
}
}