Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 86f6523

Browse files
Update OpenAIDefaultLoader.swift
1 parent 328d126 commit 86f6523

File tree

1 file changed

+18
-13
lines changed

1 file changed

+18
-13
lines changed

‎Sources/openai-async-image-swiftui/viewModel/OpenAIDefaultLoader.swift‎

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//
22
// OpenAIViewModel.swift
3-
//
3+
//
44
//
55
// Created by Igor on 28.02.2023.
66
//
@@ -19,13 +19,14 @@ import AppKit.NSImage
1919
@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *)
2020
public final class OpenAIDefaultLoader : IOpenAILoader{
2121

22-
/// Http async client
22+
/// HTTP async client to handle requests
2323
private let client : Http.Proxy<JsonReader, JsonWriter>?
2424

25-
/// Set of params for making requests
25+
/// Endpoint parameters required for making requests
2626
private let endpoint : IOpenAIImageEndpoint
2727

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
2930
public init(endpoint : IOpenAIImageEndpoint) {
3031

3132
self.endpoint = endpoint
@@ -38,33 +39,37 @@ public final class OpenAIDefaultLoader : IOpenAILoader{
3839
client = Http.Proxy(baseURL: url)
3940
}
4041

41-
/// Load image by text
42+
/// Loads an image from the OpenAI API based on a text prompt
4243
/// - 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
4647
public func load(
4748
_ prompt : String,
4849
with size : OpenAIImageSize
4950
) async throws -> Image{
5051

52+
// Prepare the request body with the prompt and size
5153
let body = Input(prompt: prompt, size: size, response_format: .b64, n: 1)
5254

55+
// Set the request headers, including authorization
5356
let headers = ["Content-Type": "application/json","Authorization": "Bearer \(endpoint.apiKey)"]
5457
let path = endpoint.path
5558

5659
guard let client = client else{
5760
throw AsyncImageErrors.clientIsNotDefined
5861
}
5962

63+
// Send the request and get the response
6064
let result: Http.Response<Output> = try await client.post(path: path, body: body, headers: headers)
6165

66+
// Convert the response to an image
6267
return try imageBase64(from: result.value)
6368
}
6469

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
6873
private func decodeBase64(from output: Output) throws -> Data?{
6974
guard let base64 = output.firstImage else {
7075
throw AsyncImageErrors.returnedNoImages
@@ -74,7 +79,7 @@ public final class OpenAIDefaultLoader : IOpenAILoader{
7479
}
7580

7681
#if os(iOS) || os(watchOS) || os(tvOS)
77-
/// Base64 encoder for iOS
82+
/// Converts base64 encoded string to UIImage for iOS
7883
/// - Parameter output: OpenAI response type
7984
/// - Returns: UIImage
8085
private func imageBase64(from output: Output) throws -> Image {
@@ -90,7 +95,7 @@ public final class OpenAIDefaultLoader : IOpenAILoader{
9095
#endif
9196

9297
#if os(macOS)
93-
/// Base64 encoder for macOS
98+
/// Converts base64 encoded string to NSImage for macOS
9499
/// - Parameter output: OpenAI response type
95100
/// - Returns: NSImage
96101
private func imageBase64(from output: Output) throws -> Image {

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /