| 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 |
|
| 13 |
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond) |
| 14 |
defer cancel() |
| 15 |
|
| 16 |
|
| 17 |
req, err := NewRequest("", "http://example.com/example.zip") |
| 18 |
if err != nil { |
| 19 |
panic(err) |
| 20 |
} |
| 21 |
req = req.WithContext(ctx) |
| 22 |
|
| 23 |
|
| 24 |
resp := DefaultClient.Do(req) |
| 25 |
if err := resp.Err(); err != nil { |
| 26 |
fmt.Println("error: request cancelled") |
| 27 |
} |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
} |
| 32 |
|
| 33 |
func ExampleRequest_SetChecksum() { |
| 34 |
|
| 35 |
req, err := NewRequest("", "http://example.com/example.zip") |
| 36 |
if err != nil { |
| 37 |
panic(err) |
| 38 |
} |
| 39 |
|
| 40 |
|
| 41 |
sum, err := hex.DecodeString("33daf4c03f86120fdfdc66bddf6bfff4661c7ca11c5da473e537f4d69b470e57") |
| 42 |
if err != nil { |
| 43 |
panic(err) |
| 44 |
} |
| 45 |
req.SetChecksum(sha256.New(), sum, true) |
| 46 |
|
| 47 |
|
| 48 |
resp := DefaultClient.Do(req) |
| 49 |
if err := resp.Err(); err != nil { |
| 50 |
panic(err) |
| 51 |
} |
| 52 |
} |