2019-09-18 01:18:06 +02:00
|
|
|
//
|
|
|
|
// FeedlyAPICaller.swift
|
|
|
|
// Account
|
|
|
|
//
|
|
|
|
// Created by Kiel Gillard on 13/9/19.
|
|
|
|
// Copyright © 2019 Ranchero Software, LLC. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import RSWeb
|
|
|
|
|
|
|
|
final class FeedlyAPICaller {
|
|
|
|
|
|
|
|
enum API {
|
|
|
|
case sandbox
|
|
|
|
case cloud
|
|
|
|
|
|
|
|
var baseUrlComponents: URLComponents {
|
|
|
|
var components = URLComponents()
|
|
|
|
components.scheme = "https"
|
|
|
|
switch self{
|
|
|
|
case .sandbox:
|
|
|
|
// https://groups.google.com/forum/#!topic/feedly-cloud/WwQWMgDmOuw
|
|
|
|
components.host = "sandbox7.feedly.com"
|
|
|
|
case .cloud:
|
|
|
|
// https://developer.feedly.com/cloud/
|
|
|
|
components.host = "cloud.feedly.com"
|
|
|
|
}
|
|
|
|
return components
|
|
|
|
}
|
2019-11-08 08:35:22 +01:00
|
|
|
|
|
|
|
var oauthAuthorizationClient: OAuthAuthorizationClient {
|
|
|
|
switch self {
|
|
|
|
case .sandbox:
|
|
|
|
return .feedlySandboxClient
|
|
|
|
case .cloud:
|
|
|
|
return .feedlyCloudClient
|
|
|
|
}
|
|
|
|
}
|
2019-09-18 01:18:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private let transport: Transport
|
|
|
|
private let baseUrlComponents: URLComponents
|
|
|
|
|
|
|
|
init(transport: Transport, api: API) {
|
|
|
|
self.transport = transport
|
|
|
|
self.baseUrlComponents = api.baseUrlComponents
|
|
|
|
}
|
|
|
|
|
|
|
|
var credentials: Credentials?
|
|
|
|
|
|
|
|
var server: String? {
|
|
|
|
return baseUrlComponents.host
|
|
|
|
}
|
2019-09-19 04:56:43 +02:00
|
|
|
|
2019-11-05 03:24:21 +01:00
|
|
|
func cancelAll() {
|
|
|
|
transport.cancelAll()
|
|
|
|
}
|
|
|
|
|
2019-12-05 20:14:05 +01:00
|
|
|
private var isSuspended = false
|
|
|
|
|
|
|
|
/// Cancels all pending requests rejects any that come in later
|
|
|
|
func suspend() {
|
|
|
|
transport.cancelAll()
|
|
|
|
isSuspended = true
|
|
|
|
}
|
|
|
|
|
|
|
|
func resume() {
|
|
|
|
isSuspended = false
|
|
|
|
}
|
|
|
|
|
2019-12-15 01:14:55 +01:00
|
|
|
func importOpml(_ opmlData: Data, completion: @escaping (Result<Void, Error>) -> ()) {
|
2019-12-05 20:14:05 +01:00
|
|
|
guard !isSuspended else {
|
|
|
|
return DispatchQueue.main.async {
|
2019-12-15 01:14:55 +01:00
|
|
|
completion(.failure(TransportError.suspended))
|
2019-12-05 20:14:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-23 09:29:53 +02:00
|
|
|
guard let accessToken = credentials?.secret else {
|
|
|
|
return DispatchQueue.main.async {
|
2019-12-15 01:14:55 +01:00
|
|
|
completion(.failure(CredentialsError.incompleteCredentials))
|
2019-09-23 09:29:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
var components = baseUrlComponents
|
|
|
|
components.path = "/v3/opml"
|
2019-09-19 04:56:43 +02:00
|
|
|
|
2019-09-23 09:29:53 +02:00
|
|
|
guard let url = components.url else {
|
|
|
|
fatalError("\(components) does not produce a valid URL.")
|
|
|
|
}
|
|
|
|
|
|
|
|
var request = URLRequest(url: url)
|
|
|
|
request.httpMethod = "POST"
|
|
|
|
request.addValue("text/xml", forHTTPHeaderField: HTTPRequestHeader.contentType)
|
|
|
|
request.addValue("application/json", forHTTPHeaderField: "Accept-Type")
|
|
|
|
request.addValue("OAuth \(accessToken)", forHTTPHeaderField: HTTPRequestHeader.authorization)
|
|
|
|
request.httpBody = opmlData
|
|
|
|
|
2019-09-27 10:13:27 +02:00
|
|
|
transport.send(request: request, resultType: String.self, dateDecoding: .millisecondsSince1970, keyDecoding: .convertFromSnakeCase) { result in
|
|
|
|
switch result {
|
|
|
|
case .success(let (httpResponse, _)):
|
|
|
|
if httpResponse.statusCode == 200 {
|
2019-12-15 01:14:55 +01:00
|
|
|
completion(.success(()))
|
2019-09-27 10:13:27 +02:00
|
|
|
} else {
|
2019-12-15 01:14:55 +01:00
|
|
|
completion(.failure(URLError(.cannotDecodeContentData)))
|
2019-09-27 10:13:27 +02:00
|
|
|
}
|
|
|
|
case .failure(let error):
|
2019-12-15 01:14:55 +01:00
|
|
|
completion(.failure(error))
|
2019-09-27 10:13:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-15 01:14:55 +01:00
|
|
|
func createCollection(named label: String, completion: @escaping (Result<FeedlyCollection, Error>) -> ()) {
|
2019-12-05 20:14:05 +01:00
|
|
|
guard !isSuspended else {
|
|
|
|
return DispatchQueue.main.async {
|
2019-12-15 01:14:55 +01:00
|
|
|
completion(.failure(TransportError.suspended))
|
2019-12-05 20:14:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-27 10:13:27 +02:00
|
|
|
guard let accessToken = credentials?.secret else {
|
|
|
|
return DispatchQueue.main.async {
|
2019-12-15 01:14:55 +01:00
|
|
|
completion(.failure(CredentialsError.incompleteCredentials))
|
2019-09-27 10:13:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
var components = baseUrlComponents
|
|
|
|
components.path = "/v3/collections"
|
|
|
|
|
|
|
|
guard let url = components.url else {
|
|
|
|
fatalError("\(components) does not produce a valid URL.")
|
|
|
|
}
|
|
|
|
|
|
|
|
var request = URLRequest(url: url)
|
|
|
|
request.httpMethod = "POST"
|
|
|
|
request.addValue("application/json", forHTTPHeaderField: HTTPRequestHeader.contentType)
|
|
|
|
request.addValue("application/json", forHTTPHeaderField: "Accept-Type")
|
|
|
|
request.addValue("OAuth \(accessToken)", forHTTPHeaderField: HTTPRequestHeader.authorization)
|
|
|
|
|
|
|
|
do {
|
|
|
|
struct CreateCollectionBody: Encodable {
|
|
|
|
var label: String
|
|
|
|
}
|
|
|
|
let encoder = JSONEncoder()
|
|
|
|
let data = try encoder.encode(CreateCollectionBody(label: label))
|
|
|
|
request.httpBody = data
|
|
|
|
} catch {
|
|
|
|
return DispatchQueue.main.async {
|
2019-12-15 01:14:55 +01:00
|
|
|
completion(.failure(error))
|
2019-09-27 10:13:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
transport.send(request: request, resultType: [FeedlyCollection].self, dateDecoding: .millisecondsSince1970, keyDecoding: .convertFromSnakeCase) { result in
|
|
|
|
switch result {
|
|
|
|
case .success(let (httpResponse, collections)):
|
|
|
|
if httpResponse.statusCode == 200, let collection = collections?.first {
|
2019-12-15 01:14:55 +01:00
|
|
|
completion(.success(collection))
|
2019-09-27 10:13:27 +02:00
|
|
|
} else {
|
2019-12-15 01:14:55 +01:00
|
|
|
completion(.failure(URLError(.cannotDecodeContentData)))
|
2019-09-27 10:13:27 +02:00
|
|
|
}
|
|
|
|
case .failure(let error):
|
2019-12-15 01:14:55 +01:00
|
|
|
completion(.failure(error))
|
2019-09-27 10:13:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-15 01:14:55 +01:00
|
|
|
func renameCollection(with id: String, to name: String, completion: @escaping (Result<FeedlyCollection, Error>) -> ()) {
|
2019-12-05 20:14:05 +01:00
|
|
|
guard !isSuspended else {
|
|
|
|
return DispatchQueue.main.async {
|
2019-12-15 01:14:55 +01:00
|
|
|
completion(.failure(TransportError.suspended))
|
2019-12-05 20:14:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-27 10:13:27 +02:00
|
|
|
guard let accessToken = credentials?.secret else {
|
|
|
|
return DispatchQueue.main.async {
|
2019-12-15 01:14:55 +01:00
|
|
|
completion(.failure(CredentialsError.incompleteCredentials))
|
2019-09-27 10:13:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
var components = baseUrlComponents
|
|
|
|
components.path = "/v3/collections"
|
|
|
|
|
|
|
|
guard let url = components.url else {
|
|
|
|
fatalError("\(components) does not produce a valid URL.")
|
|
|
|
}
|
|
|
|
|
|
|
|
var request = URLRequest(url: url)
|
|
|
|
request.httpMethod = "POST"
|
|
|
|
request.addValue("application/json", forHTTPHeaderField: HTTPRequestHeader.contentType)
|
|
|
|
request.addValue("application/json", forHTTPHeaderField: "Accept-Type")
|
|
|
|
request.addValue("OAuth \(accessToken)", forHTTPHeaderField: HTTPRequestHeader.authorization)
|
|
|
|
|
|
|
|
do {
|
|
|
|
struct RenameCollectionBody: Encodable {
|
|
|
|
var id: String
|
|
|
|
var label: String
|
|
|
|
}
|
|
|
|
let encoder = JSONEncoder()
|
|
|
|
let data = try encoder.encode(RenameCollectionBody(id: id, label: name))
|
|
|
|
request.httpBody = data
|
|
|
|
} catch {
|
|
|
|
return DispatchQueue.main.async {
|
2019-12-15 01:14:55 +01:00
|
|
|
completion(.failure(error))
|
2019-09-27 10:13:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
transport.send(request: request, resultType: [FeedlyCollection].self, dateDecoding: .millisecondsSince1970, keyDecoding: .convertFromSnakeCase) { result in
|
|
|
|
switch result {
|
|
|
|
case .success(let (httpResponse, collections)):
|
|
|
|
if httpResponse.statusCode == 200, let collection = collections?.first {
|
2019-12-15 01:14:55 +01:00
|
|
|
completion(.success(collection))
|
2019-09-27 10:13:27 +02:00
|
|
|
} else {
|
2019-12-15 01:14:55 +01:00
|
|
|
completion(.failure(URLError(.cannotDecodeContentData)))
|
2019-09-27 10:13:27 +02:00
|
|
|
}
|
|
|
|
case .failure(let error):
|
2019-12-15 01:14:55 +01:00
|
|
|
completion(.failure(error))
|
2019-09-27 10:13:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private func encodeForURLPath(_ pathComponent: String) -> String? {
|
|
|
|
return pathComponent.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)
|
|
|
|
}
|
|
|
|
|
2019-12-15 01:14:55 +01:00
|
|
|
func deleteCollection(with id: String, completion: @escaping (Result<Void, Error>) -> ()) {
|
2019-12-05 20:14:05 +01:00
|
|
|
guard !isSuspended else {
|
|
|
|
return DispatchQueue.main.async {
|
2019-12-15 01:14:55 +01:00
|
|
|
completion(.failure(TransportError.suspended))
|
2019-12-05 20:14:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-27 10:13:27 +02:00
|
|
|
guard let accessToken = credentials?.secret else {
|
|
|
|
return DispatchQueue.main.async {
|
2019-12-15 01:14:55 +01:00
|
|
|
completion(.failure(CredentialsError.incompleteCredentials))
|
2019-09-27 10:13:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
guard let encodedId = encodeForURLPath(id) else {
|
|
|
|
return DispatchQueue.main.async {
|
2019-12-15 01:14:55 +01:00
|
|
|
completion(.failure(FeedlyAccountDelegateError.unexpectedResourceId(id)))
|
2019-09-27 10:13:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
var components = baseUrlComponents
|
|
|
|
components.percentEncodedPath = "/v3/collections/\(encodedId)"
|
|
|
|
|
|
|
|
guard let url = components.url else {
|
|
|
|
fatalError("\(components) does not produce a valid URL.")
|
|
|
|
}
|
|
|
|
|
|
|
|
var request = URLRequest(url: url)
|
|
|
|
request.httpMethod = "DELETE"
|
|
|
|
request.addValue("application/json", forHTTPHeaderField: HTTPRequestHeader.contentType)
|
|
|
|
request.addValue("application/json", forHTTPHeaderField: "Accept-Type")
|
|
|
|
request.addValue("OAuth \(accessToken)", forHTTPHeaderField: HTTPRequestHeader.authorization)
|
|
|
|
|
2019-09-23 09:29:53 +02:00
|
|
|
transport.send(request: request, resultType: String.self, dateDecoding: .millisecondsSince1970, keyDecoding: .convertFromSnakeCase) { result in
|
2019-09-19 04:56:43 +02:00
|
|
|
switch result {
|
|
|
|
case .success(let (httpResponse, _)):
|
|
|
|
if httpResponse.statusCode == 200 {
|
2019-12-15 01:14:55 +01:00
|
|
|
completion(.success(()))
|
2019-09-19 04:56:43 +02:00
|
|
|
} else {
|
2019-12-15 01:14:55 +01:00
|
|
|
completion(.failure(URLError(.cannotDecodeContentData)))
|
2019-10-09 09:38:12 +02:00
|
|
|
}
|
|
|
|
case .failure(let error):
|
2019-12-15 01:14:55 +01:00
|
|
|
completion(.failure(error))
|
2019-10-09 09:38:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-15 01:14:55 +01:00
|
|
|
func removeFeed(_ feedId: String, fromCollectionWith collectionId: String, completion: @escaping (Result<Void, Error>) -> ()) {
|
2019-12-05 20:14:05 +01:00
|
|
|
guard !isSuspended else {
|
|
|
|
return DispatchQueue.main.async {
|
2019-12-15 01:14:55 +01:00
|
|
|
completion(.failure(TransportError.suspended))
|
2019-12-05 20:14:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-09 09:38:12 +02:00
|
|
|
guard let accessToken = credentials?.secret else {
|
|
|
|
return DispatchQueue.main.async {
|
2019-12-15 01:14:55 +01:00
|
|
|
completion(.failure(CredentialsError.incompleteCredentials))
|
2019-10-09 09:38:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-01 21:39:05 +01:00
|
|
|
guard let encodedCollectionId = encodeForURLPath(collectionId) else {
|
2019-10-09 09:38:12 +02:00
|
|
|
return DispatchQueue.main.async {
|
2019-12-15 01:14:55 +01:00
|
|
|
completion(.failure(FeedlyAccountDelegateError.unexpectedResourceId(collectionId)))
|
2019-10-09 09:38:12 +02:00
|
|
|
}
|
|
|
|
}
|
2019-12-01 21:39:05 +01:00
|
|
|
|
|
|
|
guard let encodedFeedId = encodeForURLPath(feedId) else {
|
|
|
|
return DispatchQueue.main.async {
|
2019-12-15 01:14:55 +01:00
|
|
|
completion(.failure(FeedlyAccountDelegateError.unexpectedResourceId(feedId)))
|
2019-12-01 21:39:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-09 09:38:12 +02:00
|
|
|
var components = baseUrlComponents
|
2019-12-01 21:39:05 +01:00
|
|
|
components.percentEncodedPath = "/v3/collections/\(encodedCollectionId)/feeds/\(encodedFeedId)"
|
2019-10-09 09:38:12 +02:00
|
|
|
|
|
|
|
guard let url = components.url else {
|
|
|
|
fatalError("\(components) does not produce a valid URL.")
|
|
|
|
}
|
|
|
|
|
|
|
|
var request = URLRequest(url: url)
|
2019-12-01 21:39:05 +01:00
|
|
|
request.httpMethod = "DELETE"
|
2019-10-09 09:38:12 +02:00
|
|
|
request.addValue("application/json", forHTTPHeaderField: HTTPRequestHeader.contentType)
|
|
|
|
request.addValue("application/json", forHTTPHeaderField: "Accept-Type")
|
|
|
|
request.addValue("OAuth \(accessToken)", forHTTPHeaderField: HTTPRequestHeader.authorization)
|
|
|
|
|
|
|
|
transport.send(request: request, resultType: [FeedlyFeed].self, dateDecoding: .millisecondsSince1970, keyDecoding: .convertFromSnakeCase) { result in
|
|
|
|
switch result {
|
2019-12-01 21:39:05 +01:00
|
|
|
case .success(let httpResponse, _):
|
|
|
|
if httpResponse.statusCode == 200 {
|
2019-12-15 01:14:55 +01:00
|
|
|
completion(.success(()))
|
2019-10-09 09:38:12 +02:00
|
|
|
} else {
|
2019-12-15 01:14:55 +01:00
|
|
|
completion(.failure(URLError(.cannotDecodeContentData)))
|
2019-10-09 09:38:12 +02:00
|
|
|
}
|
|
|
|
case .failure(let error):
|
2019-12-15 01:14:55 +01:00
|
|
|
completion(.failure(error))
|
2019-10-09 09:38:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-12-01 21:39:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
extension FeedlyAPICaller: FeedlyAddFeedToCollectionService {
|
2019-10-09 09:38:12 +02:00
|
|
|
|
2019-12-15 01:14:55 +01:00
|
|
|
func addFeed(with feedId: FeedlyFeedResourceId, title: String? = nil, toCollectionWith collectionId: String, completion: @escaping (Result<[FeedlyFeed], Error>) -> ()) {
|
2019-12-05 20:14:05 +01:00
|
|
|
guard !isSuspended else {
|
|
|
|
return DispatchQueue.main.async {
|
2019-12-15 01:14:55 +01:00
|
|
|
completion(.failure(TransportError.suspended))
|
2019-12-05 20:14:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-09 09:38:12 +02:00
|
|
|
guard let accessToken = credentials?.secret else {
|
|
|
|
return DispatchQueue.main.async {
|
2019-12-15 01:14:55 +01:00
|
|
|
completion(.failure(CredentialsError.incompleteCredentials))
|
2019-10-09 09:38:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-01 21:39:05 +01:00
|
|
|
guard let encodedId = encodeForURLPath(collectionId) else {
|
2019-10-09 09:38:12 +02:00
|
|
|
return DispatchQueue.main.async {
|
2019-12-15 01:14:55 +01:00
|
|
|
completion(.failure(FeedlyAccountDelegateError.unexpectedResourceId(collectionId)))
|
2019-10-09 09:38:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
var components = baseUrlComponents
|
2019-12-01 21:39:05 +01:00
|
|
|
components.percentEncodedPath = "/v3/collections/\(encodedId)/feeds"
|
2019-10-09 09:38:12 +02:00
|
|
|
|
|
|
|
guard let url = components.url else {
|
|
|
|
fatalError("\(components) does not produce a valid URL.")
|
|
|
|
}
|
|
|
|
|
|
|
|
var request = URLRequest(url: url)
|
2019-12-01 21:39:05 +01:00
|
|
|
request.httpMethod = "PUT"
|
2019-10-09 09:38:12 +02:00
|
|
|
request.addValue("application/json", forHTTPHeaderField: HTTPRequestHeader.contentType)
|
|
|
|
request.addValue("application/json", forHTTPHeaderField: "Accept-Type")
|
|
|
|
request.addValue("OAuth \(accessToken)", forHTTPHeaderField: HTTPRequestHeader.authorization)
|
|
|
|
|
2019-12-01 21:39:05 +01:00
|
|
|
do {
|
|
|
|
struct AddFeedBody: Encodable {
|
|
|
|
var id: String
|
|
|
|
var title: String?
|
|
|
|
}
|
|
|
|
let encoder = JSONEncoder()
|
|
|
|
let data = try encoder.encode(AddFeedBody(id: feedId.id, title: title))
|
|
|
|
request.httpBody = data
|
|
|
|
} catch {
|
|
|
|
return DispatchQueue.main.async {
|
2019-12-15 01:14:55 +01:00
|
|
|
completion(.failure(error))
|
2019-12-01 21:39:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-09 09:38:12 +02:00
|
|
|
transport.send(request: request, resultType: [FeedlyFeed].self, dateDecoding: .millisecondsSince1970, keyDecoding: .convertFromSnakeCase) { result in
|
|
|
|
switch result {
|
2019-12-01 21:39:05 +01:00
|
|
|
case .success(_, let collectionFeeds):
|
|
|
|
if let feeds = collectionFeeds {
|
2019-12-15 01:14:55 +01:00
|
|
|
completion(.success(feeds))
|
2019-10-09 09:38:12 +02:00
|
|
|
} else {
|
2019-12-15 01:14:55 +01:00
|
|
|
completion(.failure(URLError(.cannotDecodeContentData)))
|
2019-09-19 04:56:43 +02:00
|
|
|
}
|
|
|
|
case .failure(let error):
|
2019-12-15 01:14:55 +01:00
|
|
|
completion(.failure(error))
|
2019-09-19 04:56:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-09-18 01:18:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
extension FeedlyAPICaller: OAuthAuthorizationCodeGrantRequesting {
|
|
|
|
|
2019-11-07 08:54:41 +01:00
|
|
|
static func authorizationCodeUrlRequest(for request: OAuthAuthorizationRequest, baseUrlComponents: URLComponents) -> URLRequest {
|
|
|
|
var components = baseUrlComponents
|
2019-09-18 01:18:06 +02:00
|
|
|
components.path = "/v3/auth/auth"
|
|
|
|
components.queryItems = request.queryItems
|
|
|
|
|
|
|
|
guard let url = components.url else {
|
2019-11-07 08:54:41 +01:00
|
|
|
assert(components.scheme != nil)
|
|
|
|
assert(components.host != nil)
|
2019-09-18 01:18:06 +02:00
|
|
|
fatalError("\(components) does not produce a valid URL.")
|
|
|
|
}
|
|
|
|
|
|
|
|
var request = URLRequest(url: url)
|
|
|
|
request.addValue("application/json", forHTTPHeaderField: "Accept-Type")
|
|
|
|
|
|
|
|
return request
|
|
|
|
}
|
|
|
|
|
|
|
|
typealias AccessTokenResponse = FeedlyOAuthAccessTokenResponse
|
|
|
|
|
2019-12-15 01:14:55 +01:00
|
|
|
func requestAccessToken(_ authorizationRequest: OAuthAccessTokenRequest, completion: @escaping (Result<FeedlyOAuthAccessTokenResponse, Error>) -> ()) {
|
2019-12-05 20:14:05 +01:00
|
|
|
guard !isSuspended else {
|
|
|
|
return DispatchQueue.main.async {
|
2019-12-15 01:14:55 +01:00
|
|
|
completion(.failure(TransportError.suspended))
|
2019-12-05 20:14:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-18 01:18:06 +02:00
|
|
|
var components = baseUrlComponents
|
|
|
|
components.path = "/v3/auth/token"
|
|
|
|
|
|
|
|
guard let url = components.url else {
|
|
|
|
fatalError("\(components) does not produce a valid URL.")
|
|
|
|
}
|
|
|
|
|
|
|
|
var request = URLRequest(url: url)
|
|
|
|
request.httpMethod = "POST"
|
|
|
|
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
|
|
|
|
request.addValue("application/json", forHTTPHeaderField: "Accept-Type")
|
|
|
|
|
|
|
|
do {
|
|
|
|
let encoder = JSONEncoder()
|
|
|
|
encoder.keyEncodingStrategy = .convertToSnakeCase
|
|
|
|
request.httpBody = try encoder.encode(authorizationRequest)
|
|
|
|
} catch {
|
|
|
|
DispatchQueue.main.async {
|
2019-12-15 01:14:55 +01:00
|
|
|
completion(.failure(error))
|
2019-09-18 01:18:06 +02:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
transport.send(request: request, resultType: AccessTokenResponse.self, keyDecoding: .convertFromSnakeCase) { result in
|
2019-11-07 08:54:41 +01:00
|
|
|
switch result {
|
|
|
|
case .success(let (_, tokenResponse)):
|
|
|
|
if let response = tokenResponse {
|
2019-12-15 01:14:55 +01:00
|
|
|
completion(.success(response))
|
2019-11-07 08:54:41 +01:00
|
|
|
} else {
|
2019-12-15 01:14:55 +01:00
|
|
|
completion(.failure(URLError(.cannotDecodeContentData)))
|
2019-11-07 08:54:41 +01:00
|
|
|
}
|
|
|
|
case .failure(let error):
|
2019-12-15 01:14:55 +01:00
|
|
|
completion(.failure(error))
|
2019-11-07 08:54:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension FeedlyAPICaller: OAuthAcessTokenRefreshRequesting {
|
|
|
|
|
2019-12-15 01:14:55 +01:00
|
|
|
func refreshAccessToken(_ refreshRequest: OAuthRefreshAccessTokenRequest, completion: @escaping (Result<FeedlyOAuthAccessTokenResponse, Error>) -> ()) {
|
2019-12-05 20:14:05 +01:00
|
|
|
guard !isSuspended else {
|
|
|
|
return DispatchQueue.main.async {
|
2019-12-15 01:14:55 +01:00
|
|
|
completion(.failure(TransportError.suspended))
|
2019-12-05 20:14:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-07 08:54:41 +01:00
|
|
|
var components = baseUrlComponents
|
|
|
|
components.path = "/v3/auth/token"
|
|
|
|
|
|
|
|
guard let url = components.url else {
|
|
|
|
fatalError("\(components) does not produce a valid URL.")
|
|
|
|
}
|
|
|
|
|
|
|
|
var request = URLRequest(url: url)
|
|
|
|
request.httpMethod = "POST"
|
|
|
|
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
|
|
|
|
request.addValue("application/json", forHTTPHeaderField: "Accept-Type")
|
|
|
|
|
|
|
|
do {
|
|
|
|
let encoder = JSONEncoder()
|
|
|
|
encoder.keyEncodingStrategy = .convertToSnakeCase
|
|
|
|
request.httpBody = try encoder.encode(refreshRequest)
|
|
|
|
} catch {
|
|
|
|
DispatchQueue.main.async {
|
2019-12-15 01:14:55 +01:00
|
|
|
completion(.failure(error))
|
2019-11-07 08:54:41 +01:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
transport.send(request: request, resultType: AccessTokenResponse.self, keyDecoding: .convertFromSnakeCase) { result in
|
2019-09-18 01:18:06 +02:00
|
|
|
switch result {
|
|
|
|
case .success(let (_, tokenResponse)):
|
|
|
|
if let response = tokenResponse {
|
2019-12-15 01:14:55 +01:00
|
|
|
completion(.success(response))
|
2019-09-18 01:18:06 +02:00
|
|
|
} else {
|
2019-12-15 01:14:55 +01:00
|
|
|
completion(.failure(URLError(.cannotDecodeContentData)))
|
2019-09-18 01:18:06 +02:00
|
|
|
}
|
|
|
|
case .failure(let error):
|
2019-12-15 01:14:55 +01:00
|
|
|
completion(.failure(error))
|
2019-09-18 01:18:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-10-18 23:21:02 +02:00
|
|
|
|
|
|
|
extension FeedlyAPICaller: FeedlyGetCollectionsService {
|
|
|
|
|
2019-12-15 01:14:55 +01:00
|
|
|
func getCollections(completion: @escaping (Result<[FeedlyCollection], Error>) -> ()) {
|
2019-12-05 20:14:05 +01:00
|
|
|
guard !isSuspended else {
|
|
|
|
return DispatchQueue.main.async {
|
2019-12-15 01:14:55 +01:00
|
|
|
completion(.failure(TransportError.suspended))
|
2019-12-05 20:14:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-18 23:21:02 +02:00
|
|
|
guard let accessToken = credentials?.secret else {
|
|
|
|
return DispatchQueue.main.async {
|
2019-12-15 01:14:55 +01:00
|
|
|
completion(.failure(CredentialsError.incompleteCredentials))
|
2019-10-18 23:21:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
var components = baseUrlComponents
|
|
|
|
components.path = "/v3/collections"
|
|
|
|
|
|
|
|
guard let url = components.url else {
|
|
|
|
fatalError("\(components) does not produce a valid URL.")
|
|
|
|
}
|
|
|
|
|
|
|
|
var request = URLRequest(url: url)
|
|
|
|
request.addValue("application/json", forHTTPHeaderField: HTTPRequestHeader.contentType)
|
|
|
|
request.addValue("application/json", forHTTPHeaderField: "Accept-Type")
|
|
|
|
request.addValue("OAuth \(accessToken)", forHTTPHeaderField: HTTPRequestHeader.authorization)
|
|
|
|
|
|
|
|
transport.send(request: request, resultType: [FeedlyCollection].self, dateDecoding: .millisecondsSince1970, keyDecoding: .convertFromSnakeCase) { result in
|
|
|
|
switch result {
|
|
|
|
case .success(let (_, collections)):
|
|
|
|
if let response = collections {
|
2019-12-15 01:14:55 +01:00
|
|
|
completion(.success(response))
|
2019-10-18 23:21:02 +02:00
|
|
|
} else {
|
2019-12-15 01:14:55 +01:00
|
|
|
completion(.failure(URLError(.cannotDecodeContentData)))
|
2019-10-18 23:21:02 +02:00
|
|
|
}
|
|
|
|
case .failure(let error):
|
2019-12-15 01:14:55 +01:00
|
|
|
completion(.failure(error))
|
2019-10-18 23:21:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension FeedlyAPICaller: FeedlyGetStreamContentsService {
|
|
|
|
|
2019-12-15 01:14:55 +01:00
|
|
|
func getStreamContents(for resource: FeedlyResourceId, continuation: String? = nil, newerThan: Date?, unreadOnly: Bool?, completion: @escaping (Result<FeedlyStream, Error>) -> ()) {
|
2019-12-05 20:14:05 +01:00
|
|
|
guard !isSuspended else {
|
|
|
|
return DispatchQueue.main.async {
|
2019-12-15 01:14:55 +01:00
|
|
|
completion(.failure(TransportError.suspended))
|
2019-12-05 20:14:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-18 23:21:02 +02:00
|
|
|
guard let accessToken = credentials?.secret else {
|
|
|
|
return DispatchQueue.main.async {
|
2019-12-15 01:14:55 +01:00
|
|
|
completion(.failure(CredentialsError.incompleteCredentials))
|
2019-10-18 23:21:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var components = baseUrlComponents
|
|
|
|
components.path = "/v3/streams/contents"
|
|
|
|
|
|
|
|
var queryItems = [URLQueryItem]()
|
|
|
|
|
|
|
|
if let date = newerThan {
|
|
|
|
let value = String(Int(date.timeIntervalSince1970 * 1000))
|
|
|
|
let queryItem = URLQueryItem(name: "newerThan", value: value)
|
|
|
|
queryItems.append(queryItem)
|
|
|
|
}
|
|
|
|
|
|
|
|
if let flag = unreadOnly {
|
|
|
|
let value = flag ? "true" : "false"
|
|
|
|
let queryItem = URLQueryItem(name: "unreadOnly", value: value)
|
|
|
|
queryItems.append(queryItem)
|
|
|
|
}
|
|
|
|
|
|
|
|
if let value = continuation, !value.isEmpty {
|
|
|
|
let queryItem = URLQueryItem(name: "continuation", value: value)
|
|
|
|
queryItems.append(queryItem)
|
|
|
|
}
|
|
|
|
|
|
|
|
queryItems.append(contentsOf: [
|
|
|
|
URLQueryItem(name: "count", value: "1000"),
|
|
|
|
URLQueryItem(name: "streamId", value: resource.id),
|
|
|
|
])
|
|
|
|
|
|
|
|
components.queryItems = queryItems
|
|
|
|
|
|
|
|
guard let url = components.url else {
|
|
|
|
fatalError("\(components) does not produce a valid URL.")
|
|
|
|
}
|
|
|
|
|
|
|
|
var request = URLRequest(url: url)
|
|
|
|
request.addValue("application/json", forHTTPHeaderField: HTTPRequestHeader.contentType)
|
|
|
|
request.addValue("application/json", forHTTPHeaderField: "Accept-Type")
|
|
|
|
request.addValue("OAuth \(accessToken)", forHTTPHeaderField: HTTPRequestHeader.authorization)
|
|
|
|
|
|
|
|
transport.send(request: request, resultType: FeedlyStream.self, dateDecoding: .millisecondsSince1970, keyDecoding: .convertFromSnakeCase) { result in
|
|
|
|
switch result {
|
|
|
|
case .success(let (_, collections)):
|
|
|
|
if let response = collections {
|
2019-12-15 01:14:55 +01:00
|
|
|
completion(.success(response))
|
2019-10-18 23:21:02 +02:00
|
|
|
} else {
|
2019-12-15 01:14:55 +01:00
|
|
|
completion(.failure(URLError(.cannotDecodeContentData)))
|
2019-10-18 23:21:02 +02:00
|
|
|
}
|
|
|
|
case .failure(let error):
|
2019-12-15 01:14:55 +01:00
|
|
|
completion(.failure(error))
|
2019-10-18 23:21:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension FeedlyAPICaller: FeedlyGetStreamIdsService {
|
|
|
|
|
2019-12-15 01:14:55 +01:00
|
|
|
func getStreamIds(for resource: FeedlyResourceId, continuation: String? = nil, newerThan: Date?, unreadOnly: Bool?, completion: @escaping (Result<FeedlyStreamIds, Error>) -> ()) {
|
2019-12-05 20:14:05 +01:00
|
|
|
guard !isSuspended else {
|
|
|
|
return DispatchQueue.main.async {
|
2019-12-15 01:14:55 +01:00
|
|
|
completion(.failure(TransportError.suspended))
|
2019-12-05 20:14:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-18 23:21:02 +02:00
|
|
|
guard let accessToken = credentials?.secret else {
|
|
|
|
return DispatchQueue.main.async {
|
2019-12-15 01:14:55 +01:00
|
|
|
completion(.failure(CredentialsError.incompleteCredentials))
|
2019-10-18 23:21:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var components = baseUrlComponents
|
|
|
|
components.path = "/v3/streams/ids"
|
|
|
|
|
|
|
|
var queryItems = [URLQueryItem]()
|
|
|
|
|
|
|
|
if let date = newerThan {
|
|
|
|
let value = String(Int(date.timeIntervalSince1970 * 1000))
|
|
|
|
let queryItem = URLQueryItem(name: "newerThan", value: value)
|
|
|
|
queryItems.append(queryItem)
|
|
|
|
}
|
|
|
|
|
|
|
|
if let flag = unreadOnly {
|
|
|
|
let value = flag ? "true" : "false"
|
|
|
|
let queryItem = URLQueryItem(name: "unreadOnly", value: value)
|
|
|
|
queryItems.append(queryItem)
|
|
|
|
}
|
|
|
|
|
|
|
|
if let value = continuation, !value.isEmpty {
|
|
|
|
let queryItem = URLQueryItem(name: "continuation", value: value)
|
|
|
|
queryItems.append(queryItem)
|
|
|
|
}
|
|
|
|
|
|
|
|
queryItems.append(contentsOf: [
|
2020-01-09 06:24:47 +01:00
|
|
|
URLQueryItem(name: "count", value: "10000"),
|
2019-10-18 23:21:02 +02:00
|
|
|
URLQueryItem(name: "streamId", value: resource.id),
|
|
|
|
])
|
|
|
|
|
|
|
|
components.queryItems = queryItems
|
|
|
|
|
|
|
|
guard let url = components.url else {
|
|
|
|
fatalError("\(components) does not produce a valid URL.")
|
|
|
|
}
|
|
|
|
|
|
|
|
var request = URLRequest(url: url)
|
|
|
|
request.addValue("application/json", forHTTPHeaderField: HTTPRequestHeader.contentType)
|
|
|
|
request.addValue("application/json", forHTTPHeaderField: "Accept-Type")
|
|
|
|
request.addValue("OAuth \(accessToken)", forHTTPHeaderField: HTTPRequestHeader.authorization)
|
|
|
|
|
|
|
|
transport.send(request: request, resultType: FeedlyStreamIds.self, dateDecoding: .millisecondsSince1970, keyDecoding: .convertFromSnakeCase) { result in
|
|
|
|
switch result {
|
|
|
|
case .success(let (_, collections)):
|
|
|
|
if let response = collections {
|
2019-12-15 01:14:55 +01:00
|
|
|
completion(.success(response))
|
2019-10-18 23:21:02 +02:00
|
|
|
} else {
|
2019-12-15 01:14:55 +01:00
|
|
|
completion(.failure(URLError(.cannotDecodeContentData)))
|
2019-10-18 23:21:02 +02:00
|
|
|
}
|
|
|
|
case .failure(let error):
|
2019-12-15 01:14:55 +01:00
|
|
|
completion(.failure(error))
|
2019-10-18 23:21:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension FeedlyAPICaller: FeedlyGetEntriesService {
|
|
|
|
|
2019-12-15 01:14:55 +01:00
|
|
|
func getEntries(for ids: Set<String>, completion: @escaping (Result<[FeedlyEntry], Error>) -> ()) {
|
2019-12-05 20:14:05 +01:00
|
|
|
guard !isSuspended else {
|
|
|
|
return DispatchQueue.main.async {
|
2019-12-15 01:14:55 +01:00
|
|
|
completion(.failure(TransportError.suspended))
|
2019-12-05 20:14:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-18 23:21:02 +02:00
|
|
|
guard let accessToken = credentials?.secret else {
|
|
|
|
return DispatchQueue.main.async {
|
2019-12-15 01:14:55 +01:00
|
|
|
completion(.failure(CredentialsError.incompleteCredentials))
|
2019-10-18 23:21:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var components = baseUrlComponents
|
|
|
|
components.path = "/v3/entries/.mget"
|
|
|
|
|
|
|
|
guard let url = components.url else {
|
|
|
|
fatalError("\(components) does not produce a valid URL.")
|
|
|
|
}
|
|
|
|
|
|
|
|
var request = URLRequest(url: url)
|
|
|
|
|
|
|
|
do {
|
|
|
|
let body = Array(ids)
|
|
|
|
let encoder = JSONEncoder()
|
|
|
|
let data = try encoder.encode(body)
|
|
|
|
request.httpBody = data
|
|
|
|
} catch {
|
|
|
|
return DispatchQueue.main.async {
|
2019-12-15 01:14:55 +01:00
|
|
|
completion(.failure(error))
|
2019-10-18 23:21:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
request.httpMethod = "POST"
|
|
|
|
request.addValue("application/json", forHTTPHeaderField: HTTPRequestHeader.contentType)
|
|
|
|
request.addValue("application/json", forHTTPHeaderField: "Accept-Type")
|
|
|
|
request.addValue("OAuth \(accessToken)", forHTTPHeaderField: HTTPRequestHeader.authorization)
|
|
|
|
|
|
|
|
transport.send(request: request, resultType: [FeedlyEntry].self, dateDecoding: .millisecondsSince1970, keyDecoding: .convertFromSnakeCase) { result in
|
|
|
|
switch result {
|
|
|
|
case .success(let (_, entries)):
|
|
|
|
if let response = entries {
|
2019-12-15 01:14:55 +01:00
|
|
|
completion(.success(response))
|
2019-10-18 23:21:02 +02:00
|
|
|
} else {
|
2019-12-15 01:14:55 +01:00
|
|
|
completion(.failure(URLError(.cannotDecodeContentData)))
|
2019-10-18 23:21:02 +02:00
|
|
|
}
|
|
|
|
case .failure(let error):
|
2019-12-15 01:14:55 +01:00
|
|
|
completion(.failure(error))
|
2019-10-18 23:21:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension FeedlyAPICaller: FeedlyMarkArticlesService {
|
|
|
|
|
|
|
|
private struct MarkerEntriesBody: Encodable {
|
|
|
|
let type = "entries"
|
|
|
|
var action: String
|
|
|
|
var entryIds: [String]
|
|
|
|
}
|
|
|
|
|
2019-12-15 01:14:55 +01:00
|
|
|
func mark(_ articleIds: Set<String>, as action: FeedlyMarkAction, completion: @escaping (Result<Void, Error>) -> ()) {
|
2019-12-05 20:14:05 +01:00
|
|
|
guard !isSuspended else {
|
|
|
|
return DispatchQueue.main.async {
|
2019-12-15 01:14:55 +01:00
|
|
|
completion(.failure(TransportError.suspended))
|
2019-12-05 20:14:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-18 23:21:02 +02:00
|
|
|
guard let accessToken = credentials?.secret else {
|
|
|
|
return DispatchQueue.main.async {
|
2019-12-15 01:14:55 +01:00
|
|
|
completion(.failure(CredentialsError.incompleteCredentials))
|
2019-10-18 23:21:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
var components = baseUrlComponents
|
|
|
|
components.path = "/v3/markers"
|
|
|
|
|
|
|
|
guard let url = components.url else {
|
|
|
|
fatalError("\(components) does not produce a valid URL.")
|
|
|
|
}
|
|
|
|
|
|
|
|
var request = URLRequest(url: url)
|
|
|
|
request.httpMethod = "POST"
|
|
|
|
request.addValue("application/json", forHTTPHeaderField: HTTPRequestHeader.contentType)
|
|
|
|
request.addValue("application/json", forHTTPHeaderField: "Accept-Type")
|
|
|
|
request.addValue("OAuth \(accessToken)", forHTTPHeaderField: HTTPRequestHeader.authorization)
|
|
|
|
|
|
|
|
do {
|
|
|
|
let body = MarkerEntriesBody(action: action.actionValue, entryIds: Array(articleIds))
|
|
|
|
let encoder = JSONEncoder()
|
|
|
|
let data = try encoder.encode(body)
|
|
|
|
request.httpBody = data
|
|
|
|
} catch {
|
|
|
|
return DispatchQueue.main.async {
|
2019-12-15 01:14:55 +01:00
|
|
|
completion(.failure(error))
|
2019-10-18 23:21:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
transport.send(request: request, resultType: String.self, dateDecoding: .millisecondsSince1970, keyDecoding: .convertFromSnakeCase) { result in
|
|
|
|
switch result {
|
|
|
|
case .success(let (httpResponse, _)):
|
|
|
|
if httpResponse.statusCode == 200 {
|
2019-12-15 01:14:55 +01:00
|
|
|
completion(.success(()))
|
2019-10-18 23:21:02 +02:00
|
|
|
} else {
|
2019-12-15 01:14:55 +01:00
|
|
|
completion(.failure(URLError(.cannotDecodeContentData)))
|
2019-10-18 23:21:02 +02:00
|
|
|
}
|
|
|
|
case .failure(let error):
|
2019-12-15 01:14:55 +01:00
|
|
|
completion(.failure(error))
|
2019-10-18 23:21:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-11-15 09:09:14 +01:00
|
|
|
|
2019-12-01 21:39:05 +01:00
|
|
|
extension FeedlyAPICaller: FeedlySearchService {
|
|
|
|
|
2019-12-15 01:14:55 +01:00
|
|
|
func getFeeds(for query: String, count: Int, locale: String, completion: @escaping (Result<FeedlyFeedsSearchResponse, Error>) -> ()) {
|
2019-12-01 21:39:05 +01:00
|
|
|
|
2019-12-05 20:14:05 +01:00
|
|
|
guard !isSuspended else {
|
|
|
|
return DispatchQueue.main.async {
|
2019-12-15 01:14:55 +01:00
|
|
|
completion(.failure(TransportError.suspended))
|
2019-12-05 20:14:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-01 21:39:05 +01:00
|
|
|
var components = baseUrlComponents
|
|
|
|
components.path = "/v3/search/feeds"
|
|
|
|
|
|
|
|
components.queryItems = [
|
|
|
|
URLQueryItem(name: "query", value: query),
|
|
|
|
URLQueryItem(name: "count", value: String(count)),
|
|
|
|
URLQueryItem(name: "locale", value: locale)
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
guard let url = components.url else {
|
|
|
|
fatalError("\(components) does not produce a valid URL.")
|
|
|
|
}
|
|
|
|
|
|
|
|
var request = URLRequest(url: url)
|
|
|
|
request.httpMethod = "GET"
|
|
|
|
request.addValue("application/json", forHTTPHeaderField: HTTPRequestHeader.contentType)
|
|
|
|
request.addValue("application/json", forHTTPHeaderField: "Accept-Type")
|
|
|
|
|
|
|
|
transport.send(request: request, resultType: FeedlyFeedsSearchResponse.self, dateDecoding: .millisecondsSince1970, keyDecoding: .convertFromSnakeCase) { result in
|
|
|
|
switch result {
|
|
|
|
case .success(let (_, searchResponse)):
|
|
|
|
if let response = searchResponse {
|
2019-12-15 01:14:55 +01:00
|
|
|
completion(.success(response))
|
2019-12-01 21:39:05 +01:00
|
|
|
} else {
|
2019-12-15 01:14:55 +01:00
|
|
|
completion(.failure(URLError(.cannotDecodeContentData)))
|
2019-12-01 21:39:05 +01:00
|
|
|
}
|
|
|
|
case .failure(let error):
|
2019-12-15 01:14:55 +01:00
|
|
|
completion(.failure(error))
|
2019-12-01 21:39:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-15 09:09:14 +01:00
|
|
|
extension FeedlyAPICaller: FeedlyLogoutService {
|
|
|
|
|
2019-12-15 01:14:55 +01:00
|
|
|
func logout(completion: @escaping (Result<Void, Error>) -> ()) {
|
2019-12-05 20:14:05 +01:00
|
|
|
guard !isSuspended else {
|
|
|
|
return DispatchQueue.main.async {
|
2019-12-15 01:14:55 +01:00
|
|
|
completion(.failure(TransportError.suspended))
|
2019-12-05 20:14:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-15 09:09:14 +01:00
|
|
|
guard let accessToken = credentials?.secret else {
|
|
|
|
return DispatchQueue.main.async {
|
2019-12-15 01:14:55 +01:00
|
|
|
completion(.failure(CredentialsError.incompleteCredentials))
|
2019-11-15 09:09:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
var components = baseUrlComponents
|
|
|
|
components.path = "/v3/auth/logout"
|
|
|
|
|
|
|
|
guard let url = components.url else {
|
|
|
|
fatalError("\(components) does not produce a valid URL.")
|
|
|
|
}
|
|
|
|
|
|
|
|
var request = URLRequest(url: url)
|
|
|
|
request.httpMethod = "POST"
|
|
|
|
request.addValue("application/json", forHTTPHeaderField: HTTPRequestHeader.contentType)
|
|
|
|
request.addValue("application/json", forHTTPHeaderField: "Accept-Type")
|
|
|
|
request.addValue("OAuth \(accessToken)", forHTTPHeaderField: HTTPRequestHeader.authorization)
|
|
|
|
|
|
|
|
transport.send(request: request, resultType: String.self, dateDecoding: .millisecondsSince1970, keyDecoding: .convertFromSnakeCase) { result in
|
|
|
|
switch result {
|
|
|
|
case .success(let (httpResponse, _)):
|
|
|
|
if httpResponse.statusCode == 200 {
|
2019-12-15 01:14:55 +01:00
|
|
|
completion(.success(()))
|
2019-11-15 09:09:14 +01:00
|
|
|
} else {
|
2019-12-15 01:14:55 +01:00
|
|
|
completion(.failure(URLError(.cannotDecodeContentData)))
|
2019-11-15 09:09:14 +01:00
|
|
|
}
|
|
|
|
case .failure(let error):
|
2019-12-15 01:14:55 +01:00
|
|
|
completion(.failure(error))
|
2019-11-15 09:09:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|