From d3c168a12e286f1b92339de815693899ad057c02 Mon Sep 17 00:00:00 2001 From: Jonathan Bennett Date: Wed, 20 Nov 2019 17:26:27 -0500 Subject: [PATCH] use add_feed_and_wait endpoint The add_feed endpoint does not return feed info. The _and_wait endpoint can be slower (up to 10 seconds) but will make sure we gett the right URL if available. --- .../Account/Account.xcodeproj/project.pbxproj | 4 ++++ .../FeedWrangler/FeedWranglerAPICaller.swift | 24 ++++++++++++------- .../FeedWranglerAccountDelegate.swift | 4 ++-- .../FeedWranglerSubscriptionResult.swift | 18 ++++++++++++++ 4 files changed, 40 insertions(+), 10 deletions(-) create mode 100644 Frameworks/Account/FeedWrangler/FeedWranglerSubscriptionResult.swift diff --git a/Frameworks/Account/Account.xcodeproj/project.pbxproj b/Frameworks/Account/Account.xcodeproj/project.pbxproj index 389f756a5..63585f829 100644 --- a/Frameworks/Account/Account.xcodeproj/project.pbxproj +++ b/Frameworks/Account/Account.xcodeproj/project.pbxproj @@ -16,6 +16,7 @@ 3B826DAD2385C81C00FC1ADB /* FeedWranglerFeedItemsRequest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3B826DA42385C81C00FC1ADB /* FeedWranglerFeedItemsRequest.swift */; }; 3B826DAE2385C81C00FC1ADB /* FeedWranglerSubscriptionsRequest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3B826DA52385C81C00FC1ADB /* FeedWranglerSubscriptionsRequest.swift */; }; 3B826DAF2385C81C00FC1ADB /* FeedWranglerGenericResult.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3B826DA62385C81C00FC1ADB /* FeedWranglerGenericResult.swift */; }; + 3BC23AB92385ECB100371CBA /* FeedWranglerSubscriptionResult.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3BC23AB82385ECB100371CBA /* FeedWranglerSubscriptionResult.swift */; }; 5107A099227DE42E00C7C3C5 /* AccountCredentialsTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5107A098227DE42E00C7C3C5 /* AccountCredentialsTest.swift */; }; 5107A09B227DE49500C7C3C5 /* TestAccountManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5107A09A227DE49500C7C3C5 /* TestAccountManager.swift */; }; 5107A09D227DE77700C7C3C5 /* TestTransport.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5107A09C227DE77700C7C3C5 /* TestTransport.swift */; }; @@ -226,6 +227,7 @@ 3B826DA42385C81C00FC1ADB /* FeedWranglerFeedItemsRequest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FeedWranglerFeedItemsRequest.swift; sourceTree = ""; }; 3B826DA52385C81C00FC1ADB /* FeedWranglerSubscriptionsRequest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FeedWranglerSubscriptionsRequest.swift; sourceTree = ""; }; 3B826DA62385C81C00FC1ADB /* FeedWranglerGenericResult.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FeedWranglerGenericResult.swift; sourceTree = ""; }; + 3BC23AB82385ECB100371CBA /* FeedWranglerSubscriptionResult.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FeedWranglerSubscriptionResult.swift; sourceTree = ""; }; 5107A098227DE42E00C7C3C5 /* AccountCredentialsTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AccountCredentialsTest.swift; sourceTree = ""; }; 5107A09A227DE49500C7C3C5 /* TestAccountManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TestAccountManager.swift; sourceTree = ""; }; 5107A09C227DE77700C7C3C5 /* TestTransport.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TestTransport.swift; sourceTree = ""; }; @@ -428,6 +430,7 @@ 3B826DA42385C81C00FC1ADB /* FeedWranglerFeedItemsRequest.swift */, 3B826DA52385C81C00FC1ADB /* FeedWranglerSubscriptionsRequest.swift */, 3B826DA62385C81C00FC1ADB /* FeedWranglerGenericResult.swift */, + 3BC23AB82385ECB100371CBA /* FeedWranglerSubscriptionResult.swift */, ); path = FeedWrangler; sourceTree = ""; @@ -1079,6 +1082,7 @@ 9EAEC624233315F60085D7C9 /* FeedlyEntry.swift in Sources */, 9EEAE073235D01AE00E3FEE4 /* FeedlyGetStreamIdsService.swift in Sources */, 9EEEF7212355277F009E9D80 /* FeedlySyncStarredArticlesOperation.swift in Sources */, + 3BC23AB92385ECB100371CBA /* FeedWranglerSubscriptionResult.swift in Sources */, 5144EA49227B497600D19003 /* FeedbinAPICaller.swift in Sources */, 84B99C9F1FAE8D3200ECDEDB /* ContainerPath.swift in Sources */, 9E510D6E234F16A8002E6F1A /* FeedlyAddFeedRequest.swift in Sources */, diff --git a/Frameworks/Account/FeedWrangler/FeedWranglerAPICaller.swift b/Frameworks/Account/FeedWrangler/FeedWranglerAPICaller.swift index 139d51ca5..20f01e51b 100644 --- a/Frameworks/Account/FeedWrangler/FeedWranglerAPICaller.swift +++ b/Frameworks/Account/FeedWrangler/FeedWranglerAPICaller.swift @@ -65,20 +65,28 @@ final class FeedWranglerAPICaller: NSObject { } } - func addSubscription(url: String, completion: @escaping (Result) -> Void) { + func addSubscription(url: String, completion: @escaping (Result) -> Void) { let url = FeedWranglerConfig .clientURL - .appendingPathComponent("subscriptions/add_feed") - .appendingQueryItem(URLQueryItem(name: "feed_url", value: url)) - - standardSend(url: url, resultType: FeedWranglerGenericResult.self) { result in + .appendingPathComponent("subscriptions/add_feed_and_wait") + .appendingQueryItems([ + URLQueryItem(name: "feed_url", value: url), + URLQueryItem(name: "choose_first", value: "true") + ]) + + standardSend(url: url, resultType: FeedWranglerSubscriptionResult.self) { result in switch result { case .success(let (_, results)): - if let error = results?.error { - completion(.failure(FeedWranglerError.general(message: error))) + if let results = results { + if let error = results.error { + completion(.failure(FeedWranglerError.general(message: error))) + } else { + completion(.success(results.feed)) + } } else { - completion(.success(())) + completion(.failure(FeedWranglerError.general(message: "No feed found"))) } + case .failure(let error): completion(.failure(error)) diff --git a/Frameworks/Account/FeedWrangler/FeedWranglerAccountDelegate.swift b/Frameworks/Account/FeedWrangler/FeedWranglerAccountDelegate.swift index 02e59b02c..78a66fb9b 100644 --- a/Frameworks/Account/FeedWrangler/FeedWranglerAccountDelegate.swift +++ b/Frameworks/Account/FeedWrangler/FeedWranglerAccountDelegate.swift @@ -284,8 +284,8 @@ final class FeedWranglerAccountDelegate: AccountDelegate { self.refreshProgress.completeTask() switch result { - case .success: - let feed = account.createWebFeed(with: name, url: url, webFeedID: url, homePageURL: url) + case .success(let subscription): + let feed = account.createWebFeed(with: subscription.title, url: subscription.feedURL, webFeedID: String(subscription.feedID), homePageURL: subscription.siteURL) completion(.success(feed)) case .failure(let error): diff --git a/Frameworks/Account/FeedWrangler/FeedWranglerSubscriptionResult.swift b/Frameworks/Account/FeedWrangler/FeedWranglerSubscriptionResult.swift new file mode 100644 index 000000000..2bd21bbee --- /dev/null +++ b/Frameworks/Account/FeedWrangler/FeedWranglerSubscriptionResult.swift @@ -0,0 +1,18 @@ +// +// FeedWranglerSubscriptionResult.swift +// Account +// +// Created by Jonathan Bennett on 2019-11-20. +// Copyright © 2019 Ranchero Software, LLC. All rights reserved. +// + +import Foundation + +struct FeedWranglerSubscriptionResult: Hashable, Codable { + + let feed: FeedWranglerSubscription + let error: String? + let result: String + +} +