Skip to content

Commit

Permalink
Merge pull request #63 from jonasman/separateStreaming
Browse files Browse the repository at this point in the history
Separate streaming + oAuth streaming auth
  • Loading branch information
jonasman authored Dec 19, 2020
2 parents 4af1ab0 + d49b0b1 commit 3404786
Show file tree
Hide file tree
Showing 20 changed files with 615 additions and 432 deletions.
1 change: 1 addition & 0 deletions .swiftlint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ disabled_rules: # rule identifiers to exclude from running
- operator_whitespace
- cyclomatic_complexity
- variable_name
- switch_case_alignment
included: # paths to include during linting. `--path` is ignored if present.
- Sources
line_length: 200
Expand Down
56 changes: 56 additions & 0 deletions .swiftpm/xcode/xcshareddata/xcschemes/TeslaSwift-Package.xcscheme
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,62 @@
ReferencedContainer = "container:">
</BuildableReference>
</BuildActionEntry>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "TeslaSwiftStreaming"
BuildableName = "TeslaSwiftStreaming"
BlueprintName = "TeslaSwiftStreaming"
ReferencedContainer = "container:">
</BuildableReference>
</BuildActionEntry>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "TeslaSwiftStreamingCombine"
BuildableName = "TeslaSwiftStreamingCombine"
BlueprintName = "TeslaSwiftStreamingCombine"
ReferencedContainer = "container:">
</BuildableReference>
</BuildActionEntry>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "TeslaSwiftStreamingRx"
BuildableName = "TeslaSwiftStreamingRx"
BlueprintName = "TeslaSwiftStreamingRx"
ReferencedContainer = "container:">
</BuildableReference>
</BuildActionEntry>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "TeslaSwiftStreaminRx"
BuildableName = "TeslaSwiftStreaminRx"
BlueprintName = "TeslaSwiftStreaminRx"
ReferencedContainer = "container:">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
Expand Down
22 changes: 11 additions & 11 deletions Package.resolved
Original file line number Diff line number Diff line change
@@ -1,15 +1,6 @@
{
"object": {
"pins": [
{
"package": "OHHTTPStubs",
"repositoryURL": "https://github.com/AliSoftware/OHHTTPStubs.git",
"state": {
"branch": "feature/spm-support",
"revision": "fda9902f8c5c4170c6914d7dc845174e8c75bf92",
"version": null
}
},
{
"package": "PromiseKit",
"repositoryURL": "https://github.com/mxcl/PromiseKit",
Expand All @@ -24,8 +15,17 @@
"repositoryURL": "https://github.com/ReactiveX/RxSwift.git",
"state": {
"branch": null,
"revision": "b3e888b4972d9bc76495dd74d30a8c7fad4b9395",
"version": "5.0.1"
"revision": "97e14358a3e6564de8a6289362385a92d85936e1",
"version": "6.0.0-rc.2"
}
},
{
"package": "Starscream",
"repositoryURL": "https://github.com/daltoniam/Starscream.git",
"state": {
"branch": null,
"revision": "df8d82047f6654d8e4b655d1b1525c64e1059d21",
"version": "4.0.4"
}
}
]
Expand Down
16 changes: 11 additions & 5 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,27 @@ let package = Package(
],
products: [
.library(name: "TeslaSwift", targets: ["TeslaSwift"]),
.library(name: "TeslaSwiftStreaming", targets: ["TeslaSwift"]),
.library(name: "TeslaSwiftRx", targets: ["TeslaSwiftRx"]),
.library(name: "TeslaSwiftCombine", targets: ["TeslaSwiftCombine"]),
.library(name: "TeslaSwiftPromiseKit", targets: ["TeslaSwiftPMK"]),
.library(name: "TeslaSwiftRx", targets: ["TeslaSwiftRx"])
.library(name: "TeslaSwiftStreamingRx", targets: ["TeslaSwiftStreamingRx"]),
.library(name: "TeslaSwiftStreamingCombine", targets: ["TeslaSwiftCombine"])
],
dependencies: [
.package(url: "https://github.com/ReactiveX/RxSwift.git", .exact("6.0.0-rc.2")),
.package(url: "https://github.com/mxcl/PromiseKit", from: "6.0.0"),
.package(url: "https://github.com/ReactiveX/RxSwift.git", from: "5.0.0"),
.package(url: "https://github.com/daltoniam/Starscream.git", from: "3.0.0"),
.package(url: "https://github.com/daltoniam/Starscream.git", from: "4.0.0"),
//.package(url: "https://github.com/AliSoftware/OHHTTPStubs.git", Package.Dependency.Requirement.branch("feature/spm-support"))
],
targets: [
.target(name: "TeslaSwift", dependencies: ["Starscream"]),
.target(name: "TeslaSwift"),
.target(name: "TeslaSwiftStreaming", dependencies: ["TeslaSwift", "Starscream"], path: "Sources/Extensions/Streaming"),
.target(name: "TeslaSwiftRx", dependencies: ["TeslaSwift", "RxSwift", "RxCocoa"], path: "Sources/Extensions/Rx"),
.target(name: "TeslaSwiftCombine", dependencies: ["TeslaSwift"], path: "Sources/Extensions/Combine"),
.target(name: "TeslaSwiftPMK", dependencies: ["TeslaSwift", "PromiseKit"], path: "Sources/Extensions/PromiseKit"),
.target(name: "TeslaSwiftRx", dependencies: ["TeslaSwift", "RxSwift", "RxCocoa"], path: "Sources/Extensions/Rx"),
.target(name: "TeslaSwiftStreamingRx", dependencies: ["TeslaSwiftStreaming", "TeslaSwiftRx"], path: "Sources/Extensions/StreamingRx"),
.target(name: "TeslaSwiftStreamingCombine", dependencies: ["TeslaSwiftStreaming", "TeslaSwiftCombine"], path: "Sources/Extensions/StreamingCombine"),
//.testTarget(name: "TeslaSwiftTests", dependencies: ["TeslaSwiftPMK", "PromiseKit", "OHHTTPStubsSwift"], path: "TeslaSwiftTests")
]
)
48 changes: 39 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,37 @@ Copy `Sources` folder into your project
If you don't need any extensions, use this line

```ruby
pod 'TeslaSwift', '~> 6'
pod 'TeslaSwift', '~> 7'
```
If you need PromiseKit extensions, use this line

```ruby
pod 'TeslaSwift/PromiseKit', '~> 6'
pod 'TeslaSwift/PromiseKit', '~> 7'
```
If you need Combine extensions, use this line

```ruby
pod 'TeslaSwift/Combine', '~> 6'
pod 'TeslaSwift/Combine', '~> 7'
```
If you need Rx extensions, use this line

```ruby
pod 'TeslaSwift/Rx', '~> 6'
pod 'TeslaSwift/Rx', '~> 7'
```

Streaming extension

```ruby
pod 'TeslaSwift/Streaming', '~> 7'
```
If you need Combine extensions for Streaming, use this line
```ruby
pod 'TeslaSwift/StreamingCombine', '~> 7'
```
If you need Rx extensions for Streaming, use this line

```ruby
pod 'TeslaSwift/StreamingRx', '~> 7'
```

### Swift Package Manager
Expand All @@ -43,10 +58,11 @@ You can use [Swift Package Manager](https://swift.org/package-manager/) and spec
```

There are also extensions for Combine `TeslaSwiftCombine`, PromiseKit `TeslaSwiftPMK` and Rx `TeslaSwiftRx`
The Streaming extensions are: `TeslaSwiftStreaming`, Combine `TeslaSwiftStreamingCombine` and Rx `TeslaSwiftStreamingRx`

## Usage

Tesla's server is not fully compatible with ATS so you need to add the following to your app Info.plist
Tesla's server is not compatible with ATS so you need to add the following to your app Info.plist

```XML
<key>NSAppTransportSecurity</key>
Expand Down Expand Up @@ -108,7 +124,7 @@ guard let safeWebloginViewController = authViewControler else { /* error */ retu
present(safeWebloginViewController, animated: true, completion: nil)
```

Or if you use PromiseKit you can check the success:
Or if you use PromiseKit you can check success like this:

```swift
api.authenticate(email: email, password: password)
Expand Down Expand Up @@ -147,12 +163,26 @@ class ViewController {
```

## Streaming
Import the module

```swift
import TeslaSwiftStreaming
```

Import the extension modules if needed (with the previous line)

```swift
import TeslaSwiftStreamingCombine
```
```swift
import TeslaSwiftStreamingRx
```
```swift
class ViewController {

func showStream() {
api.openStream(vehicle: myVehicle, dataReceived: {
stream = TeslaStreaming(teslaSwift: api)
stream.openStream(vehicle: myVehicle, dataReceived: {
(event: TeslaStreamEvent) in
switch event {
case .open:
Expand All @@ -167,7 +197,7 @@ class ViewController {
})

// After some events...
api.closeStream()
stream.closeStream()
}
}
```
Expand All @@ -193,7 +223,7 @@ After the authentication is done. The library manages itself the access token.
When the token expires the library will request a new token using the previous refresh token.

## Roadmap
6.x
7.x

Add new API features and summon

Expand Down
65 changes: 0 additions & 65 deletions Sources/Extensions/Combine/TeslaSwift+Combine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -194,71 +194,6 @@ extension TeslaSwift {

return future
}

public func streamPublisher(vehicle: Vehicle) -> TeslaStreamingPublisher {

guard let email = email,
let vehicleToken = vehicle.tokens?.first else {
//dataReceived((nil, TeslaError.streamingMissingEmailOrVehicleToken))
let authentication = TeslaStreamAuthentication(email: "", vehicleToken: "", vehicleId: "\(vehicle.vehicleID!)")
return streamPublisher(authentication: authentication)
}

let authentication = TeslaStreamAuthentication(email: email, vehicleToken: vehicleToken, vehicleId: "\(vehicle.vehicleID!)")


return streamPublisher(authentication: authentication)
}
}

@available(iOS 13.0, macOS 10.15, watchOS 6, tvOS 13, *)
extension TeslaSwift {

public struct TeslaStreamingPublisher: Publisher, Cancellable {

public typealias Output = TeslaStreamingEvent
public typealias Failure = Error

let authentication: TeslaStreamAuthentication
let stream: TeslaStreaming

init(authentication: TeslaStreamAuthentication, stream: TeslaStreaming) {
self.authentication = authentication
self.stream = stream
}

public func receive<S>(subscriber: S) where S : Subscriber, TeslaStreamingPublisher.Failure == S.Failure, TeslaStreamingPublisher.Output == S.Input {

stream.openStream(authentication: authentication) {
(streamEvent: TeslaStreamingEvent) in

switch streamEvent {
case .open:
_ = subscriber.receive(TeslaStreamingEvent.open)
case .event(let event):
_ = subscriber.receive(TeslaStreamingEvent.event(event))
case .error(let error):
_ = subscriber.receive(TeslaStreamingEvent.error(error))
case .disconnected:
_ = subscriber.receive(TeslaStreamingEvent.disconnected)
subscriber.receive(completion: Subscribers.Completion.finished)
}

}

}

public func cancel() {
stream.closeStream()
}

}

func streamPublisher(authentication: TeslaStreamAuthentication) -> TeslaStreamingPublisher {

return TeslaStreamingPublisher(authentication: authentication, stream: TeslaStreaming())
}

}

#endif
Loading

0 comments on commit 3404786

Please sign in to comment.