From 9d5544153c403e8fae71dee8c96d46a61d6a6f34 Mon Sep 17 00:00:00 2001 From: Ailton Vieira Date: Wed, 28 Jun 2023 20:08:42 -0300 Subject: [PATCH] Fix ServiceExecutor protocol to require implementation --- Sources/SwiftRequest/ServiceExecutor.swift | 16 ++++++++++------ .../ServiceMethodExpander.swift | 2 +- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/Sources/SwiftRequest/ServiceExecutor.swift b/Sources/SwiftRequest/ServiceExecutor.swift index 1c872df..a614480 100644 --- a/Sources/SwiftRequest/ServiceExecutor.swift +++ b/Sources/SwiftRequest/ServiceExecutor.swift @@ -5,19 +5,23 @@ public protocol ServiceExecutor { } extension ServiceExecutor { - public func execute(_ request: Request) async throws -> Output { - let (output, _): (Output, _) = try await self.execute(request) + public func callAsFunction(_ request: Request) async throws -> Output { + let (output, _): (Output, _) = try await self(request) return output } - public func execute(_ request: Request) async throws -> (Output, HTTPURLResponse) { - let (data, response) = try await self.execute(request) + public func callAsFunction(_ request: Request) async throws -> (Output, HTTPURLResponse) { + let (data, response): (Data, HTTPURLResponse) = try await self.execute(request) let output = try JSONDecoder().decode(Output.self, from: data) return (output, response) } - public func execute(_ request: Request) async throws -> Data { - let (data, _) = try await self.execute(request) + public func callAsFunction(_ request: Request) async throws -> Data { + let (data, _) = try await self(request) return data } + + public func callAsFunction(_ request: Request) async throws -> (Data, HTTPURLResponse) { + return try await execute(request) + } } diff --git a/Sources/SwiftRequestMacros/ServiceMethodExpander.swift b/Sources/SwiftRequestMacros/ServiceMethodExpander.swift index 6347c02..cc82cd0 100644 --- a/Sources/SwiftRequestMacros/ServiceMethodExpander.swift +++ b/Sources/SwiftRequestMacros/ServiceMethodExpander.swift @@ -52,7 +52,7 @@ class ServiceMethodExpander { let codeBlock = CodeBlockSyntax { VariableDeclSyntax(Keyword.let, name: "request", initializer: .init(value: request)) - ReturnStmtSyntax(expression: "try await executor.execute(request)" as ExprSyntax) + ReturnStmtSyntax(expression: "try await executor(request)" as ExprSyntax) } let newDeclaration = declaration