-
Notifications
You must be signed in to change notification settings - Fork 0
/
FoundationImpl.swift
113 lines (99 loc) · 3.27 KB
/
FoundationImpl.swift
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
// (c) 2024 and onwards Pizza Studio (AGPL v3.0 License).
// ====================
// This code is released under the AGPL v3.0 License (SPDX-License-Identifier: AGPL-3.0)
import Foundation
#if canImport(FoundationNetworking)
import FoundationNetworking
#endif
#if canImport(WinSDK) || (!canImport(AppKit) && !canImport(UIKit) && !canImport(Glibc))
let isWindows = true
#else
let isWindows = false
#endif
/// An extension that provides async support for fetching a URL
///
/// Needed because the Linux version of Swift does not support async URLSession yet.
extension URLSession {
enum AsyncError: Error {
case invalidUrlResponse, missingResponseData
}
/// A reimplementation of `URLSession.shared.asyncData(from: url)` required for Linux
///
/// ref: https://gist.github.com/aronbudinszky/66cdb71d734ae48a2609c7f2c094a02d
///
/// - Parameter url: The URL for which to load data.
/// - Returns: Data and response.
///
/// - Usage:
///
/// let (data, response) = try await URLSession.shared.asyncData(from: url)
func asyncData(from url: URL) async throws -> (Data, URLResponse) {
guard !isWindows else {
return try await data(from: url)
}
return try await withCheckedThrowingContinuation { continuation in
let task = self.dataTask(with: url) { data, response, error in
if let error = error {
continuation.resume(throwing: error)
return
}
guard let response = response as? HTTPURLResponse else {
continuation.resume(throwing: AsyncError.invalidUrlResponse)
return
}
guard let data = data else {
continuation.resume(throwing: AsyncError.missingResponseData)
return
}
continuation.resume(returning: (data, response))
}
task.resume()
}
}
}
// MARK: - Internal Swift Extensions
extension Array where Element: Identifiable {
subscript(wrapping index: Element.ID) -> [Element] {
filter { $0.id == index }
}
}
// MARK: - Decoding Strategy for Decoding UpperCamenCases
extension JSONDecoder.KeyDecodingStrategy {
static var convertFromPascalCase: Self {
.custom { keys in
PascalCaseKey(stringValue: keys.last!.stringValue)
}
}
}
// MARK: - PascalCaseKey
struct PascalCaseKey: CodingKey {
// MARK: Lifecycle
init(stringValue str: String) {
let allCapicalized = str.filter(\.isLowercase).isEmpty
guard !allCapicalized else {
self.stringValue = str.lowercased()
self.intValue = nil
return
}
var count = 0
perCharCheck: for char in str {
if char.isUppercase {
count += 1
} else {
break perCharCheck
}
}
if count > 1 {
count -= 1
}
self.stringValue = str.prefix(count).lowercased() + str.dropFirst(count)
self.intValue = nil
}
init(intValue: Int) {
self.stringValue = String(intValue)
self.intValue = intValue
}
// MARK: Internal
let stringValue: String
let intValue: Int?
}