Skip to content

Commit

Permalink
update quick-start.md (#1527)
Browse files Browse the repository at this point in the history
  • Loading branch information
compoundradius authored Dec 7, 2022
1 parent b1a5093 commit 7b13c34
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 35 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ SWIFT:=swift
# Where products will be built; this is the SPM default.
SWIFT_BUILD_PATH:=./.build
SWIFT_BUILD_CONFIGURATION=debug
SWIFT_FLAGS=--build-path=${SWIFT_BUILD_PATH} --configuration=${SWIFT_BUILD_CONFIGURATION} --enable-test-discovery
SWIFT_FLAGS=--scratch-path=${SWIFT_BUILD_PATH} --configuration=${SWIFT_BUILD_CONFIGURATION}
# Force release configuration (for plugins)
SWIFT_FLAGS_RELEASE=$(patsubst --configuration=%,--configuration=release,$(SWIFT_FLAGS))

Expand Down
86 changes: 52 additions & 34 deletions docs/quick-start.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ and other tutorials):

```sh
$ # Clone the repository at the latest release to get the example code:
$ git clone -b 1.0.0 https://github.com/grpc/grpc-swift
$ git clone -b 1.13.0 https://github.com/grpc/grpc-swift
$ # Navigate to the repository
$ cd grpc-swift/
```
Expand Down Expand Up @@ -131,64 +131,82 @@ In the same directory, open
method like this:

```swift
class GreeterProvider: Helloworld_GreeterProvider {
final class GreeterProvider: Helloworld_GreeterAsyncProvider {
let interceptors: Helloworld_GreeterServerInterceptorFactoryProtocol? = nil

func sayHello(
request: Helloworld_HelloRequest,
context: StatusOnlyCallContext
) -> EventLoopFuture<Helloworld_HelloReply> {
context: GRPCAsyncServerCallContext
) async throws -> Helloworld_HelloReply {
let recipient = request.name.isEmpty ? "stranger" : request.name
let response = Helloworld_HelloReply.with {
return Helloworld_HelloReply.with {
$0.message = "Hello \(recipient)!"
}
return context.eventLoop.makeSucceededFuture(response)
}

func sayHelloAgain(
request: Helloworld_HelloRequest,
context: StatusOnlyCallContext
) -> EventLoopFuture<Helloworld_HelloReply> {
context: GRPCAsyncServerCallContext
) async throws -> Helloworld_HelloReply {
let recipient = request.name.isEmpty ? "stranger" : request.name
let response = Helloworld_HelloReply.with {
return Helloworld_HelloReply.with {
$0.message = "Hello again \(recipient)!"
}
return context.eventLoop.makeSucceededFuture(response)
}
}
```

#### Update the client

In the same directory, open
`Sources/Examples/HelloWorld/Client/main.swift`. Call the new method like this:
`Sources/Examples/HelloWorld/Client/HelloWorldClient.swift`. Call the new method like this:

```swift
func greet(name: String?, client greeter: Helloworld_GreeterClient) {
// Form the request with the name, if one was provided.
let request = Helloworld_HelloRequest.with {
$0.name = name ?? ""
}
func run() async throws {
// Setup an `EventLoopGroup` for the connection to run on.
//
// See: https://github.com/apple/swift-nio#eventloops-and-eventloopgroups
let group = MultiThreadedEventLoopGroup(numberOfThreads: 1)

// Make sure the group is shutdown when we're done with it.
defer {
try! group.syncShutdownGracefully()
}

// Make the RPC call to the server.
let sayHello = greeter.sayHello(request)
// Configure the channel, we're not using TLS so the connection is `insecure`.
let channel = try GRPCChannelPool.with(
target: .host("localhost", port: self.port),
transportSecurity: .plaintext,
eventLoopGroup: group
)

// wait() on the response to stop the program from exiting before the response is received.
do {
let response = try sayHello.response.wait()
print("Greeter received: \(response.message)")
} catch {
print("Greeter failed: \(error)")
return
}
// Close the connection when we're done with it.
defer {
try! channel.close().wait()
}

// Provide the connection to the generated client.
let greeter = Helloworld_GreeterAsyncClient(channel: channel)

let sayHelloAgain = greeter.sayHelloAgain(request)
do {
let response = try sayHelloAgain.response.wait()
print("Greeter received: \(response.message)")
} catch {
print("Greeter failed: \(error)")
return
// Form the request with the name, if one was provided.
let request = Helloworld_HelloRequest.with {
$0.name = self.name ?? ""
}

do {
let greeting = try await greeter.sayHello(request)
print("Greeter received: \(greeting.message)")
} catch {
print("Greeter failed: \(error)")
}

do {
let greetingAgain = try await greeter.sayHelloAgain(request)
print("Greeter received: \(greetingAgain.message)")
} catch {
print("Greeter failed: \(error)")
}
}
}
```

#### Run!
Expand Down

0 comments on commit 7b13c34

Please sign in to comment.