Repository Public
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project : MacLustr SMS Gateway9// File : sdk/swift/Sources/MacLustrSMS/Models.swift10// Module : MacLustrSMS11// Repository : https://git.spboucher.ai/maclustr-sms12// Project Website : https://sms.spboucher.ai13// Created : 2026-11-3014// Last Modified : 2026-11-3015//16// =============================================================================17// AUTHOR & PROFESSIONAL AFFILIATION18// =============================================================================19//20// Author : Simon-Pierre Boucher21// Position : Professor of Finance22// Institution : Université du Québec en Outaouais (UQO)23// Department : Département des sciences administratives24// Organization : SP Boucher25//26// Website : https://spboucher.ai27// Git : https://git.spboucher.ai28//29// =============================================================================30// OFFICIAL CONTACT DIRECTORY31// =============================================================================32//33// General : info@spboucher.ai34// Professional : contact@spboucher.ai35// Direct : simon@spboucher.ai36// Research : research@spboucher.ai37// Development : dev@spboucher.ai38// Support : support@spboucher.ai39// Security/Admin : admin@spboucher.ai40// Notifications : notifications@spboucher.ai41//42// Preferred Contact: admin@spboucher.ai43//44// =============================================================================45// COPYRIGHT & INTELLECTUAL PROPERTY46// =============================================================================47//48// Copyright (c) 202649// Simon-Pierre Boucher50//51// All rights reserved where applicable.52//53// This file is governed by the applicable project license,54// copyright notices, institutional agreements and relevant law.55//56// Unless expressly authorized by the applicable license or57// rights holder, copying, redistribution, sublicensing,58// publication and commercial exploitation are not permitted.59//60// Existing third-party rights and notices remain unaffected.61//62// =============================================================================63// CONFIDENTIALITY & SECURITY64// =============================================================================65//66// Classification : Public67// Distribution : Public repository; use is governed by the project license68// Security Contact : admin@spboucher.ai69//70// Access to this source does not independently grant permission71// to redistribute or disclose confidential materials.72//73// Do not embed credentials, private keys, access tokens,74// certificates or confidential personal information.75//76// =============================================================================77// LICENSE & MAINTENANCE78// =============================================================================79//80// License : None specified (no license file in this repository)81// Maintainer : Simon-Pierre Boucher82// Technical Contact: dev@spboucher.ai83//84// =============================================================================85// END OF SP BOUCHER SOURCE HEADER86// =============================================================================8788import Foundation8990/// Valeur JSON quelconque (détails d'événements, compteurs d'usage, charge utile de webhook).91public enum JSONValue: Codable, Sendable, Equatable {92 case null, bool(Bool), number(Double), string(String), array([JSONValue]), object([String: JSONValue])9394 public init(from decoder: Decoder) throws {95 let c = try decoder.singleValueContainer()96 if c.decodeNil() { self = .null }97 else if let v = try? c.decode(Bool.self) { self = .bool(v) }98 else if let v = try? c.decode(Double.self) { self = .number(v) }99 else if let v = try? c.decode(String.self) { self = .string(v) }100 else if let v = try? c.decode([JSONValue].self) { self = .array(v) }101 else { self = .object(try c.decode([String: JSONValue].self)) }102 }103 public func encode(to encoder: Encoder) throws {104 var c = encoder.singleValueContainer()105 switch self {106 case .null: try c.encodeNil()107 case .bool(let v): try c.encode(v)108 case .number(let v): try c.encode(v)109 case .string(let v): try c.encode(v)110 case .array(let v): try c.encode(v)111 case .object(let v): try c.encode(v)112 }113 }114 public subscript(key: String) -> JSONValue? { if case .object(let o) = self { return o[key] }; return nil }115 public var stringValue: String? { if case .string(let s) = self { return s }; return nil }116 public var doubleValue: Double? { if case .number(let n) = self { return n }; return nil }117 public var boolValue: Bool? { if case .bool(let b) = self { return b }; return nil }118}119120public struct MessageError: Codable, Sendable, Equatable { public let code: String; public let detail: String? }121public struct MessageEvent: Codable, Sendable { public let type: String; public let from: String?; public let to: String?; public let source: String; public let detail: JSONValue?; public let at: String? }122123public struct Message: Codable, Sendable, Identifiable {124 /// Statuts dont un message ne sort plus. « submitted » (remis au réseau) n'en fait pas partie.125 public static let finalStatuses: Set<String> = ["delivered", "failed", "expired", "cancelled", "unknown"]126 public let id: String127 public let direction: String128 /// held, queued, dispatched, accepted, submitting, submitted, delivered, failed, expired, cancelled, unknown, received129 public let status: String130 public let to: String?131 public let from: String?132 public let text: String?133 public let textPurged: Bool?134 public let deviceId: String?135 public let simId: String?136 public let applicationId: String?137 public let conversationId: String?138 public let encoding: String?139 public let segments: Int?140 public let characters: Int?141 public let error: MessageError?142 public let createdAt: String?143 public let expiresAt: String?144 public let dispatchedAt: String?145 public let acceptedAt: String?146 public let submittedAt: String?147 public let deliveredAt: String?148 public let finalizedAt: String?149 public let receivedAt: String?150 public let events: [MessageEvent]?151 /// true si l'API a renvoyé un message déjà créé avec la même Idempotency-Key (aucun second envoi).152 public var replayed: Bool = false153 public var isFinal: Bool { Message.finalStatuses.contains(status) }154155 enum CodingKeys: String, CodingKey {156 case id, direction, status, to, from, text, textPurged, deviceId, simId, applicationId, conversationId, encoding, segments, characters, error157 case createdAt, expiresAt, dispatchedAt, acceptedAt, submittedAt, deliveredAt, finalizedAt, receivedAt, events158 }159}160161public struct Sim: Codable, Sendable, Identifiable {162 public let id: String163 public let deviceId: String164 public let subscriptionId: Int?165 public let slotIndex: Int?166 public let carrierName: String?167 public let displayName: String?168 public let label: String?169 /// physical, esim, unknown170 public let type: String?171 public let number: String?172 public let numberSource: String?173 public let state: String?174 public let signalLevel: Int?175 public let roaming: Bool?176 public let active: Bool177 public let sendEnabled: Bool178 public let dailyLimit: Int?179 public let updatedAt: String?180}181182public struct Device: Codable, Sendable, Identifiable {183 public let id: String184 public let name: String185 public let manufacturer: String?186 public let model: String?187 public let androidVersion: String?188 public let appVersion: String?189 public let online: Bool190 public let enabled: Bool191 public let maintenance: Bool192 public let available: Bool193 public let lastSeenAt: String?194 public let batteryPct: Int?195 public let charging: Bool?196 public let network: String?197 public let gatewayState: String?198 public let sims: [Sim]199}200201public struct PhoneNumber: Codable, Sendable { public let number: String; public let simId: String; public let deviceId: String; public let carrierName: String?; public let source: String?; public let label: String?; public let sendEnabled: Bool; public let active: Bool }202public struct Conversation: Codable, Sendable, Identifiable { public let id: String; public let simId: String; public let deviceId: String?; public let number: String?; public let peer: String; public let lastMessageAt: String?; public let lastDirection: String?; public let inboundCount: Int; public let outboundCount: Int; public let unread: Int }203public struct Webhook: Codable, Sendable, Identifiable {204 public let id: String; public let url: String; public let description: String?; public let events: [String]; public let enabled: Bool; public let failureStreak: Int?; public let createdAt: String?205 /// Secret de signature : présent uniquement dans la réponse de création.206 public let secret: String?207}208/// Vérification d'un numéro par code. Le code lui-même n'apparaît jamais dans l'API.209public struct Verification: Codable, Sendable, Identifiable {210 public let id: String211 public let to: String212 /// pending, approved, failed, expired, cancelled213 public let status: String214 public let attempts: Int215 public let attemptsRemaining: Int216 public let createdAt: String?217 public let expiresAt: String?218 public let approvedAt: String?219 public let messageId: String?220 public let messageStatus: String?221}222/// Résultat d'un contrôle de code. `valid` vaut true une seule fois : ne se fier qu'à ce champ.223public struct VerificationCheck: Codable, Sendable, Identifiable {224 public let id: String225 public let to: String226 public let status: String227 public let attempts: Int228 public let attemptsRemaining: Int229 public let expiresAt: String?230 public let approvedAt: String?231 public let valid: Bool232}233public struct Page<T: Codable & Sendable>: Codable, Sendable { public let data: [T]; public let hasMore: Bool; public let nextCursor: String? }234public struct Quota: Codable, Sendable { public let daily: Int; public let usedToday: Int; public let ratePerMinute: Int }235public struct Usage: Codable, Sendable { public let applicationId: String; public let quota: Quota; public let from: String; public let to: String; public let totals: [String: Int]; public let days: [[String: JSONValue]] }236public struct Queue: Codable, Sendable { public let pending: Int; public let inFlight: Int; public let held: Int; public let oldestPendingAt: String?; public let items: [[String: JSONValue]] }237public struct Health: Codable, Sendable { public let status: String; public let version: String; public let time: String }238public struct WebhookEvent: Codable, Sendable { public let event: String; public let id: String; public let createdAt: String?; public let data: JSONValue }239240/// Erreur renvoyée par l'API (ou échec réseau : status = 0, code = "network_error" ; délai de wait dépassé : code = "wait_timeout").241public struct MacLustrSMSError: Error, LocalizedError, Sendable {242 public let status: Int243 public let code: String244 public let message: String245 public let details: JSONValue?246 /// wait() : dernier état connu du message quand le délai est dépassé.247 public let lastMessage: Message?248 /// Réponses 429 : secondes à attendre avant de réessayer (en-tête Retry-After), nil si le serveur ne l'indique pas.249 public let retryAfter: TimeInterval?250 public init(status: Int, code: String, message: String, details: JSONValue? = nil, lastMessage: Message? = nil, retryAfter: TimeInterval? = nil) { self.status = status; self.code = code; self.message = message; self.details = details; self.lastMessage = lastMessage; self.retryAfter = retryAfter }251 public var errorDescription: String? { "\(code) (\(status)) : \(message)" }252}253public enum WebhookVerificationError: Error, Equatable { case invalidTimestamp, timestampOutOfTolerance, invalidSignature, invalidBody }254