@@ -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}
0 commit comments