@@ -121,61 +121,55 @@ extension HTTPClient {
121121 httpClientProvider: . createNew
122122 )
123123
124- defer {
125- Task {
126- try await awsClient. shutdown ( )
124+ return try await run {
125+ // Extract region from URL
126+ let urlComponents = URLComponents ( string: url. string)
127+ guard let host = urlComponents? . host else {
128+ throw AppError . genericError ( nil , " Invalid URL: \( url. string) " )
127129 }
128- }
129-
130- // Extract region from URL
131- let urlComponents = URLComponents ( string: url. string)
132- guard let host = urlComponents? . host else {
133- throw AppError . genericError ( nil , " Invalid URL: \( url. string) " )
134- }
135130
136- let region = extractRegionFromHost ( host) ?? . useast2
137-
138- // Get credentials from the credential provider
139- let credentials : Credential
140- do {
141- credentials = try await awsClient. credentialProvider. getCredential (
142- on: awsClient. eventLoopGroup. next ( ) ,
143- logger: Logger ( label: " aws-credentials " )
144- ) . get ( )
145- } catch {
146- throw AppError . genericError ( nil , " Failed to retrieve AWS credentials: \( error) " )
147- }
131+ let region = extractRegionFromHost ( host) ?? . useast2
132+
133+ // Get credentials from the credential provider
134+ let credentials : Credential
135+ do {
136+ credentials = try await awsClient. credentialProvider. getCredential (
137+ on: awsClient. eventLoopGroup. next ( ) ,
138+ logger: Logger ( label: " aws-credentials " )
139+ ) . get ( )
140+ } catch {
141+ throw AppError . genericError ( nil , " Failed to retrieve AWS credentials: \( error) " )
142+ }
148143
149- // Use Soto's AWSSigner to sign the request headers
150- let signer = AWSSigner (
151- credentials: credentials,
152- name: " execute-api " ,
153- region: region. rawValue
154- )
144+ // Use Soto's AWSSigner to sign the request headers
145+ let signer = AWSSigner (
146+ credentials: credentials,
147+ name: " execute-api " ,
148+ region: region. rawValue
149+ )
155150
156- // Create the request URL
157- guard let requestURL = URL ( string: url. string) else {
158- throw AppError . genericError ( nil , " Invalid URL: \( url. string) " )
159- }
151+ // Create the request URL
152+ guard let requestURL = URL ( string: url. string) else {
153+ throw AppError . genericError ( nil , " Invalid URL: \( url. string) " )
154+ }
160155
161- // Sign the headers for the request
162- let signedHeaders : HTTPHeaders
163- do {
164- signedHeaders = try await signer. signHeaders (
156+ // Sign the headers for the request
157+ let signedHeaders : HTTPHeaders
158+ signedHeaders = signer. signHeaders (
165159 url: requestURL,
166160 method: HTTPMethod . GET,
167161 headers: HTTPHeaders ( ) ,
168162 body: nil
169163 )
170- } catch {
171- throw AppError . genericError ( nil , " Failed to sign request: \( error) " )
172- }
173164
174- // Create request with signed headers
175- let request = try Request ( url: url. string, method: . GET, headers: signedHeaders)
165+ // Create request with signed headers
166+ let request = try Request ( url: url. string, method: . GET, headers: signedHeaders)
176167
177- // Execute the request
178- return try await shared. execute ( request: request) . get ( )
168+ // Execute the request
169+ return try await shared. execute ( request: request) . get ( )
170+ } defer: {
171+ try await awsClient. shutdown ( )
172+ }
179173 }
180174
181175 private static func extractRegionFromHost( _ host: String ) -> Region ? {
0 commit comments