1
1
//
2
2
// OpenAIViewModel.swift
3
- //
3
+ //
4
4
//
5
5
// Created by Igor on 28.02.2023.
6
6
//
@@ -19,13 +19,14 @@ import AppKit.NSImage
19
19
@available ( iOS 15 . 0 , macOS 12 . 0 , tvOS 15 . 0 , watchOS 8 . 0 , * )
20
20
public final class OpenAIDefaultLoader : IOpenAILoader {
21
21
22
- /// Http async client
22
+ /// HTTP async client to handle requests
23
23
private let client : Http . Proxy < JsonReader , JsonWriter > ?
24
24
25
- /// Set of params for making requests
25
+ /// Endpoint parameters required for making requests
26
26
private let endpoint : IOpenAIImageEndpoint
27
27
28
- /// - Parameter endpoint: Set of params for making requests
28
+ /// Initializes the loader with endpoint parameters
29
+ /// - Parameter endpoint: Set of parameters for making requests
29
30
public init ( endpoint : IOpenAIImageEndpoint ) {
30
31
31
32
self . endpoint = endpoint
@@ -38,33 +39,37 @@ public final class OpenAIDefaultLoader : IOpenAILoader{
38
39
client = Http . Proxy ( baseURL: url)
39
40
}
40
41
41
- /// Load image by text
42
+ /// Loads an image from the OpenAI API based on a text prompt
42
43
/// - Parameters:
43
- /// - prompt: Text
44
- /// - size: Image size
45
- /// - Returns: Open AI Image
44
+ /// - prompt: The text prompt describing the desired image
45
+ /// - size: The size of the generated image
46
+ /// - Returns: OpenAI Image
46
47
public func load(
47
48
_ prompt : String ,
48
49
with size : OpenAIImageSize
49
50
) async throws -> Image {
50
51
52
+ // Prepare the request body with the prompt and size
51
53
let body = Input ( prompt: prompt, size: size, response_format: . b64, n: 1 )
52
54
55
+ // Set the request headers, including authorization
53
56
let headers = [ " Content-Type " : " application/json " , " Authorization " : " Bearer \( endpoint. apiKey) " ]
54
57
let path = endpoint. path
55
58
56
59
guard let client = client else {
57
60
throw AsyncImageErrors . clientIsNotDefined
58
61
}
59
62
63
+ // Send the request and get the response
60
64
let result : Http . Response < Output > = try await client. post ( path: path, body: body, headers: headers)
61
65
66
+ // Convert the response to an image
62
67
return try imageBase64 ( from: result. value)
63
68
}
64
69
65
- /// Decode base64 to Data
66
- /// - Parameter output: Received format from the endpoint
67
- /// - Returns: Decoded data
70
+ /// Decodes base64 encoded string to Data
71
+ /// - Parameter output: The output received from the endpoint
72
+ /// - Returns: Decoded Data
68
73
private func decodeBase64( from output: Output ) throws -> Data ? {
69
74
guard let base64 = output. firstImage else {
70
75
throw AsyncImageErrors . returnedNoImages
@@ -74,7 +79,7 @@ public final class OpenAIDefaultLoader : IOpenAILoader{
74
79
}
75
80
76
81
#if os(iOS) || os(watchOS) || os(tvOS)
77
- /// Base64 encoder for iOS
82
+ /// Converts base64 encoded string to UIImage for iOS
78
83
/// - Parameter output: OpenAI response type
79
84
/// - Returns: UIImage
80
85
private func imageBase64( from output: Output ) throws -> Image {
@@ -90,7 +95,7 @@ public final class OpenAIDefaultLoader : IOpenAILoader{
90
95
#endif
91
96
92
97
#if os(macOS)
93
- /// Base64 encoder for macOS
98
+ /// Converts base64 encoded string to NSImage for macOS
94
99
/// - Parameter output: OpenAI response type
95
100
/// - Returns: NSImage
96
101
private func imageBase64( from output: Output ) throws -> Image {
0 commit comments