Created
August 16, 2021 19:07
-
-
Save jasonsalas/ff7a79b5b40fdd34ff1a465d6b862513 to your computer and use it in GitHub Desktop.
gRPC server-side streaming in Go - client package
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package transaction | |
import ( | |
"context" | |
"io" | |
"log" | |
bank "github.com/jasonsalas/protobank/pkg/protobuf/bank" | |
"google.golang.org/grpc" | |
) | |
type Client struct { | |
client bank.TransactionServiceClient | |
} | |
func NewClient(conn grpc.ClientConnInterface) Client { | |
return Client{ | |
client: bank.NewTransactionServiceClient(conn), | |
} | |
} | |
func (c Client) Fetch(ctx context.Context, accountID string) error { | |
stream, err := c.client.Fetch(ctx, &bank.FetchRequest{AccountId: accountID}) | |
if err != nil { | |
return err | |
} | |
for { | |
response, err := stream.Recv() | |
if err != nil { | |
if err == io.EOF { | |
log.Println("> ALL DONE!") | |
return nil | |
} | |
return err | |
} | |
trx := response.GetTransaction() | |
log.Println("TIME:", trx.GetTime().String()) | |
log.Println("OPERATION:", trx.GetOperation().String()) | |
log.Println("AMOUNT:", trx.GetAmount()) | |
log.Println("------") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment