aboutsummaryrefslogtreecommitdiff
path: root/Sources/LichessClient/LichessClient+Broadcasts.swift
blob: 93f8df782c2f5d0b6ecb9f6ae690b33bdfd0c1d5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
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<LichessClient.TournamentResponse.CodingKeys> =
        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<HTTPBody>, 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)
    }
  }
}