-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a828554
commit c40a26e
Showing
8 changed files
with
229 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
Sources/OpenAIService/Networking/MultipartFormDataRequest.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
// | ||
// MultipartFormDataRequest.swift | ||
// OpenAIDemo | ||
// | ||
// Created by Gusakovsky, Sergey on 5.03.23. | ||
// | ||
|
||
import Foundation | ||
|
||
struct MultipartFormDataRequest { | ||
private let boundary: String = UUID().uuidString | ||
private let body = NSMutableData() | ||
let endpoint: OpenAIEndpoint | ||
|
||
init(endpoint: OpenAIEndpoint) { | ||
self.endpoint = endpoint | ||
} | ||
|
||
func asURLRequest() -> URLRequest? { | ||
guard let baseUrl = URL(string: endpoint.baseURL()) else { | ||
return nil | ||
} | ||
|
||
guard var urlComponents = URLComponents(url: baseUrl, resolvingAgainstBaseURL: true) else { | ||
return nil | ||
} | ||
|
||
urlComponents.path = endpoint.path | ||
|
||
guard let url = urlComponents.url else { | ||
return nil | ||
} | ||
|
||
var request = URLRequest(url: url) | ||
|
||
request.httpMethod = endpoint.method.rawValue | ||
request.setValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type") | ||
|
||
self.body.append("--\(boundary)--") | ||
request.httpBody = self.body as Data | ||
|
||
return request | ||
} | ||
|
||
func addTextField(named name: String, value: String) { | ||
self.body.append(textFormField(named: name, value: value)) | ||
} | ||
|
||
func addDataField(named name: String, formData: FormData) { | ||
self.body.append(dataFormField(named: name, formData: formData)) | ||
} | ||
|
||
private func textFormField(named name: String, value: String) -> String { | ||
var fieldString = "--\(boundary)\r\n" | ||
fieldString += "Content-Disposition: form-data; name=\"\(name)\"\r\n" | ||
fieldString += "\r\n" | ||
fieldString += "\(value)\r\n" | ||
|
||
return fieldString | ||
} | ||
|
||
private func dataFormField(named name: String, formData: FormData) -> Data { | ||
let fieldData = NSMutableData() | ||
|
||
fieldData.append("--\(boundary)\r\n") | ||
fieldData.append("Content-Disposition: form-data; name=\"\(name)\"; filename=\"\(formData.fileName)\"\r\n") | ||
fieldData.append("Content-Type: \(formData.mimeType)\r\n") | ||
fieldData.append("\r\n") | ||
fieldData.append(formData.data) | ||
fieldData.append("\r\n") | ||
|
||
return fieldData as Data | ||
} | ||
} | ||
|
||
extension NSMutableData { | ||
func append(_ string: String) { | ||
if let data = string.data(using: .utf8) { | ||
self.append(data) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
Sources/OpenAIService/Networking/OpenAIImageEditsBody.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
// | ||
// OpenAIImageEditsBody.swift | ||
// OpenAIDemo | ||
// | ||
// Created by Gusakovsky, Sergey on 5.03.23. | ||
// | ||
|
||
import Foundation | ||
#if os(iOS) | ||
import UIKit | ||
#endif | ||
|
||
public struct OpenAIImageEditsBody { | ||
public let image: FormData | ||
public let mask: FormData | ||
public let prompt: String | ||
public let numberOfImages: Int | ||
public let size: OpenAIGenerationImageSize | ||
public let responseFormat: OpenAIGenerationImageResponseFormat | ||
public let user: String? | ||
|
||
public init( | ||
image: Data, | ||
mask: Data, | ||
prompt: String, | ||
numberOfImages: Int = 1, | ||
size: OpenAIGenerationImageSize = .large, | ||
responseFormat: OpenAIGenerationImageResponseFormat = .url, | ||
user: String? = nil | ||
) { | ||
self.image = FormData(data: image, mimeType: "image/png", fileName: "image.png") | ||
self.mask = FormData(data: mask, mimeType: "image/png", fileName: "image.png") | ||
self.prompt = prompt | ||
self.numberOfImages = numberOfImages | ||
self.size = size | ||
self.responseFormat = responseFormat | ||
self.user = user | ||
} | ||
|
||
#if os(iOS) | ||
public init?( | ||
image: UIImage, | ||
mask: UIImage, | ||
prompt: String, | ||
numberOfImages: Int = 1, | ||
size: OpenAIGenerationImageSize = .large, | ||
responseFormat: OpenAIGenerationImageResponseFormat = .url, | ||
user: String? = nil | ||
) { | ||
guard let imageData = image.pngData() else { return nil } | ||
guard let maskData = mask.pngData() else { return nil } | ||
self.image = FormData(data: imageData, mimeType: "image/png", fileName: "image.png") | ||
self.mask = FormData(data: maskData, mimeType: "image/png", fileName: "image.png") | ||
self.prompt = prompt | ||
self.numberOfImages = numberOfImages | ||
self.size = size | ||
self.responseFormat = responseFormat | ||
self.user = user | ||
} | ||
#endif | ||
|
||
public var body: [String: Any] { | ||
var result: [String: Any] = [ | ||
"image": self.image, | ||
"mask": self.mask, | ||
"prompt": self.prompt, | ||
"n": self.numberOfImages, | ||
"size": self.size.rawValue, | ||
"response_format": self.responseFormat.rawValue | ||
] | ||
if let user = self.user { | ||
result["user"] = user | ||
} | ||
|
||
return result | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters