-
Hi sbertix, hope you have had a great weekend. I was wondering if you are ever planning to make videos that explaining Swiftagram. I am very new to Swift and am having difficulties understanding how Swiftagram works so sorry for a newbie question. My main question was if you could show me and example where you use the secret to comment on a post. Thanks in advance. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 7 replies
-
Hey @yusupm, I did indeed have a great weekend, thx man 😊 Hope you had one too.
I never actually thought about it. I mean it would be nice having like introductory video-tutorials, but I'm honestly not sure I have the time to actually go through with it.
Sure. I'm gonna assume you're using Combine, and that you don't already know the import Combine
import Foundation
import Swiftagram
/// A `class` we're defining just to hold reference to all calls.
/// This is not necessary is just to make sure you understand
/// `bin` has to be retained.
final class Instagram {
/// A shared instance of `Instagram`.
static let `default`: Instagram = .init()
/// The dispose bag.
private var bin: Set<AnyCancellable> = []
/// Init.
private init() { }
/// Comment "Amazing picture" on Instagram's last post.
///
/// - parameter secret: A valid `Secret`.
func comment(with secret: Secret) {
// Return all users matching the query
// `Instagram`.
Endpoint.User.all(matching: "Instagram")
.unlocking(with: secret)
.publish()
.prefix(1) // Fetch one page only.
.compactMap(\.users?.first?.identifier) // @instagram has to be the first one.
.flatMap { instagramUserIdentifier in
// Request one page of the most recent
// posts made by @instagram.
Endpoint.Media.Posts.owned(by: instagramUserIdentifier)
.unlocking(with: secret)
.publish()
.prefix(1) // One page is more than enough as we only need one post.
.compactMap(\.media?.first?.identifier) // Get the identifier for the most recent one.
}
.flatMap { mostRecentInstagramPostIdentifier in
// Post a comment.
Endpoint.Media.Posts.comment("Amazing picture", on: mostRecentInstagramPostIdentifier)
.unlocking(with: secret)
.publish()
}
.sink(receiveCompletion: { if case .failure(let error) = $0 { print(error.localizedDescription) }},
receiveValue: { if let error = $0.error { print(error.localizedDescription) }})
.store(in: &bin)
}
/// Comment "Amazing picture" on Instagram's last post.
///
/// - parameter secret: An optional `Secret`.
func comment(with secret: Secret?) {
guard let secret = secret else { return }
comment(with: secret)
}
} This way you can do |
Beta Was this translation helpful? Give feedback.
Hey @yusupm,
I did indeed have a great weekend, thx man 😊 Hope you had one too.
I never actually thought about it. I mean it would be nice having like introductory video-tutorials, but I'm honestly not sure I have the time to actually go through with it.
It's more likely I'll just write more examples and explanations (it's already planned for
5.0.0
).Sure. I'm gonna assume you're using Combine, and that you don't already know the
identifier
of the media you wanna comment on.I'll just assume, for simpli…