Skip to content

Commit

Permalink
add SimpleAuthSample
Browse files Browse the repository at this point in the history
  • Loading branch information
kosyloa committed Jun 18, 2024
1 parent a15407a commit 6fdbc95
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 1 deletion.
2 changes: 2 additions & 0 deletions DXFeedFramework.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -917,6 +917,7 @@
64F73BA52B67CD5B0088EC37 /* GraalException+Ext.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "GraalException+Ext.swift"; sourceTree = "<group>"; };
64F9C6C12B4BFD8F003ED014 /* DXFeedconnect.playground */ = {isa = PBXFileReference; lastKnownFileType = file.playground; path = DXFeedconnect.playground; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.swift; };
64FDD7B62C21A09000D4469B /* DxFeedReconnectSample.playground */ = {isa = PBXFileReference; lastKnownFileType = file.playground; path = DxFeedReconnectSample.playground; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.swift; };
64FDD7B72C21AF9D00D4469B /* SimpleAuthSample.playground */ = {isa = PBXFileReference; lastKnownFileType = file.playground; path = SimpleAuthSample.playground; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.swift; };
803BAC0D29BFA50700FFAB1C /* DXFeedFramework.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = DXFeedFramework.framework; sourceTree = BUILT_PRODUCTS_DIR; };
803BAC1029BFA50700FFAB1C /* DxFeedSwiftFramework.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = DxFeedSwiftFramework.h; sourceTree = "<group>"; };
803BAC1529BFA50700FFAB1C /* DXFeedFrameworkTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = DXFeedFrameworkTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
Expand Down Expand Up @@ -1117,6 +1118,7 @@
64A631CF2BDFAA27002E1002 /* OnDemandSample.playground */,
643A329C2BD15F2900F6F790 /* LastEventsConsole.playground */,
64FDD7B62C21A09000D4469B /* DxFeedReconnectSample.playground */,
64FDD7B72C21AF9D00D4469B /* SimpleAuthSample.playground */,
);
path = Playgrounds;
sourceTree = "<group>";
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,9 @@ is a simple demonstration of how to get live updates for Instrument Profiles
- [x] [DxFeedReconnectSample](https://github.com/dxFeed/dxfeed-graal-swift-api/blob/swift/Samples/Playgrounds/DxFeedReconnectSample.playground/Contents.swift)
is a simple demonstration of how to connect to an endpoint, subscribe to market data events, handle reconnections
and re-subscribing.

- [x] [SimpleAuthSample](https://github.com/dxFeed/dxfeed-graal-swift-api/blob/swift/Samples/Playgrounds/SimpleAuthSample.playground/Contents.swift)
is a simple demonstration of how to connect to endpoint requires authentication token, subscribe to market data events, and handle periodic token updates.

## Current State

### Endpoint Roles
Expand Down
99 changes: 99 additions & 0 deletions Samples/Playgrounds/SimpleAuthSample.playground/Contents.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import Cocoa
import PlaygroundSupport
import DXFeedFramework

// Empty Event Listener with handler
class Listener: DXEventListener, Hashable {

static func == (lhs: Listener, rhs: Listener) -> Bool {
lhs === rhs
}

func hash(into hasher: inout Hasher) {
hasher.combine("\(self):\(stringReference(self))")
}
var callback: ([MarketEvent]) -> Void = { _ in }

func receiveEvents(_ events: [MarketEvent]) {
self.callback(events)
}

init(overrides: (Listener) -> Listener) {
_ = overrides(self)
}
}

// Empty Endpoint Listener with handler
class EndpoointStateListener: DXEndpointListener, Hashable {
func endpointDidChangeState(old: DXFeedFramework.DXEndpointState, new: DXFeedFramework.DXEndpointState) {
callback(old, new)
}

static func == (lhs: EndpoointStateListener, rhs: EndpoointStateListener) -> Bool {
lhs === rhs
}

func hash(into hasher: inout Hasher) {
hasher.combine("\(self):\(stringReference(self))")
}
var callback: (DXEndpointState, DXEndpointState) -> Void = { _,_ in }

init(overrides: (EndpoointStateListener) -> EndpoointStateListener) {
_ = overrides(self)
}
}

let address = "demo.dxfeed.com:7300";

func updateTokenAndReconnect() {
try? DXEndpoint.getInstance().connect("\(address)[login=entitle:\(generateToken())]")
}

func generateToken() -> String {
let length = Int.random(in: 4...10)
let letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
return String((0..<length).map { _ in letters.randomElement()! })
}


// Demonstrates how to connect to endpoint requires authentication token, subscribe to market data events,
// and handle periodic token updates.
let endpoint = try DXEndpoint.getInstance()
// Add a listener for state changes to the default application-wide singleton instance of DXEndpoint.
let stateListener = EndpoointStateListener { listener in
listener.callback = { old, new in
print("Connection state changed: \(old) -> \(new)")
}
return listener
}
endpoint.add(listener: stateListener)

// Set up a timer to periodically update the token and reconnect every 10 seconds.
// The first connection will be made immediately.
// After reconnection, all existing subscriptions will be re-subscribed automatically.
Timer.scheduledTimer(withTimeInterval: 10, repeats: true) { timer in
updateTokenAndReconnect()
}.fire()

// Create a subscription for Quote events.
let subscriptionQuote = try endpoint.getFeed()?.createSubscription(Quote.self)
// Listener must be attached before symbols are added.
let listener = Listener { listener in
listener.callback = { events in
// Event listener that prints each received event.
events.forEach { event in
print(event.toString())
}
}
return listener
}
try subscriptionQuote?.add(listener: listener)

// Add the specified symbol to the subscription.
try subscriptionQuote?.addSymbols("ETH/USD:GDAX")

// Keep the application running indefinitely.
PlaygroundPage.current.needsIndefiniteExecution = true

// to finish execution run this line
PlaygroundPage.current.finishExecution()
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<playground version='5.0' target-platform='macos' buildActiveScheme='true' importAppTypes='true'>
<timeline fileName='timeline.xctimeline'/>
</playground>

0 comments on commit 6fdbc95

Please sign in to comment.