From f1cc08c4c3bfbc844047dd7b6b68bc6fab9baedf Mon Sep 17 00:00:00 2001 From: Navan Chauhan Date: Wed, 24 Apr 2024 01:57:08 -0600 Subject: initial commit --- .../LichessClient/LichessClient+Broadcasts.swift | 110 +++++++++++++++++++++ 1 file changed, 110 insertions(+) create mode 100644 Sources/LichessClient/LichessClient+Broadcasts.swift (limited to 'Sources/LichessClient/LichessClient+Broadcasts.swift') diff --git a/Sources/LichessClient/LichessClient+Broadcasts.swift b/Sources/LichessClient/LichessClient+Broadcasts.swift new file mode 100644 index 0000000..93f8df7 --- /dev/null +++ b/Sources/LichessClient/LichessClient+Broadcasts.swift @@ -0,0 +1,110 @@ +// +// LichessClient+Broadcasts.swift +// +// +// Created by Navan Chauhan on 4/24/24. +// + +import Foundation +import OpenAPIRuntime + +extension LichessClient { + public struct TournamentResponse: Codable, Identifiable { + public let id: String + public let tour: Tournament + public let rounds: [Round] + + public init(from decoder: Decoder) throws { + let container: KeyedDecodingContainer = + try decoder.container(keyedBy: LichessClient.TournamentResponse.CodingKeys.self) + self.tour = try container.decode( + LichessClient.Tournament.self, forKey: LichessClient.TournamentResponse.CodingKeys.tour) + self.rounds = try container.decode( + [LichessClient.Round].self, forKey: LichessClient.TournamentResponse.CodingKeys.rounds) + self.id = tour.id + } + } + + public struct Tournament: Codable { + public let id: String + public let name: String + public let slug: String + public let description: String + public let markup: String? + public let url: String? + } + + public struct Round: Codable, Identifiable { + public let id: String + public let name: String + public let slug: String + public let startsAt: Date // UNIX Epoch? + public let finished: Bool? + public let ongoing: Bool? + + } + + public struct Player: Codable { + public let userId: String + public let name: String + public let color: String + } + + public func broadcastIndex(nb: Int = 20) async throws -> AsyncThrowingMapSequence< + JSONLinesDeserializationSequence, LichessClient.TournamentResponse + > { + let response = try await underlyingClient.broadcastIndex(query: .init(nb: nb)) + switch response { + case .ok(let okResponse): + let tournaments = try okResponse.body.application_x_hyphen_ndjson.asDecodedJSONLines( + of: TournamentResponse.self) + return tournaments + case .undocumented(let statusCode, _): + throw LichessClientError.undocumentedResponse(statusCode: statusCode) + } + } + + public func broadcastRound( + broadcastTournamentSlug: String = "-", broadcastRoundSlug: String = "-", + broadcastRoundId: String + ) async throws { + let response = try await underlyingClient.broadcastRoundGet( + path: .init( + broadcastTournamentSlug: broadcastTournamentSlug, + broadcastRoundSlug: broadcastRoundSlug, + broadcastRoundId: broadcastRoundId + )) + switch response { + case .ok(let okResponse): + print(okResponse.body, try okResponse.body.json) + // TODO: Return + case .undocumented(let statusCode, _): + throw LichessClientError.undocumentedResponse(statusCode: statusCode) + } + } + + public func getBroadcastRoundPGN(broadcastRoundId: String) async throws -> HTTPBody { + let response = try await underlyingClient.broadcastRoundPgn( + path: .init( + broadcastRoundId: broadcastRoundId + )) + + switch response { + case .ok(let okResponse): + return try okResponse.body.application_x_hyphen_chess_hyphen_pgn + case .undocumented(let statusCode, _): + throw LichessClientError.undocumentedResponse(statusCode: statusCode) + } + } + + public func broadcastStreamRoundPgn(broadcastRoundId: String) async throws -> HTTPBody { + let response = try await underlyingClient.broadcastStreamRoundPgn( + path: .init(broadcastRoundId: broadcastRoundId)) + switch response { + case .ok(let okResponse): + return try okResponse.body.application_x_hyphen_chess_hyphen_pgn + case .undocumented(let statusCode, _): + throw LichessClientError.undocumentedResponse(statusCode: statusCode) + } + } +} -- cgit v1.2.3