launchdarkly / swift-eventsource

Server-sent events (SSE) client implementation in Swift for iOS, macOS, tvOS, and watchOS
https://launchdarkly.github.io/swift-eventsource/
Other
187 stars 34 forks source link

How do I add Authentication to the request? #83

Closed osirvics closed 1 month ago

osirvics commented 1 month ago

Will appreciate it if I can get a sample on how authentication, like an access token or cookies can be added to the request

keelerm84 commented 1 month ago

When constructing the EventSource, you must provide a Config. This Config allows you to provide additional headers that will be sent on each request. You can see an example below.

import Foundation
import LDSwiftEventSource

class MyEventHandler: EventHandler {
  // implementation ignored for brevity
}

var eventHandler = MyEventHandler()
var config = EventSource.Config(handler: eventHandler, url: url)
config.headers = ["Authorization": "WHATEVER YOU WANT"]

var eventSource = EventSource(config: config)
eventSource.start()
osirvics commented 1 month ago

Many thanks 🙏