|
1 | 1 | # 🚧WIP🚧: Swift ETCD client
|
2 | 2 |
|
3 |
| -ETCD is a Swift Package in development that provides a convenient way to communicate with [etcd](https://etcd.io) servers. |
| 3 | +Swift ETCD is a Swift Package that provides a convenient way to communicate with [etcd](https://etcd.io) servers. |
4 | 4 |
|
5 | 5 | ## Getting Started
|
| 6 | +To depend on Swift ETCD within your own project, you can add a `dependencies` clause to your `Package.swift` |
| 7 | +``` |
| 8 | +dependencies: [ |
| 9 | + .package(url: "https://github.com/swift-server/swift-etcd-client-gsoc") |
| 10 | +] |
| 11 | +``` |
6 | 12 |
|
7 | 13 | ## Overview
|
| 14 | +The ETCD Client allows for communication with an ETCD Server. The Client takes care of establishing a connection, and handling asynchronous execution of commands. |
8 | 15 |
|
9 |
| -TBD |
| 16 | +Here's an example of how you can use the ETCD Client |
10 | 17 |
|
11 |
| -## Contributing |
| 18 | +```swift |
| 19 | +struct Example { |
| 20 | + static func main() async throws { |
| 21 | + let eventLoopGroup = MultiThreadedEventLoopGroup.singleton |
| 22 | + do { |
| 23 | + let etcdClient = EtcdClient(host: "localhost", port: 2379, eventLoopGroup: eventLoopGroup) |
| 24 | + try await withThrowingTaskGroup(of: Void.self) { group in |
| 25 | + group.addTask { |
| 26 | + do { |
| 27 | + try await etcdClient.watch("foo") { sequence in |
| 28 | + var iterator = sequence.makeAsyncIterator() |
| 29 | + while let event = try await iterator.next() { |
| 30 | + print(event) |
| 31 | + } |
| 32 | + } |
| 33 | + } catch { |
| 34 | + print("Error watching key: \(error)") |
| 35 | + } |
| 36 | + } |
| 37 | + // Sleeping for a second to let the watch above setup |
| 38 | + try await Task.sleep(for: .seconds(1)) |
| 39 | + |
| 40 | + try await etcdClient.set("foo", value: "bar") |
| 41 | + let key = "foo".data(using: .utf8)! |
| 42 | + let rangeRequest = RangeRequest(key: key) |
| 43 | + if let value = try await etcdClient.getRange(rangeRequest) { |
| 44 | + if let stringValue = String(data: value, encoding: .utf8) { |
| 45 | + print("Value is: \(stringValue)") |
| 46 | + let deleteRangeRequest = DeleteRangeRequest(key: key) |
| 47 | + try await etcdClient.deleteRange(deleteRangeRequest) |
| 48 | + print("Key deleted") |
| 49 | + |
| 50 | + // Trying to get the value again |
| 51 | + let deletedValue = try await etcdClient.getRange(rangeRequest) |
| 52 | + if deletedValue == nil { |
| 53 | + print("Key not found after deletion") |
| 54 | + } else { |
| 55 | + print("Value after deletion: \(deletedValue!)") |
| 56 | + } |
| 57 | + } else { |
| 58 | + print("Unable to get value") |
| 59 | + } |
| 60 | + } else { |
| 61 | + print("Key not found") |
| 62 | + } |
| 63 | + try await Task.sleep(for: .seconds(2)) |
| 64 | + do { |
| 65 | + try await etcdClient.set("foo", value: "updated_value") |
| 66 | + } catch { |
| 67 | + print("Error setting updated value: \(error)") |
| 68 | + } |
| 69 | + } |
| 70 | + } catch { |
| 71 | + print("Error: \(error)") |
| 72 | + } |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +``` |
12 | 77 |
|
13 |
| -TBD |
| 78 | +## Contributing |
| 79 | +To contribute to this package, please look at [CONTRIBUTING.md](./CONTRIBUTING.md) |
0 commit comments