Skip to content

Commit

Permalink
Make Invocation.params optional
Browse files Browse the repository at this point in the history
  • Loading branch information
kolyasev committed Jul 10, 2022
1 parent 30d743d commit 1fa44f3
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Sources/SwiftJSONRPC/Client/RPCClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ open class RPCClient {
return Request(
id: identifier,
method: invocation.method,
params: try coder.encode(invocation.params)
params: try invocation.params.flatMap { try coder.encode($0) }
)
}

Expand Down
8 changes: 4 additions & 4 deletions Sources/SwiftJSONRPC/Invocation/Invocation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public struct Invocation<Params, Result> where Params: InvocationParams, Result:

// MARK: - Initialization

init(method: String, params: Params, resultType: Result.Type) {
init(method: String, params: Params?, resultType: Result.Type) {
self.method = method
self.params = params
}
Expand All @@ -19,12 +19,12 @@ public struct Invocation<Params, Result> where Params: InvocationParams, Result:

public let method: String

public let params: Params
public let params: Params?

}

public typealias InvocationParams = Encodable
public typealias InvocationResult = Decodable

public struct VoidInvocationParams: InvocationParams {}
public struct VoidInvocationResult: InvocationResult {}
struct VoidInvocationParams: InvocationParams {}
struct VoidInvocationResult: InvocationResult {}
12 changes: 3 additions & 9 deletions Sources/SwiftJSONRPC/Service/RPCService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,17 @@ open class RPCService {
return try await invoke(method, params: VoidInvocationParams())
}

open func invoke<Params>(_ method: String, params: Params) async throws where Params: InvocationParams {
open func invoke<Params>(_ method: String, params: Params?) async throws where Params: InvocationParams {
_ = try await invoke(method, params: params) as VoidInvocationResult
}

open func invoke<Params, Result>(_ method: String, params: Params) async throws -> Result
open func invoke<Params, Result>(_ method: String, params: Params?) async throws -> Result
where Params: InvocationParams, Result: InvocationResult {

// Init invocation object
let invocation = makeInvocation(method: method, params: params, resultType: Result.self)
let invocation = Invocation(method: method, params: params, resultType: Result.self)

// Perform invocation
return try await client.invoke(invocation)
}

// MARK: - Private Functions

private func makeInvocation<Params, Result>(method: String, params: Params, resultType: Result.Type) -> Invocation<Params, Result> {
return Invocation(method: method, params: params, resultType: resultType)
}
}

0 comments on commit 1fa44f3

Please sign in to comment.