Skip to content

WIP - A Swift library for interacting with Twitch Chat, API and Pub-/EventSub

License

Notifications You must be signed in to change notification settings

LosFarmosCTL/swift-twitch-client

Repository files navigation

swift-twitch-client

CI

Swift native client to access various 3rd party twitch elements like the API and Chat.

Caution

The developement of this library is still very much in progress, APIs may be incomplete or change without any notice!

I do not recommend to use this in any projects until I've finalized the design.

This is the current idea for how the usage might look in the end (not at all final, if I find something that feels better right now I will just change it):

Helix

let twitch = TwitchClient(with: credentials)

// Completion Handlers

twitch.helixTask(for: .doSomething(param1: "forsen")) { result, error in
}

// Async/Await
let result = try await twitch.helix(endpoint: .doSomething(param1: "forsen"))

// Combine
twitch.helixPublisher(for: .doSomething(param1: "forsen")).sink(
  receiveCompletion: { error in

  },
  receiveValue: { result in

  })

Chat (IRC)

let twitch = TwitchClient(with: credentials)

let irc = try await twitch.ircClient(.authenticated)

try await irc.join(to: "forsen")
try await irc.part(from: "forsen")

// Using callbacks

irc.listener { result in
  switch result {
  case .success(let message): print(message)
  case .failure(let error): print(error)
  }
}

// Using AsyncStream

let stream = irc.stream()
for try await message in stream {
  if case .privateMessage(let privMsg) = message {
    try await client.send("Received message: \(privMsg.message)", in: "forsen")
  }
}

// Using Combine

let publisher = irc.publisher()

irc.publisher().sink(
  receiveCompletion: {
    // handle error
  },
  receiveValue: {
    // handle msg
  })

EventSub

// WIP