-
Notifications
You must be signed in to change notification settings - Fork 0
/
CompletionProvider.swift
66 lines (55 loc) · 2.24 KB
/
CompletionProvider.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
//
// CompletionProvider.swift
// EditorCore
//
// Created by Arthur Guiot on 6/8/22.
//
import Foundation
public class CompletionProvider {
public static let shared = CompletionProvider()
let baseURL = "https://Nota-gpt.pr1mer.tech"
// MARK: - Make a request
func fetch<T, E>(for decodingType: T.Type, at path: String, with body: E? = nil, using method: String = "GET") async throws -> T where T: Decodable, E: Encodable {
let url = URL(string: "\(baseURL)/\(path)")!
var request = URLRequest(url: url)
if let body = body {
let requestBody = try JSONEncoder().encode(body)
request.httpBody = requestBody
// Only for POST
request.httpMethod = method
}
let (data, _) = try await URLSession.shared.data(for: request)
let decoded = try JSONDecoder().decode(decodingType, from: data)
return decoded
}
struct DefaultBody: Codable {}
func get<T>(for decodingType: T.Type, at path: String) async throws -> T where T: Decodable {
let url = URL(string: "\(baseURL)/\(path)")!
let request = URLRequest(url: url)
let (data, _) = try await URLSession.shared.data(for: request)
let decoded = try JSONDecoder().decode(decodingType, from: data)
return decoded
}
// MARK: - Quick Methods
// Models
public struct ModelsResponse: Codable {
var object = "list"
public struct Model: Codable {
let id: String
var object = "model"
}
let data: [Model]
}
public func getModels() async throws -> [ModelsResponse.Model] {
let response = try await get(for: ModelsResponse.self, at: "models")
return response.data
}
// MARK: - Security
static public func hardwareUUID() -> String? {
let matchingDict = IOServiceMatching("IOPlatformExpertDevice")
let platformExpert = IOServiceGetMatchingService(kIOMainPortDefault, matchingDict)
defer{ IOObjectRelease(platformExpert) }
guard platformExpert != 0 else { return nil }
return IORegistryEntryCreateCFProperty(platformExpert, kIOPlatformUUIDKey as CFString, kCFAllocatorDefault, 0).takeRetainedValue() as? String
}
}