-
Notifications
You must be signed in to change notification settings - Fork 854
Expand file tree
/
Copy pathOpenCodeGoUsageFetcher.swift
More file actions
775 lines (708 loc) · 28.9 KB
/
OpenCodeGoUsageFetcher.swift
File metadata and controls
775 lines (708 loc) · 28.9 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
import Foundation
#if canImport(FoundationNetworking)
import FoundationNetworking
#endif
public enum OpenCodeGoUsageError: LocalizedError {
case invalidCredentials
case networkError(String)
case apiError(String)
case parseFailed(String)
public var errorDescription: String? {
switch self {
case .invalidCredentials:
"OpenCode Go session cookie is invalid or expired."
case let .networkError(message):
"OpenCode Go network error: \(message)"
case let .apiError(message):
"OpenCode Go API error: \(message)"
case let .parseFailed(message):
"OpenCode Go parse error: \(message)"
}
}
}
public struct OpenCodeGoUsageFetcher: Sendable {
private static let log = CodexBarLog.logger(LogCategories.opencodeGoUsage)
private static let baseURL = URL(string: "https://opencode.ai")!
private static let serverURL = URL(string: "https://opencode.ai/_server")!
private static let workspacesServerID = "def39973159c7f0483d8793a822b8dbb10d067e12c65455fcb4608459ba0234f"
private static let userAgent =
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) " +
"AppleWebKit/537.36 (KHTML, like Gecko) Chrome/143.0.0.0 Safari/537.36"
private struct ServerRequest {
let serverID: String
let args: String?
let method: String
let referer: URL
}
private static let percentKeys = [
"usagePercent",
"usedPercent",
"percentUsed",
"percent",
"usage_percent",
"used_percent",
"utilization",
"utilizationPercent",
"utilization_percent",
"usage",
]
private static let resetInKeys = [
"resetInSec",
"resetInSeconds",
"resetSeconds",
"reset_sec",
"reset_in_sec",
"resetsInSec",
"resetsInSeconds",
"resetIn",
"resetSec",
]
private static let resetAtKeys = [
"resetAt",
"resetsAt",
"reset_at",
"resets_at",
"nextReset",
"next_reset",
"renewAt",
"renew_at",
]
public static func fetchUsage(
cookieHeader: String,
timeout: TimeInterval,
now: Date = Date(),
workspaceIDOverride: String? = nil,
session: URLSession = .shared) async throws -> OpenCodeGoUsageSnapshot
{
guard let requestCookieHeader = OpenCodeWebCookieSupport.requestCookieHeader(from: cookieHeader) else {
throw OpenCodeGoUsageError.invalidCredentials
}
let workspaceID: String = if let override = self.normalizeWorkspaceID(workspaceIDOverride) {
override
} else {
try await self.fetchWorkspaceID(
cookieHeader: requestCookieHeader,
timeout: timeout,
session: session)
}
let subscriptionText = try await self.fetchUsagePage(
workspaceID: workspaceID,
cookieHeader: requestCookieHeader,
timeout: timeout,
session: session)
return try self.parseSubscription(text: subscriptionText, now: now)
}
private static func fetchWorkspaceID(
cookieHeader: String,
timeout: TimeInterval,
session: URLSession) async throws -> String
{
let text = try await self.fetchServerText(
request: ServerRequest(
serverID: self.workspacesServerID,
args: nil,
method: "GET",
referer: self.baseURL),
cookieHeader: cookieHeader,
timeout: timeout,
session: session)
if self.looksSignedOut(text: text) {
throw OpenCodeGoUsageError.invalidCredentials
}
var ids = self.parseWorkspaceIDs(text: text)
if ids.isEmpty {
ids = self.parseWorkspaceIDsFromJSON(text: text)
}
if ids.isEmpty {
Self.log.error("OpenCode Go workspace ids missing after GET; retrying with POST.")
let fallback = try await self.fetchServerText(
request: ServerRequest(
serverID: self.workspacesServerID,
args: "[]",
method: "POST",
referer: self.baseURL),
cookieHeader: cookieHeader,
timeout: timeout,
session: session)
if self.looksSignedOut(text: fallback) {
throw OpenCodeGoUsageError.invalidCredentials
}
ids = self.parseWorkspaceIDs(text: fallback)
if ids.isEmpty {
ids = self.parseWorkspaceIDsFromJSON(text: fallback)
}
if ids.isEmpty {
throw OpenCodeGoUsageError.parseFailed("Missing workspace id.")
}
return ids[0]
}
return ids[0]
}
public static func normalizeWorkspaceID(_ raw: String?) -> String? {
guard let raw else { return nil }
let trimmed = raw.trimmingCharacters(in: .whitespacesAndNewlines)
if trimmed.hasPrefix("wrk_"), trimmed.count > 4 {
return trimmed
}
if let url = URL(string: trimmed) {
let parts = url.pathComponents
if let index = parts.firstIndex(of: "workspace"),
parts.count > index + 1
{
let candidate = parts[index + 1]
if candidate.hasPrefix("wrk_"), candidate.count > 4 {
return candidate
}
}
}
if let match = trimmed.range(of: #"wrk_[A-Za-z0-9]+"#, options: .regularExpression) {
return String(trimmed[match])
}
return nil
}
static func parseWorkspaceIDs(text: String) -> [String] {
let pattern = #"id\s*:\s*\"(wrk_[^\"]+)\""#
guard let regex = try? NSRegularExpression(pattern: pattern, options: []) else { return [] }
let nsrange = NSRange(text.startIndex..<text.endIndex, in: text)
return regex.matches(in: text, options: [], range: nsrange).compactMap { match in
guard let range = Range(match.range(at: 1), in: text) else { return nil }
return String(text[range])
}
}
private static func parseWorkspaceIDsFromJSON(text: String) -> [String] {
guard let data = text.data(using: .utf8),
let object = try? JSONSerialization.jsonObject(with: data, options: [])
else {
return []
}
var results: [String] = []
self.collectWorkspaceIDs(object: object, out: &results)
return results
}
private static func collectWorkspaceIDs(object: Any, out: inout [String]) {
if let dict = object as? [String: Any] {
for (_, value) in dict {
self.collectWorkspaceIDs(object: value, out: &out)
}
return
}
if let array = object as? [Any] {
for value in array {
self.collectWorkspaceIDs(object: value, out: &out)
}
return
}
if let string = object as? String,
string.hasPrefix("wrk_"),
!out.contains(string)
{
out.append(string)
}
}
private static func fetchUsagePage(
workspaceID: String,
cookieHeader: String,
timeout: TimeInterval,
session: URLSession) async throws -> String
{
let url = URL(string: "https://opencode.ai/workspace/\(workspaceID)/go") ?? self.baseURL
let text = try await self.fetchPageText(
url: url,
cookieHeader: cookieHeader,
timeout: timeout,
session: session)
if self.looksSignedOut(text: text) {
throw OpenCodeGoUsageError.invalidCredentials
}
guard self.parseSubscriptionJSON(text: text, now: Date()) != nil ||
self.extractDouble(
pattern: #"rollingUsage[^}]*?usagePercent\s*:\s*([0-9]+(?:\.[0-9]+)?)"#,
text: text) != nil
else {
Self.log.error("OpenCode Go usage page payload missing usage fields.")
throw OpenCodeGoUsageError.parseFailed("Missing usage fields.")
}
return text
}
static func parseSubscription(text: String, now: Date) throws -> OpenCodeGoUsageSnapshot {
if let snapshot = self.parseSubscriptionJSON(text: text, now: now) {
return snapshot
}
guard let rollingPercent = self.extractDouble(
pattern: #"rollingUsage[^}]*?usagePercent\s*:\s*([0-9]+(?:\.[0-9]+)?)"#,
text: text),
let rollingReset = self.extractInt(
pattern: #"rollingUsage[^}]*?resetInSec\s*:\s*([0-9]+)"#,
text: text),
let weeklyPercent = self.extractDouble(
pattern: #"weeklyUsage[^}]*?usagePercent\s*:\s*([0-9]+(?:\.[0-9]+)?)"#,
text: text),
let weeklyReset = self.extractInt(
pattern: #"weeklyUsage[^}]*?resetInSec\s*:\s*([0-9]+)"#,
text: text)
else {
throw OpenCodeGoUsageError.parseFailed("Missing usage fields.")
}
let monthlyPercent = self.extractDouble(
pattern: #"monthlyUsage[^}]*?usagePercent\s*:\s*([0-9]+(?:\.[0-9]+)?)"#,
text: text)
let monthlyReset = self.extractInt(
pattern: #"monthlyUsage[^}]*?resetInSec\s*:\s*([0-9]+)"#,
text: text)
return OpenCodeGoUsageSnapshot(
hasMonthlyUsage: monthlyPercent != nil || monthlyReset != nil,
rollingUsagePercent: rollingPercent,
weeklyUsagePercent: weeklyPercent,
monthlyUsagePercent: monthlyPercent ?? 0,
rollingResetInSec: rollingReset,
weeklyResetInSec: weeklyReset,
monthlyResetInSec: monthlyReset ?? 0,
updatedAt: now)
}
private static func parseSubscriptionJSON(text: String, now: Date) -> OpenCodeGoUsageSnapshot? {
guard let data = text.data(using: .utf8),
let object = try? JSONSerialization.jsonObject(with: data, options: []),
let dict = object as? [String: Any]
else {
return nil
}
if let snapshot = self.parseUsageDictionary(dict, now: now) {
return snapshot
}
for key in ["data", "result", "usage", "billing", "payload"] {
if let nested = dict[key] as? [String: Any],
let snapshot = self.parseUsageDictionary(nested, now: now)
{
return snapshot
}
}
if let snapshot = self.parseUsageNested(dict, now: now, depth: 0) {
return snapshot
}
return self.parseUsageFromCandidates(object: object, now: now)
}
private static func parseUsageDictionary(_ dict: [String: Any], now: Date) -> OpenCodeGoUsageSnapshot? {
if let usage = dict["usage"] as? [String: Any],
let snapshot = self.parseUsageDictionary(usage, now: now)
{
return snapshot
}
let rollingKeys = ["rollingUsage", "rolling", "rolling_usage", "rollingWindow", "rolling_window"]
let weeklyKeys = ["weeklyUsage", "weekly", "weekly_usage", "weeklyWindow", "weekly_window"]
let monthlyKeys = ["monthlyUsage", "monthly", "monthly_usage", "monthlyWindow", "monthly_window"]
let rolling = self.firstDict(from: dict, keys: rollingKeys)
let weekly = self.firstDict(from: dict, keys: weeklyKeys)
let monthly = self.firstDict(from: dict, keys: monthlyKeys)
guard let rolling, let weekly else { return nil }
return self.buildSnapshot(rolling: rolling, weekly: weekly, monthly: monthly, now: now)
}
private static func parseUsageNested(_ dict: [String: Any], now: Date, depth: Int) -> OpenCodeGoUsageSnapshot? {
if depth > 3 { return nil }
var rolling: [String: Any]?
var weekly: [String: Any]?
var monthly: [String: Any]?
for (key, value) in dict {
guard let sub = value as? [String: Any] else { continue }
let lower = key.lowercased()
if lower.contains("rolling") || lower.contains("hour") || lower.contains("5h") || lower.contains("5-hour") {
rolling = sub
} else if lower.contains("weekly") || lower.contains("week") {
weekly = sub
} else if lower.contains("monthly") || lower.contains("month") {
monthly = sub
}
}
if let rolling, let weekly,
let snapshot = self.buildSnapshot(rolling: rolling, weekly: weekly, monthly: monthly, now: now)
{
return snapshot
}
for value in dict.values {
if let sub = value as? [String: Any],
let snapshot = self.parseUsageNested(sub, now: now, depth: depth + 1)
{
return snapshot
}
}
return nil
}
private static func parseUsageFromCandidates(object: Any, now: Date) -> OpenCodeGoUsageSnapshot? {
let candidates = self.collectWindowCandidates(object: object, now: now)
guard !candidates.isEmpty else { return nil }
let rollingCandidates = candidates.filter { candidate in
candidate.pathLower.contains("rolling") ||
candidate.pathLower.contains("hour") ||
candidate.pathLower.contains("5h") ||
candidate.pathLower.contains("5-hour")
}
let weeklyCandidates = candidates.filter { candidate in
candidate.pathLower.contains("weekly") ||
candidate.pathLower.contains("week")
}
let monthlyCandidates = candidates.filter { candidate in
candidate.pathLower.contains("monthly") ||
candidate.pathLower.contains("month")
}
let rolling = self.pickCandidate(
preferred: rollingCandidates,
fallback: candidates,
pickShorter: true)
let weekly = self.pickCandidate(
from: weeklyCandidates.filter { candidate in
candidate.id != rolling?.id
},
pickShorter: false)
let monthly = self.pickCandidate(
from: monthlyCandidates.filter { candidate in
candidate.id != rolling?.id && candidate.id != weekly?.id
},
pickShorter: false)
guard let rolling, let weekly else { return nil }
return OpenCodeGoUsageSnapshot(
hasMonthlyUsage: monthly != nil,
rollingUsagePercent: rolling.percent,
weeklyUsagePercent: weekly.percent,
monthlyUsagePercent: monthly?.percent ?? 0,
rollingResetInSec: rolling.resetInSec,
weeklyResetInSec: weekly.resetInSec,
monthlyResetInSec: monthly?.resetInSec ?? 0,
updatedAt: now)
}
private struct WindowCandidate {
let id: UUID
let percent: Double
let resetInSec: Int
let pathLower: String
}
private static func collectWindowCandidates(object: Any, now: Date) -> [WindowCandidate] {
var candidates: [WindowCandidate] = []
self.collectWindowCandidates(object: object, now: now, path: [], out: &candidates)
return candidates
}
private static func collectWindowCandidates(
object: Any,
now: Date,
path: [String],
out: inout [WindowCandidate])
{
if let dict = object as? [String: Any] {
if let window = self.parseWindow(dict, now: now) {
let pathLower = path.joined(separator: ".").lowercased()
out.append(WindowCandidate(
id: UUID(),
percent: window.percent,
resetInSec: window.resetInSec,
pathLower: pathLower))
}
for (key, value) in dict {
self.collectWindowCandidates(object: value, now: now, path: path + [key], out: &out)
}
return
}
if let array = object as? [Any] {
for (index, value) in array.enumerated() {
self.collectWindowCandidates(
object: value,
now: now,
path: path + ["[\(index)]"],
out: &out)
}
}
}
private static func pickCandidate(
preferred: [WindowCandidate],
fallback: [WindowCandidate],
pickShorter: Bool,
excluding excluded: UUID? = nil) -> WindowCandidate?
{
let filteredPreferred = preferred.filter { $0.id != excluded }
if let picked = self.pickCandidate(from: filteredPreferred, pickShorter: pickShorter) {
return picked
}
let filteredFallback = fallback.filter { $0.id != excluded }
return self.pickCandidate(from: filteredFallback, pickShorter: pickShorter)
}
private static func pickCandidate(from candidates: [WindowCandidate], pickShorter: Bool) -> WindowCandidate? {
guard !candidates.isEmpty else { return nil }
let comparator: (WindowCandidate, WindowCandidate) -> Bool = { lhs, rhs in
if pickShorter {
if lhs.resetInSec == rhs.resetInSec { return lhs.percent > rhs.percent }
return lhs.resetInSec < rhs.resetInSec
}
if lhs.resetInSec == rhs.resetInSec { return lhs.percent > rhs.percent }
return lhs.resetInSec > rhs.resetInSec
}
return candidates.min(by: comparator)
}
private static func firstDict(from dict: [String: Any], keys: [String]) -> [String: Any]? {
for key in keys {
if let value = dict[key] as? [String: Any] {
return value
}
}
return nil
}
private static func buildSnapshot(
rolling: [String: Any],
weekly: [String: Any],
monthly: [String: Any]?,
now: Date) -> OpenCodeGoUsageSnapshot?
{
guard let rollingWindow = self.parseWindow(rolling, now: now),
let weeklyWindow = self.parseWindow(weekly, now: now)
else {
return nil
}
let monthlyWindow = monthly.flatMap { self.parseWindow($0, now: now) }
return OpenCodeGoUsageSnapshot(
hasMonthlyUsage: monthlyWindow != nil,
rollingUsagePercent: rollingWindow.percent,
weeklyUsagePercent: weeklyWindow.percent,
monthlyUsagePercent: monthlyWindow?.percent ?? 0,
rollingResetInSec: rollingWindow.resetInSec,
weeklyResetInSec: weeklyWindow.resetInSec,
monthlyResetInSec: monthlyWindow?.resetInSec ?? 0,
updatedAt: now)
}
private static func parseWindow(_ dict: [String: Any], now: Date) -> (percent: Double, resetInSec: Int)? {
var percent: Double?
for key in self.percentKeys {
if let value = self.doubleValue(from: dict[key]) {
percent = value
break
}
}
if percent == nil {
let usedKeys = ["used", "usage", "consumed", "count", "usedTokens"]
let limitKeys = ["limit", "total", "quota", "max", "cap", "tokenLimit"]
var used: Double?
for key in usedKeys {
if let value = self.doubleValue(from: dict[key]) {
used = value
break
}
}
var limit: Double?
for key in limitKeys {
if let value = self.doubleValue(from: dict[key]) {
limit = value
break
}
}
if let used, let limit, limit > 0 {
percent = (used / limit) * 100
}
}
guard var resolvedPercent = percent else { return nil }
if resolvedPercent <= 1.0, resolvedPercent >= 0 {
resolvedPercent *= 100
}
resolvedPercent = max(0, min(100, resolvedPercent))
var resetInSec: Int?
for key in self.resetInKeys {
if let value = self.intValue(from: dict[key]) {
resetInSec = value
break
}
}
if resetInSec == nil {
for key in self.resetAtKeys {
if let resetAt = self.dateValue(from: dict[key]) {
resetInSec = max(0, Int(resetAt.timeIntervalSince(now)))
break
}
}
}
let resolvedReset = max(0, resetInSec ?? 0)
return (resolvedPercent, resolvedReset)
}
private static func fetchServerText(
request serverRequest: ServerRequest,
cookieHeader: String,
timeout: TimeInterval,
session: URLSession) async throws -> String
{
let url = self.serverRequestURL(
serverID: serverRequest.serverID,
args: serverRequest.args,
method: serverRequest.method)
var urlRequest = URLRequest(url: url)
urlRequest.httpMethod = serverRequest.method
urlRequest.timeoutInterval = timeout
urlRequest.setValue(cookieHeader, forHTTPHeaderField: "Cookie")
urlRequest.setValue(serverRequest.serverID, forHTTPHeaderField: "X-Server-Id")
urlRequest.setValue("server-fn:\(UUID().uuidString)", forHTTPHeaderField: "X-Server-Instance")
urlRequest.setValue(self.userAgent, forHTTPHeaderField: "User-Agent")
urlRequest.setValue(self.baseURL.absoluteString, forHTTPHeaderField: "Origin")
urlRequest.setValue(serverRequest.referer.absoluteString, forHTTPHeaderField: "Referer")
urlRequest.setValue("text/javascript, application/json;q=0.9, */*;q=0.8", forHTTPHeaderField: "Accept")
if serverRequest.method.uppercased() != "GET",
let args = serverRequest.args
{
urlRequest.httpBody = args.data(using: .utf8)
urlRequest.setValue("application/json", forHTTPHeaderField: "Content-Type")
}
let (data, response) = try await session.data(for: urlRequest)
guard let httpResponse = response as? HTTPURLResponse else {
throw OpenCodeGoUsageError.networkError("Invalid response")
}
guard httpResponse.statusCode == 200 else {
let bodyText = String(data: data, encoding: .utf8) ?? ""
let contentType = httpResponse.value(forHTTPHeaderField: "Content-Type") ?? "unknown"
Self.log.error("OpenCode Go returned \(httpResponse.statusCode) (type=\(contentType) length=\(data.count))")
if self.looksSignedOut(text: bodyText) {
throw OpenCodeGoUsageError.invalidCredentials
}
if httpResponse.statusCode == 401 || httpResponse.statusCode == 403 {
throw OpenCodeGoUsageError.invalidCredentials
}
if let message = self.extractServerErrorMessage(from: bodyText) {
throw OpenCodeGoUsageError.apiError("HTTP \(httpResponse.statusCode): \(message)")
}
throw OpenCodeGoUsageError.apiError("HTTP \(httpResponse.statusCode)")
}
guard let text = String(data: data, encoding: .utf8) else {
throw OpenCodeGoUsageError.parseFailed("Response was not UTF-8.")
}
return text
}
private static func fetchPageText(
url: URL,
cookieHeader: String,
timeout: TimeInterval,
session: URLSession) async throws -> String
{
var request = URLRequest(url: url)
request.httpMethod = "GET"
request.timeoutInterval = timeout
request.setValue(cookieHeader, forHTTPHeaderField: "Cookie")
request.setValue(self.userAgent, forHTTPHeaderField: "User-Agent")
request.setValue(
"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
forHTTPHeaderField: "Accept")
let (data, response) = try await session.data(for: request)
guard let httpResponse = response as? HTTPURLResponse else {
throw OpenCodeGoUsageError.networkError("Invalid response")
}
guard httpResponse.statusCode == 200 else {
let bodyText = String(data: data, encoding: .utf8) ?? ""
if self.looksSignedOut(text: bodyText) {
throw OpenCodeGoUsageError.invalidCredentials
}
if httpResponse.statusCode == 401 || httpResponse.statusCode == 403 {
throw OpenCodeGoUsageError.invalidCredentials
}
if let message = self.extractServerErrorMessage(from: bodyText) {
throw OpenCodeGoUsageError.apiError("HTTP \(httpResponse.statusCode): \(message)")
}
throw OpenCodeGoUsageError.apiError("HTTP \(httpResponse.statusCode)")
}
guard let text = String(data: data, encoding: .utf8) else {
throw OpenCodeGoUsageError.parseFailed("Response was not UTF-8.")
}
return text
}
private static func serverRequestURL(serverID: String, args: String?, method: String) -> URL {
guard method.uppercased() == "GET" else {
return self.serverURL
}
var components = URLComponents(url: self.serverURL, resolvingAgainstBaseURL: false)
var queryItems = [URLQueryItem(name: "id", value: serverID)]
if let args, !args.isEmpty {
queryItems.append(URLQueryItem(name: "args", value: args))
}
components?.queryItems = queryItems
return components?.url ?? self.serverURL
}
private static func looksSignedOut(text: String) -> Bool {
let lower = text.lowercased()
return lower.contains("login") || lower.contains("sign in") || lower.contains("auth/authorize")
}
private static func extractServerErrorMessage(from text: String) -> String? {
guard let data = text.data(using: .utf8),
let object = try? JSONSerialization.jsonObject(with: data, options: [])
else {
if let match = text.range(of: #"(?i)<title>([^<]+)</title>"#, options: .regularExpression) {
return String(text[match].dropFirst(7).dropLast(8)).trimmingCharacters(in: .whitespacesAndNewlines)
}
return nil
}
guard let dict = object as? [String: Any] else { return nil }
if let message = dict["message"] as? String, !message.isEmpty {
return message
}
if let error = dict["error"] as? String, !error.isEmpty {
return error
}
if let detail = dict["detail"] as? String, !detail.isEmpty {
return detail
}
return nil
}
private static func extractDouble(pattern: String, text: String) -> Double? {
guard let regex = try? NSRegularExpression(pattern: pattern, options: []) else { return nil }
let nsrange = NSRange(text.startIndex..<text.endIndex, in: text)
guard let match = regex.firstMatch(in: text, options: [], range: nsrange),
let range = Range(match.range(at: 1), in: text)
else {
return nil
}
return Double(text[range])
}
private static func extractInt(pattern: String, text: String) -> Int? {
guard let regex = try? NSRegularExpression(pattern: pattern, options: []) else { return nil }
let nsrange = NSRange(text.startIndex..<text.endIndex, in: text)
guard let match = regex.firstMatch(in: text, options: [], range: nsrange),
let range = Range(match.range(at: 1), in: text)
else {
return nil
}
return Int(text[range])
}
private static func doubleValue(from value: Any?) -> Double? {
switch value {
case let number as Double:
number
case let number as NSNumber:
number.doubleValue
case let string as String:
Double(string.trimmingCharacters(in: .whitespacesAndNewlines))
default:
nil
}
}
private static func intValue(from value: Any?) -> Int? {
switch value {
case let number as Int:
number
case let number as NSNumber:
number.intValue
case let string as String:
Int(string.trimmingCharacters(in: .whitespacesAndNewlines))
default:
nil
}
}
private static func dateValue(from value: Any?) -> Date? {
guard let value else { return nil }
if let number = self.doubleValue(from: value) {
if number > 1_000_000_000_000 {
return Date(timeIntervalSince1970: number / 1000)
}
if number > 1_000_000_000 {
return Date(timeIntervalSince1970: number)
}
}
if let string = value as? String {
if let number = Double(string.trimmingCharacters(in: .whitespacesAndNewlines)) {
return self.dateValue(from: number)
}
let formatter = ISO8601DateFormatter()
formatter.formatOptions = [.withInternetDateTime, .withFractionalSeconds]
if let parsed = formatter.date(from: string) {
return parsed
}
}
return nil
}
}