|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the swift-etcd-client-gsoc open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2024 Apple Inc. and the swift-etcd-client-gsoc project authors |
| 6 | +// Licensed under Apache License v2.0 |
| 7 | +// |
| 8 | +// See LICENSE.txt for license information |
| 9 | +// See CONTRIBUTORS.txt for the list of swift-etcd-client-gsoc project authors |
| 10 | +// |
| 11 | +// SPDX-License-Identifier: Apache-2.0 |
| 12 | +// |
| 13 | +//===----------------------------------------------------------------------===// |
| 14 | + |
| 15 | +import Foundation |
| 16 | +import GRPC |
| 17 | +import NIO |
| 18 | +import SwiftProtobuf |
| 19 | + |
| 20 | +public final class EtcdClient: @unchecked Sendable { |
| 21 | + private let host: String |
| 22 | + private let port: Int |
| 23 | + private var group: EventLoopGroup |
| 24 | + private var connection: ClientConnection |
| 25 | + private var client: Etcdserverpb_KVNIOClient |
| 26 | + |
| 27 | + /// Initialize a new ETCD Connection. |
| 28 | + /// |
| 29 | + /// - Parameters: |
| 30 | + /// - host: The host address of the ETCD server. |
| 31 | + /// - port: The port number of the ETCD server. |
| 32 | + /// - eventLoopGroup: The event loop group to use for this connection. |
| 33 | + public init(host: String, port: Int, eventLoopGroup: EventLoopGroup) { |
| 34 | + self.host = host |
| 35 | + self.port = port |
| 36 | + self.group = eventLoopGroup |
| 37 | + self.connection = ClientConnection.insecure(group: self.group) |
| 38 | + .connect(host: host, port: port) |
| 39 | + self.client = Etcdserverpb_KVNIOClient(channel: self.connection) |
| 40 | + } |
| 41 | + |
| 42 | + /// Sets a value for a specified key in the ETCD server. |
| 43 | + /// |
| 44 | + /// - Parameters: |
| 45 | + /// - key: The key for which the value is to be set. Parameter is of type Sequence<UInt8>. |
| 46 | + /// - value: The ETCD value to set for the key. Parameter is of type Sequence<UInt8>. |
| 47 | + public func set(_ key: some Sequence<UInt8>, value: some Sequence<UInt8>) async throws { |
| 48 | + var putRequest = Etcdserverpb_PutRequest() |
| 49 | + putRequest.key = Data(key) |
| 50 | + putRequest.value = Data(value) |
| 51 | + let call = client.put(putRequest) |
| 52 | + _ = try await call.response.get() |
| 53 | + } |
| 54 | + |
| 55 | + /// Sets a value for a specified key in the ETCD server. |
| 56 | + /// |
| 57 | + /// - Parameters: |
| 58 | + /// - key: The key for which the value is to be set. Parameter is of type String, |
| 59 | + /// - value: The ETCD value to set for the key. Parameter is of type String. |
| 60 | + public func set(_ key: String, value: String) async throws { |
| 61 | + try await set(key.utf8, value: value.utf8) |
| 62 | + } |
| 63 | + |
| 64 | + /// Fetch the value for a key from the ETCD server. |
| 65 | + /// |
| 66 | + /// - Parameter key: The key to fetch the value for. Parameter is of type Sequence<UInt8>. |
| 67 | + /// - Returns: A `Value` containing the fetched value, or `nil` if no value was found. |
| 68 | + public func get(_ key: some Sequence<UInt8>) async throws -> Data? { |
| 69 | + var rangeRequest = Etcdserverpb_RangeRequest() |
| 70 | + rangeRequest.key = Data(key) |
| 71 | + |
| 72 | + let call = client.range(rangeRequest) |
| 73 | + let response = try await call.response.get() |
| 74 | + |
| 75 | + guard let kv = response.kvs.first else { |
| 76 | + return nil |
| 77 | + } |
| 78 | + return kv.value |
| 79 | + } |
| 80 | + |
| 81 | + /// Fetch the value for a key from the ETCD server. |
| 82 | + /// |
| 83 | + /// - Parameter key: The key to fetch the value for. Parameter is of type Sequence<UInt8>. |
| 84 | + /// - Returns: A `Value` containing the fetched value, or `nil` if no value was found. |
| 85 | + public func get(_ key: String) async throws -> Data? { |
| 86 | + return try await get(key.utf8) |
| 87 | + } |
| 88 | +} |
0 commit comments