@@ -52,11 +52,7 @@ extension Client {
52
52
}
53
53
}
54
54
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 {
60
56
let urlStr = " \( BASE_URL) / \( endpoint) "
61
57
62
58
guard let url = URL ( string: urlStr) else {
@@ -68,17 +64,24 @@ extension Client {
68
64
if let organization = organization {
69
65
request. setValue ( organization, forHTTPHeaderField: " OpenAI-Organization " )
70
66
}
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)
75
79
76
80
return request
77
81
}
78
82
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 {
80
84
do {
81
- let request = try buildRequest ( to: endpoint, body: body)
82
85
self . log ? ( " Request: \( request) " )
83
86
let ( result, response) = try await URLSession . shared. data ( for: request)
84
87
@@ -112,30 +115,30 @@ extension Client {
112
115
///
113
116
/// - Returns the list of available ``Model``s.
114
117
public func models( ) async throws -> [ Model ] {
115
- return try await executeRequest ( to: . models)
118
+ return try await executeRequest ( buildRequest ( to: . models) )
116
119
}
117
120
118
121
/// Requests the details for the specified ``Model/ID``.
119
122
///
120
123
/// - Parameter id: The ``Model/ID``
121
124
/// - Returns the model details.
122
125
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) ) )
124
127
}
125
128
126
129
/// Requests completions for the given request.
127
130
///
128
131
/// - Parameter request: The ``Completions/Request``.
129
132
/// - Returns The ``Completions/Response``
130
133
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) )
132
135
}
133
136
134
137
/// Requests edits for the given request.
135
138
///
136
139
/// - Parameter request: The ``Edits/Request``
137
140
/// - Returns the ``Edits/Response``
138
141
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) )
140
143
}
141
144
}
0 commit comments