Skip to content

Commit

Permalink
Refactor Endpoints to use Projectables
Browse files Browse the repository at this point in the history
  • Loading branch information
sbertix committed Feb 23, 2021
1 parent b5b36ec commit 1689ebf
Show file tree
Hide file tree
Showing 23 changed files with 475 additions and 437 deletions.
5 changes: 2 additions & 3 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,12 @@ let package = Package(
targets: ["SwiftagramCrypto"])],
// Package dependencies.
dependencies: [.package(url: "https://github.com/sbertix/ComposableRequest.git", .branch("development")),
.package(url: "https://github.com/sbertix/SwCrypt.git", .upToNextMinor(from: "5.1.0")),
.package(url: "https://github.com/sbertix/Swiftchain.git", .upToNextMinor(from: "1.0.0"))],
.package(url: "https://github.com/sbertix/SwCrypt.git", .upToNextMinor(from: "5.1.0"))],
// All targets.
targets: [.target(name: "Swiftagram",
dependencies: ["ComposableRequest", "ComposableStorage"]),
.target(name: "SwiftagramCrypto",
dependencies: ["Swiftagram", "SwCrypt", "Swiftchain"]),
dependencies: ["ComposableStorageCrypto", "SwCrypt", "Swiftagram"]),
.testTarget(name: "SwiftagramTests",
dependencies: ["Swiftagram", "SwiftagramCrypto"])]
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public final class WebViewAuthenticator<Storage: ComposableStorage.Storage>: Aut
}

@available(iOS 11.0, macOS 10.13, macCatalyst 13.0, *)
public extension WebViewAuthenticator where Storage == ComposableStorage.TransientStorage<Secret> {
public extension WebViewAuthenticator where Storage == TransientStorage<Secret> {
/// Init.
///
/// - parameters:
Expand Down
38 changes: 17 additions & 21 deletions Sources/Swiftagram/Endpoints/EndpointDirect.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,35 +16,31 @@ public extension Endpoint {
private static let base = Endpoint.version1.direct_v2.appendingDefaultHeader()

/// Fetch all threads.
public static var inbox: Paginated<Conversation.Collection, RankedPageReference<String, String>?> {
public static var inbox: Paginated<Conversation.Collection, String?> {
.init { secret, session, pages in
// Persist the rank token.
let rank = pages.offset?.rank ?? String(Int.random(in: 1_000..<10_000))
// Prepare the actual pager.
return Pager(pages) { _, next, _ in
Projectables.Pager(pages) { _, next, _ in
base.inbox
.header(appending: secret.header)
.query(appending: ["visual_message_return_type": "unseen",
"direction": next.flatMap { _ in "older" },
"cursor": next,
"thread_message_limit": "10",
"persistent_badging": "true",
"limit": "20",
"seq_id": rank])
.session(session)
"limit": "20"])
.project(session)
.map(\.data)
.wrap()
.map(Conversation.Collection.init)
}
.eraseToAnyObservable()
.observe(on: session.scheduler)
.eraseToAnyObservable()
}
}

/// All pending threads.
public static var pendingInbox: Paginated<Conversation.Collection, String?> {
.init { secret, session, pages in
Pager(pages) { _, next, _ in
Projectables.Pager(pages) { _, next, _ in
base.path(appending: "pending_inbox")
.header(appending: secret.header)
.query(appending: ["visual_message_return_type": "unseen",
Expand All @@ -53,13 +49,13 @@ public extension Endpoint {
"thread_message_limit": "10",
"persistent_badging": "true",
"limit": "20"])
.session(session)
.project(session)
.map(\.data)
.wrap()
.map(Conversation.Collection.init)
}
.eraseToAnyObservable()
.observe(on: session.scheduler)
.eraseToAnyObservable()
}
}

Expand All @@ -68,19 +64,19 @@ public extension Endpoint {
/// - parameter query: An optional `String`.
public static func recipients(matching query: String? = nil) -> Disposable<Recipient.Collection> {
.init { secret, session in
Deferred {
Projectables.Deferred {
base.path(appending: "ranked_recipients/")
.header(appending: secret.header)
.header(appending: ["mode": "raven",
"query": query,
"show_threads": "true"])
.session(session)
.project(session)
.map(\.data)
.wrap()
.map(Recipient.Collection.init)
}
.eraseToAnyObservable()
.observe(on: session.scheduler)
.eraseToAnyObservable()
}
}

Expand All @@ -89,36 +85,36 @@ public extension Endpoint {
/// - parameter identifier: A `String` holding reference to a valid thread identifier.
public static func conversation(matching identifier: String) -> Paginated<Conversation.Unit, String?> {
.init { secret, session, pages in
Pager(pages) { _, next, _ in
Projectables.Pager(pages) { _, next, _ in
base.threads
.path(appending: identifier)
.header(appending: secret.header)
.query(appending: ["visual_message_return_type": "unseen",
"direction": next.flatMap { _ in "older" },
"cursor": next,
"limit": "20"])
.session(session)
.project(session)
.map(\.data)
.wrap()
.map(Conversation.Unit.init)
}
.eraseToAnyObservable()
.observe(on: session.scheduler)
.eraseToAnyObservable()
}
}

/// Get user presence.
public static var presence: Disposable<Wrapper> {
.init { secret, session in
Deferred {
Projectables.Deferred {
base.path(appending: "get_presence/")
.header(appending: secret.header)
.session(session)
.project(session)
.map(\.data)
.wrap()
}
.eraseToAnyObservable()
.observe(on: session.scheduler)
.eraseToAnyObservable()
}
}
}
Expand Down
18 changes: 9 additions & 9 deletions Sources/Swiftagram/Endpoints/EndpointDiscover.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,33 +20,33 @@ public extension Endpoint {
/// - parameter identifier: A `String` holding reference to a valid user identifier.
public static func users(like identifier: String) -> Disposable<Swiftagram.User.Collection> {
.init { secret, session in
Deferred {
Projectables.Deferred {
base.chaining
.query(appending: identifier, forKey: "target_id")
.header(appending: secret.header)
.session(session)
.project(session)
.map(\.data)
.wrap()
.map(Swiftagram.User.Collection.init)
}
.eraseToAnyObservable()
.observe(on: session.scheduler)
.eraseToAnyObservable()
}
}

/// The explore feed.
public static var explore: Paginated<Page<Wrapper, String?>, String?> {
.init { secret, session, pages in
Pager(pages) { _, next, _ in
Projectables.Pager(pages) { _, next, _ in
base.explore
.header(appending: secret.header)
.query(appending: next, forKey: "max_id")
.session(session)
.project(session)
.map(\.data)
.wrap()
}
.eraseToAnyObservable()
.observe(on: session.scheduler)
.eraseToAnyObservable()
}
}

Expand All @@ -55,7 +55,7 @@ public extension Endpoint {
/// - parameter page: An optional `String` holding reference to a valid cursor. Defaults to `nil`.
public static var topics: Paginated<Page<Wrapper, String?>, String?> {
.init { secret, session, pages in
Pager(pages) { _, next, _ in
Projectables.Pager(pages) { _, next, _ in
base.topical_explore
.header(appending: secret.header)
.query(appending: ["is_prefetch": "true",
Expand All @@ -65,12 +65,12 @@ public extension Endpoint {
"session_id": secret["sessionid"]!,
"include_fixed_destinations": "false",
"max_id": next])
.session(session)
.project(session)
.map(\.data)
.wrap()
}
.eraseToAnyObservable()
.observe(on: session.scheduler)
.eraseToAnyObservable()
}
}
}
Expand Down
30 changes: 15 additions & 15 deletions Sources/Swiftagram/Endpoints/EndpointFriendship.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,19 @@ public extension Endpoint {
// Persist the rank token.
let rank = pages.offset?.rank ?? String(Int.random(in: 1_000..<10_000))
// Prepare the actual pager.
return Pager(pages) { _, next, _ in
return Projectables.Pager(pages) { _, next, _ in
base.path(appending: identifier)
.following
.header(appending: secret.header)
.header(appending: rank, forKey: "rank_token")
.query(appending: ["q": query, "max_id": next])
.session(session)
.project(session)
.map(\.data)
.wrap()
.map(Swiftagram.User.Collection.init)
}
.eraseToAnyObservable()
.observe(on: session.scheduler)
.eraseToAnyObservable()
}
}

Expand All @@ -57,19 +57,19 @@ public extension Endpoint {
// Persist the rank token.
let rank = pages.offset?.rank ?? String(Int.random(in: 1_000..<10_000))
// Prepare the actual pager.
return Pager(pages) { _, next, _ in
return Projectables.Pager(pages) { _, next, _ in
base.path(appending: identifier)
.followers
.header(appending: secret.header)
.header(appending: rank, forKey: "rank_token")
.query(appending: ["q": query, "max_id": next])
.session(session)
.project(session)
.map(\.data)
.wrap()
.map(Swiftagram.User.Collection.init)
}
.eraseToAnyObservable()
.observe(on: session.scheduler)
.eraseToAnyObservable()
}
}

Expand All @@ -78,17 +78,17 @@ public extension Endpoint {
/// - parameter identifier: A `String` holding reference to a valid user identifier.
public static func summary(for identifier: String) -> Disposable<Swiftagram.Friendship> {
.init { secret, session in
Deferred {
Projectables.Deferred {
base.show
.path(appending: identifier)
.header(appending: secret.header)
.session(session)
.project(session)
.map(\.data)
.wrap()
.map(Swiftagram.Friendship.init)
}
.eraseToAnyObservable()
.observe(on: session.scheduler)
.eraseToAnyObservable()
}
}

Expand All @@ -97,36 +97,36 @@ public extension Endpoint {
/// - parameter identifiers: A collection of `String`s hoding reference to valid user identifiers.
public static func summary<C: Collection>(for identifiers: C) -> Disposable<Swiftagram.Friendship.Dictionary> where C.Element == String {
.init { secret, session in
Deferred {
Projectables.Deferred {
base.path(appending: "show_many/")
.header(appending: secret.header)
.body(["user_ids": identifiers.joined(separator: ","),
"_csrftoken": secret["csrftoken"]!,
"_uuid": secret.client.device.identifier.uuidString])
.session(session)
.project(session)
.map(\.data)
.wrap()
.map(Swiftagram.Friendship.Dictionary.init)
}
.eraseToAnyObservable()
.observe(on: session.scheduler)
.eraseToAnyObservable()
}
}

/// A list of users who requested to follow you, without having been processed yet.
public static var pendingRequests: Paginated<Swiftagram.User.Collection, String?> {
.init { secret, session, pages in
Pager(pages) { _, next, _ in
Projectables.Pager(pages) { _, next, _ in
base.pending
.header(appending: secret.header)
.query(appending: next, forKey: "max_id")
.session(session)
.project(session)
.map(\.data)
.wrap()
.map(Swiftagram.User.Collection.init)
}
.eraseToAnyObservable()
.observe(on: session.scheduler)
.eraseToAnyObservable()
}
}
}
Expand Down
18 changes: 9 additions & 9 deletions Sources/Swiftagram/Endpoints/EndpointLocation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public extension Endpoint {
public static func around(coordinates: Swiftagram.Location.Coordinates,
matching query: String? = nil) -> Disposable<Swiftagram.Location.Collection> {
.init { secret, session in
Deferred {
Projectables.Deferred {
Endpoint.version1
.appendingDefaultHeader()
.path(appending: "location_search/")
Expand All @@ -35,13 +35,13 @@ public extension Endpoint {
"_uid": secret.identifier,
"_uuid": secret.client.device.identifier.uuidString
])
.session(session)
.project(session)
.map(\.data)
.wrap()
.map(Swiftagram.Location.Collection.init)
}
.eraseToAnyObservable()
.observe(on: session.scheduler)
.eraseToAnyObservable()
}
}

Expand All @@ -50,20 +50,20 @@ public extension Endpoint {
/// - parameter identifier: A valid location identifier.
public static func summary(for identifier: String) -> Disposable<Swiftagram.Location.Unit> {
.init { secret, session in
Deferred {
Projectables.Deferred {
Endpoint.version1
.locations
.path(appending: identifier)
.path(appending: "info/")
.appendingDefaultHeader()
.header(appending: secret.header)
.session(session)
.project(session)
.map(\.data)
.wrap()
.map(Swiftagram.Location.Unit.init)
}
.eraseToAnyObservable()
.observe(on: session.scheduler)
.eraseToAnyObservable()
}
}

Expand All @@ -72,20 +72,20 @@ public extension Endpoint {
/// - parameter identifier: A valid location identifier.
public static func stories(at identifier: String) -> Disposable<TrayItem.Unit> {
.init { secret, session in
Deferred {
Projectables.Deferred {
Endpoint.version1
.locations
.path(appending: identifier)
.path(appending: "story/")
.appendingDefaultHeader()
.header(appending: secret.header)
.session(session)
.project(session)
.map(\.data)
.wrap()
.map(TrayItem.Unit.init)
}
.eraseToAnyObservable()
.observe(on: session.scheduler)
.eraseToAnyObservable()
}
}
}
Expand Down
Loading

0 comments on commit 1689ebf

Please sign in to comment.