MCPcopy
hub / github.com/connectrpc/connect-go / ExampleIsNotModifiedError

Function ExampleIsNotModifiedError

error_example_test.go:41–71  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

39}
40
41func ExampleIsNotModifiedError() {
42 // Assume that the server from NewNotModifiedError's example is running on
43 // localhost:8080.
44 client := pingv1connect.NewPingServiceClient(
45 http.DefaultClient,
46 "http://localhost:8080",
47 // Enable client-side support for HTTP GETs.
48 connect.WithHTTPGet(),
49 )
50 req := &pingv1.PingRequest{Number: 42}
51 ctx, callInfo := connect.NewClientContext(context.Background())
52 _, err := client.Ping(ctx, req)
53 if err != nil {
54 fmt.Println(err)
55 return
56 }
57 // If the server set an Etag, we can use it to cache the response.
58 etag := callInfo.ResponseHeader().Get("Etag")
59 if etag == "" {
60 fmt.Println("no Etag in response headers")
61 return
62 }
63 fmt.Println("cached response with Etag", etag)
64 // Now we'd like to make the same request again, but avoid re-fetching the
65 // response if possible.
66 callInfo.RequestHeader().Set("If-None-Match", etag)
67 _, err = client.Ping(context.Background(), req)
68 if connect.IsNotModifiedError(err) {
69 fmt.Println("can reuse cached response")
70 }
71}

Callers

nothing calls this directly

Calls 8

PingMethod · 0.95
NewPingServiceClientFunction · 0.92
WithHTTPGetFunction · 0.92
NewClientContextFunction · 0.92
IsNotModifiedErrorFunction · 0.92
GetMethod · 0.65
ResponseHeaderMethod · 0.65
RequestHeaderMethod · 0.65

Tested by

no test coverage detected