This file contains hidden or 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 fetch | |
import ( | |
"encoding/json" | |
"errors" | |
"io" | |
"math" | |
"net/http" | |
"sort" | |
"time" |
This file contains hidden or 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
import ( | |
"testing" | |
"github.com/stretchr/testify/assert" | |
) | |
func Test_getEndpoint(t *testing.T) { | |
type args struct { | |
version int | |
} |
This file contains hidden or 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
var ErrInvalidAPIVersion = errors.New("invalid api version") | |
func getEndpoint(version int) (string, error) { | |
switch version { | |
case 1: | |
return "/api/v1/some-data", nil | |
case 2: | |
return "/api/v2/data", nil | |
default: | |
return "", ErrInvalidAPIVersion |
This file contains hidden or 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 main | |
import ( | |
"context" | |
"log" | |
"net/http" | |
"github.com/google/uuid" | |
"github.com/gorilla/mux" | |
) |
This file contains hidden or 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 main | |
import ( | |
"context" | |
"fmt" | |
) | |
func main() { | |
ctx := context.Background() | |
ctx = addValue(ctx) |
This file contains hidden or 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 main | |
import ( | |
"context" | |
"fmt" | |
"io/ioutil" | |
"net/http" | |
neturl "net/url" | |
"time" | |
) |
This file contains hidden or 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
func greetWithTimeout(c greetpb.GreetServiceClient) { | |
req := &greetpb.GreetRequest{ | |
Greeting: &greetpb.Greeting{ | |
FirstName: "Ricardo", | |
LastName: "Linck", | |
}, | |
} | |
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond) | |
defer cancel() | |
res, err := c.Greet(ctx, req) |
This file contains hidden or 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
func (*server) Greet(ctx context.Context, req *greetpb.GreetRequest) (*greetpb.GreetResponse, error) { | |
log.Println("Greet rpc invoked!") | |
time.Sleep(500 * time.Millisecond) | |
if ctx.Err() == context.Canceled { | |
return nil, status.Error(codes.Canceled, "Client cancelled the request") | |
} | |
first := req.Greeting.FirstName |
This file contains hidden or 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
func sum(c calculatorpb.CalculatorServiceClient) { | |
req := &calculatorpb.SumRequest{ | |
NumA: 3, | |
NumB: 10, | |
} | |
ctx := metadata.AppendToOutgoingContext(context.Background(), "user", "test") | |
res, err := c.Sum(ctx, req) | |
if err != nil { | |
log.Fatalf("Error calling Sum RPC: %v", err) | |
} |
This file contains hidden or 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
func (*server) Sum(ctx context.Context, req *calculatorpb.SumRequest) (*calculatorpb.SumResponse, error) { | |
log.Printf("Sum rpc invoked with req: %v\n", req) | |
md, _ := metadata.FromIncomingContext(ctx) | |
log.Printf("Metadata received: %v", md) | |
return &calculatorpb.SumResponse{ | |
Result: req.NumA + req.NumB, | |
}, nil | |
} |