-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
370 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
Pod::Spec.new do |s| | ||
s.name = 'RestlerCombine' | ||
s.version = '1.1' | ||
s.summary = 'Framework for REST requests in Swift' | ||
s.description = <<-DESC | ||
Restler is a framework for type-safe and easy REST API requests in Swift. | ||
DESC | ||
s.homepage = 'https://github.com/railwaymen/restler' | ||
s.license = { :type => 'MIT', :file => 'LICENSE'} | ||
s.author = { 'Bartłomiej Świerad' => 'bartlomiej.swierad@railwaymen.org' } | ||
s.source = { | ||
:git => 'https://github.com/railwaymen/restler.git', | ||
:tag => s.version.to_s | ||
} | ||
s.ios.deployment_target = '13.0' | ||
s.swift_versions = '5.2' | ||
|
||
s.source_files = 'Sources/RestlerCombine/**/*.swift' | ||
s.dependency 'RestlerCore', '~> 1.1' | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import Foundation | ||
import RestlerCore | ||
import Combine | ||
|
||
@available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) | ||
extension Restler.Request { | ||
public var publisher: RestlerRequestPublisher<D> { | ||
RestlerRequestPublisher<D>(request: self) | ||
} | ||
} | ||
|
||
@available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) | ||
public struct RestlerRequestPublisher<DecodedType>: Publisher { | ||
public typealias Output = DecodedType | ||
public typealias Failure = Error | ||
|
||
private let request: Restler.Request<DecodedType> | ||
|
||
// MARK: Initialization | ||
public init(request: Restler.Request<DecodedType>) { | ||
self.request = request | ||
} | ||
|
||
// MARK: Publisher | ||
public func receive<S>(subscriber: S) where S: Subscriber, Failure == S.Failure, Output == S.Input { | ||
let subscription = Subscription(request: request, subscriber: subscriber) | ||
subscriber.receive(subscription: subscription) | ||
} | ||
} | ||
|
||
// MARK: - Subscription | ||
@available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) | ||
extension RestlerRequestPublisher { | ||
|
||
final class Subscription<S: Subscriber>: Combine.Subscription where Failure == S.Failure, Output == S.Input { | ||
var combineIdentifier: CombineIdentifier = .init() | ||
|
||
private var task: RestlerTaskType? | ||
private var subscriber: S? | ||
private let request: Restler.Request<DecodedType> | ||
|
||
// MARK: Initialization | ||
init(request: Restler.Request<DecodedType>, subscriber: S) { | ||
self.request = request | ||
self.subscriber = subscriber | ||
} | ||
|
||
// MARK: Subscription | ||
func request(_ demand: Subscribers.Demand) { | ||
task = request.subscribe( | ||
onSuccess: { [self] in | ||
_ = subscriber?.receive($0) | ||
subscriber?.receive(completion: .finished) | ||
}, | ||
onFailure: { [self] in | ||
subscriber?.receive(completion: .failure($0)) | ||
}) | ||
} | ||
|
||
func cancel() { | ||
subscriber = nil | ||
task?.cancel() | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.