Skip to content

Commit e254e87

Browse files
ayushi2103“Ayushi
andauthored
Create ETCD get / set functions (#4)
* Doc: Added a proposal for swift-etcd-client API * Update 0001-swift-etcd-api.md: made the doc for get function * updated key-val operation * feat: added docke-compose and dockerfile * feat: added docke-compose and dockerfile * fix: update dockerfile * feat: add proto and create etcd client * fix: Create correct proto files and add http and annotations * fix: fixed package.swift and created main entrypoint * fix: create get / set and executable target in package.swift * fix: Update ETCD client with PR comments * fix: PR comment fixes * fix: make ETCDValue conform to generic types * fix: Update ETCDClient and Example to use Sequence<UInt8> * Added tests to ETCD get / set methods * Add documentation for get / set * Cleaned up ETCDClient files and updated with copyright information. Deleted ETCDValue as it was no longer being used. * Rename ETCDGetSetTests * Update eventLoopGroup to singleton in test * Remove forced unwrap of Etcdserverpb_KVNIOClient --------- Co-authored-by: “Ayushi <“[email protected]”>
1 parent c613077 commit e254e87

File tree

13 files changed

+1915
-42
lines changed

13 files changed

+1915
-42
lines changed

Package.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ let package = Package(
3333
.package(url: "https://github.com/apple/swift-nio.git", from: "2.56.0"),
3434
.package(url: "https://github.com/apple/swift-log.git", from: "1.0.0"),
3535
.package(url: "https://github.com/swift-server/swift-service-lifecycle.git", from: "2.0.0"),
36+
.package(url: "https://github.com/grpc/grpc-swift.git", from: "1.21.0"),
37+
.package(url: "https://github.com/apple/swift-protobuf.git", from: "1.14.0"),
3638
],
3739
targets: [
3840
.target(
@@ -43,6 +45,8 @@ let package = Package(
4345
.product(name: "NIOEmbedded", package: "swift-nio"),
4446
.product(name: "Logging", package: "swift-log"),
4547
.product(name: "ServiceLifecycle", package: "swift-service-lifecycle"),
48+
.product(name: "GRPC", package: "grpc-swift"),
49+
.product(name: "SwiftProtobuf", package: "swift-protobuf"),
4650
]
4751
),
4852
.testTarget(
@@ -51,9 +55,7 @@ let package = Package(
5155
),
5256
.executableTarget(
5357
name: "ETCDExample",
54-
dependencies: [
55-
.target(name: "ETCD"),
56-
]
58+
dependencies: ["ETCD"]
5759
),
5860
]
5961
)

Sources/ETCD/ETCDClient.swift

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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+
}

Sources/ETCD/empty.swift

Lines changed: 0 additions & 13 deletions
This file was deleted.

Sources/ETCD/proto/annotations.proto

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright 2024 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
syntax = "proto3";
16+
17+
package google.api;
18+
19+
import "http.proto";
20+
import "google/protobuf/descriptor.proto";
21+
22+
option go_package = "google.golang.org/genproto/googleapis/api/annotations;annotations";
23+
option java_multiple_files = true;
24+
option java_outer_classname = "AnnotationsProto";
25+
option java_package = "com.google.api";
26+
option objc_class_prefix = "GAPI";
27+
28+
extend google.protobuf.MethodOptions {
29+
// See `HttpRule`.
30+
HttpRule http = 72295728;
31+
}

0 commit comments

Comments
 (0)