Skip to content

Commit 0aa2aea

Browse files
ayushi2103“Ayushi
andauthored
Update README for swift-etcd-client (#12)
Update README to add getting started, overview, and contributing information. --------- Co-authored-by: “Ayushi <“[email protected]”>
1 parent 0e1546d commit 0aa2aea

File tree

1 file changed

+70
-4
lines changed

1 file changed

+70
-4
lines changed

README.md

Lines changed: 70 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,79 @@
11
# 🚧WIP🚧: Swift ETCD client
22

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.
44

55
## 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+
```
612

713
## 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.
815

9-
TBD
16+
Here's an example of how you can use the ETCD Client
1017

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+
```
1277

13-
TBD
78+
## Contributing
79+
To contribute to this package, please look at [CONTRIBUTING.md](./CONTRIBUTING.md)

0 commit comments

Comments
 (0)