Skip to content

Commit

Permalink
book: add Hello.swift snippet
Browse files Browse the repository at this point in the history
Signed-off-by: Yuki Kishimoto <[email protected]>
  • Loading branch information
yukibtc committed Nov 27, 2024
1 parent fdd0d6a commit acecd89
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 11 deletions.
4 changes: 2 additions & 2 deletions book/snippets/js/src/hello.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ export async function hello() {

// ANCHOR: output
console.log("Event ID:", res.id.toBech32());
console.log("Successfully sent to:", res.output.success);
console.log("Failed to sent to:", res.output.failed);
console.log("Sent to:", res.output.success);
console.log("Not sent to:", res.output.failed);
// ANCHOR_END: output
}
// ANCHOR_END: full
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@ suspend fun hello() {

// ANCHOR: publish
val builder = EventBuilder.textNote("Hello, rust-nostr!")
client.sendEventBuilder(builder)
val res = client.sendEventBuilder(builder)
// ANCHOR_END: publish

// ANCHOR: output
// TODO
println("Event ID: ${res.id.toBech32()}")
println("Sent to: ${res.output.success}")
println("Not sent to: ${res.output.failed}")
// ANCHOR_END: output
}
// ANCHOR_END: full
4 changes: 2 additions & 2 deletions book/snippets/python/src/hello.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ async def hello():

# ANCHOR: output
print(f"Event ID: {res.id.to_bech32()}")
print(f"Successfully sent to: {res.output.success}")
print(f"Failed to send to: {res.output.failed}")
print(f"Sent to: {res.output.success}")
print(f"Not send to: {res.output.failed}")
# ANCHOR_END: output

# ANCHOR_END: full
28 changes: 28 additions & 0 deletions book/snippets/swift/NostrSnippets/Sources/Hello.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// ANCHOR: full
import Foundation
import NostrSDK

func hello() async throws {
// ANCHOR: client
let keys = Keys.generate()
let signer = NostrSigner.keys(keys: keys)
let client = Client(signer: signer)
// ANCHOR_END: client

// ANCHOR: connect
try await client.addRelay(url: "wss://relay.damus.io")
await client.connect()
// ANCHOR_END: connect

// ANCHOR: publish
let builder = EventBuilder.textNote(content: "Hello, rust-nostr!")
let output = try await client.sendEventBuilder(builder: builder)
// ANCHOR_END: publish

// ANCHOR: output
print("Event ID: \(try output.id.toBech32())")
print("Sent to: \(output.success)")
print("Not sent to: \(output.failed)")
// ANCHOR_END: output
}
// ANCHOR_END: full
20 changes: 15 additions & 5 deletions book/src/sdk/hello.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ The first step is to generate random keys needed for the client and construct th
<div slot="title">Swift</div>
<section>

TODO
```swift,ignore
{{#include ../../snippets/swift/NostrSnippets/Sources/Hello.swift:client}}
```

</section>

Expand Down Expand Up @@ -102,7 +104,9 @@ Next, add some relays to your client and connect to them.
<div slot="title">Swift</div>
<section>

TODO
```swift,ignore
{{#include ../../snippets/swift/NostrSnippets/Sources/Hello.swift:connect}}
```

</section>

Expand Down Expand Up @@ -160,7 +164,9 @@ build a text note with the [EventBuilder](event/builder.md) and publish it to re
<div slot="title">Swift</div>
<section>

TODO
```swift,ignore
{{#include ../../snippets/swift/NostrSnippets/Sources/Hello.swift:publish}}
```

</section>

Expand Down Expand Up @@ -216,7 +222,9 @@ Published the event, you can inspect the output to ensure everything worked corr
<div slot="title">Swift</div>
<section>

TODO
```swift,ignore
{{#include ../../snippets/swift/NostrSnippets/Sources/Hello.swift:output}}
```

</section>

Expand Down Expand Up @@ -272,7 +280,9 @@ Here’s the full example that includes all the steps from generating keys to in
<div slot="title">Swift</div>
<section>

TODO
```swift,ignore
{{#include ../../snippets/swift/NostrSnippets/Sources/Hello.swift:full}}
```

</section>

Expand Down

0 comments on commit acecd89

Please sign in to comment.