| 1 | import Foundation |
| 2 | |
| 3 | // The person API's pairing, device and error shapes, from |
| 4 | // docs/api/equipo-v1.openapi.json. The conversation is in Thin.swift. Field |
| 5 | // names follow the wire (snake_case is converted by `EquipoJSON.decoder`). |
| 6 | // Optional numbers mean unknown, never zero. |
| 7 | |
| 8 | public struct Person: Codable, Sendable, Equatable { |
| 9 | public var address: String |
| 10 | public var displayIdentity: String |
| 11 | public var active: Bool |
| 12 | public var role: String |
| 13 | } |
| 14 | |
| 15 | public struct Me: Codable, Sendable, Equatable { |
| 16 | public var person: Person |
| 17 | public var teamId: String |
| 18 | public var teamName: String |
| 19 | public var revision: Int64 |
| 20 | } |
| 21 | |
| 22 | public struct Page<Item: Codable & Sendable>: Codable, Sendable { |
| 23 | public var items: [Item] |
| 24 | public var nextCursor: String |
| 25 | public var revision: Int64 |
| 26 | } |
| 27 | |
| 28 | |
| 29 | |
| 30 | |
| 31 | |
| 32 | |
| 33 | |
| 34 | |
| 35 | |
| 36 | |
| 37 | |
| 38 | |
| 39 | public struct Ack: Codable, Sendable, Equatable { |
| 40 | public var revision: Int64 |
| 41 | } |
| 42 | |
| 43 | public struct DeviceChallenge: Codable, Sendable, Equatable { |
| 44 | public var id: String |
| 45 | public var deviceSecret: String |
| 46 | public var userCode: String |
| 47 | public var challenge: String |
| 48 | public var verificationUri: String |
| 49 | public var expiresAt: Date |
| 50 | public var pollIntervalSeconds: Int |
| 51 | } |
| 52 | |
| 53 | public struct DeviceCredential: Codable, Sendable, Equatable { |
| 54 | public var id: String |
| 55 | public var token: String |
| 56 | public var audience: String |
| 57 | public var instanceId: String |
| 58 | public var expiresAt: Date |
| 59 | } |
| 60 | |
| 61 | public struct Device: Codable, Sendable, Identifiable, Equatable { |
| 62 | public var id: String |
| 63 | public var label: String |
| 64 | public var createdAt: Date |
| 65 | public var expiresAt: Date |
| 66 | public var revokedAt: Date? |
| 67 | public var revision: Int64 |
| 68 | public var lastUsedAt: Date? |
| 69 | } |
| 70 | |
| 71 | |
| 72 | |
| 73 | /// The person API's error body. |
| 74 | public struct APIErrorBody: Codable, Sendable, Equatable { |
| 75 | public var code: String |
| 76 | public var copyKey: String |
| 77 | public var requestId: String |
| 78 | public var currentRevision: Int64? |
| 79 | } |
| 80 | |
| 81 | public enum EquipoJSON { |
| 82 | /// Go's time.Time encodes RFC 3339 with optional fractional seconds. |
| 83 | public static var decoder: JSONDecoder { |
| 84 | let d = JSONDecoder() |
| 85 | d.keyDecodingStrategy = .convertFromSnakeCase |
| 86 | d.dateDecodingStrategy = .custom { decoder in |
| 87 | let c = try decoder.singleValueContainer() |
| 88 | let raw = try c.decode(String.self) |
| 89 | if let date = parseDate(raw) { return date } |
| 90 | throw DecodingError.dataCorruptedError(in: c, debugDescription: "not an RFC 3339 time: \(raw)") |
| 91 | } |
| 92 | return d |
| 93 | } |
| 94 | public static var encoder: JSONEncoder { |
| 95 | let e = JSONEncoder() |
| 96 | e.keyEncodingStrategy = .convertToSnakeCase |
| 97 | e.outputFormatting = [.sortedKeys, .withoutEscapingSlashes] |
| 98 | return e |
| 99 | } |
| 100 | static func parseDate(_ raw: String) -> Date? { |
| 101 | let fractional = Date.ISO8601FormatStyle(includingFractionalSeconds: true) |
| 102 | let whole = Date.ISO8601FormatStyle() |
| 103 | return (try? fractional.parse(raw)) ?? (try? whole.parse(raw)) |
| 104 | } |
| 105 | } |
| 106 | |