Debian Codegraph @

v3/example_request_test.go

raw symbols SCIP
1 package grab
2
3 import (
4 "context"
5 "crypto/sha256"
6 "encoding/hex"
7 "fmt"
8 "time"
9 )
10
11 func ExampleRequest_WithContext() {
12 // create context with a 100ms timeout
13 ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
14 defer cancel()
15
16 // create download request with context
17 req, err := NewRequest("", "http://example.com/example.zip")
18 if err != nil {
19 panic(err)
20 }
21 req = req.WithContext(ctx)
22
23 // send download request
24 resp := DefaultClient.Do(req)
25 if err := resp.Err(); err != nil {
26 fmt.Println("error: request cancelled")
27 }
28
29 // Output:
30 // error: request cancelled
31 }
32
33 func ExampleRequest_SetChecksum() {
34 // create download request
35 req, err := NewRequest("", "http://example.com/example.zip")
36 if err != nil {
37 panic(err)
38 }
39
40 // set request checksum
41 sum, err := hex.DecodeString("33daf4c03f86120fdfdc66bddf6bfff4661c7ca11c5da473e537f4d69b470e57")
42 if err != nil {
43 panic(err)
44 }
45 req.SetChecksum(sha256.New(), sum, true)
46
47 // download and validate file
48 resp := DefaultClient.Do(req)
49 if err := resp.Err(); err != nil {
50 panic(err)
51 }
52 }