Skip to content

Commit 72e9fb0

Browse files
committed
Improved type handling when encoding/decoding
1 parent 76104a0 commit 72e9fb0

File tree

2 files changed

+20
-17
lines changed

2 files changed

+20
-17
lines changed

Sources/OpenAIGPT3/Client.swift

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,7 @@ extension Client {
5252
}
5353
}
5454

55-
/// Builds a ``URLRequest`` based on the ``Endpoint`` and `body` value.
56-
///
57-
/// - Parameter endpoint: The ``Endpoint`` being requested.
58-
/// - Parameter body: The ``Encodable`` value to send as the request body.
59-
private func buildRequest(to endpoint: Endpoint, body: Encodable? = nil) throws -> URLRequest {
55+
private func buildRequest(to endpoint: Endpoint) throws -> URLRequest {
6056
let urlStr = "\(BASE_URL)/\(endpoint)"
6157

6258
guard let url = URL(string: urlStr) else {
@@ -68,17 +64,24 @@ extension Client {
6864
if let organization = organization {
6965
request.setValue(organization, forHTTPHeaderField: "OpenAI-Organization")
7066
}
71-
if let body = body {
72-
request.setValue(APPLICATION_JSON, forHTTPHeaderField: "Content-Type")
73-
request.httpBody = try jsonEncodeData(body)
74-
}
67+
68+
return request
69+
}
70+
71+
/// Builds a ``URLRequest`` based on the ``Endpoint`` and `body` value.
72+
///
73+
/// - Parameter endpoint: The ``Endpoint`` being requested.
74+
/// - Parameter body: The ``Encodable`` value to send as the request body.
75+
private func buildRequest<B: Encodable>(to endpoint: Endpoint, body: B) throws -> URLRequest {
76+
var request = try buildRequest(to: endpoint)
77+
request.setValue(APPLICATION_JSON, forHTTPHeaderField: "Content-Type")
78+
request.httpBody = try jsonEncodeData(body)
7579

7680
return request
7781
}
7882

79-
private func executeRequest<T: Decodable>(to endpoint: Endpoint, body: Encodable? = nil, returning outputType: T.Type = T.self) async throws -> T {
83+
private func executeRequest<T: Decodable>(_ request: URLRequest, returning outputType: T.Type = T.self) async throws -> T {
8084
do {
81-
let request = try buildRequest(to: endpoint, body: body)
8285
self.log?("Request: \(request)")
8386
let (result, response) = try await URLSession.shared.data(for: request)
8487

@@ -112,30 +115,30 @@ extension Client {
112115
///
113116
/// - Returns the list of available ``Model``s.
114117
public func models() async throws -> [Model] {
115-
return try await executeRequest(to: .models)
118+
return try await executeRequest(buildRequest(to: .models))
116119
}
117120

118121
/// Requests the details for the specified ``Model/ID``.
119122
///
120123
/// - Parameter id: The ``Model/ID``
121124
/// - Returns the model details.
122125
public func model(for id: Model.ID) async throws -> Model {
123-
return try await executeRequest(to: .model(id))
126+
return try await executeRequest(buildRequest(to: .model(id)))
124127
}
125128

126129
/// Requests completions for the given request.
127130
///
128131
/// - Parameter request: The ``Completions/Request``.
129132
/// - Returns The ``Completions/Response``
130133
public func completions(for request: Completions.Request) async throws -> Completions.Response {
131-
return try await executeRequest(to: .completions, body: request)
134+
return try await executeRequest(buildRequest(to: .completions, body: request))
132135
}
133136

134137
/// Requests edits for the given request.
135138
///
136139
/// - Parameter request: The ``Edits/Request``
137140
/// - Returns the ``Edits/Response``
138141
public func edits(for request: Edits.Request) async throws -> Edits.Request {
139-
return try await executeRequest(to: .edits, body: request)
142+
return try await executeRequest(buildRequest(to: .edits, body: request))
140143
}
141144
}

Sources/OpenAIGPT3/Utils.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ import Foundation
44
///
55
/// - Parameter value: The value to encode
66
/// - Returns The encoded value.
7-
func jsonEncode(_ value: Encodable) throws -> String {
7+
func jsonEncode<T: Encodable>(_ value: T) throws -> String {
88
return try String(decoding: jsonEncodeData(value), as: UTF8.self)
99
}
1010

1111
/// Encodes the provided value to a JSON ``Data`` value
1212
///
1313
/// - Parameter value: The value to encode
1414
/// - Returns the encoded value.
15-
func jsonEncodeData(_ value: Encodable) throws -> Data {
15+
func jsonEncodeData<T: Encodable>(_ value: T) throws -> Data {
1616
let encoder = JSONEncoder()
1717
encoder.keyEncodingStrategy = .convertToSnakeCase
1818
encoder.dateEncodingStrategy = .custom({ date, encoder in

0 commit comments

Comments
 (0)