| 1 |
package grab |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"sync" |
| 6 |
) |
| 7 |
|
| 8 |
func ExampleClient_Do() { |
| 9 |
client := NewClient() |
| 10 |
req, err := NewRequest("/tmp", "http://example.com/example.zip") |
| 11 |
if err != nil { |
| 12 |
panic(err) |
| 13 |
} |
| 14 |
|
| 15 |
resp := client.Do(req) |
| 16 |
if err := resp.Err(); err != nil { |
| 17 |
panic(err) |
| 18 |
} |
| 19 |
|
| 20 |
fmt.Println("Download saved to", resp.Filename) |
| 21 |
} |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
func ExampleClient_DoChannel() { |
| 28 |
|
| 29 |
reqch := make(chan *Request) |
| 30 |
respch := make(chan *Response, 10) |
| 31 |
|
| 32 |
|
| 33 |
client := NewClient() |
| 34 |
wg := sync.WaitGroup{} |
| 35 |
for i := 0; i < 4; i++ { |
| 36 |
wg.Add(1) |
| 37 |
go func() { |
| 38 |
client.DoChannel(reqch, respch) |
| 39 |
wg.Done() |
| 40 |
}() |
| 41 |
} |
| 42 |
|
| 43 |
go func() { |
| 44 |
|
| 45 |
for i := 0; i < 10; i++ { |
| 46 |
url := fmt.Sprintf("http://example.com/example%d.zip", i+1) |
| 47 |
req, err := NewRequest("/tmp", url) |
| 48 |
if err != nil { |
| 49 |
panic(err) |
| 50 |
} |
| 51 |
reqch <- req |
| 52 |
} |
| 53 |
close(reqch) |
| 54 |
|
| 55 |
|
| 56 |
wg.Wait() |
| 57 |
close(respch) |
| 58 |
}() |
| 59 |
|
| 60 |
|
| 61 |
for resp := range respch { |
| 62 |
|
| 63 |
if err := resp.Err(); err != nil { |
| 64 |
panic(err) |
| 65 |
} |
| 66 |
|
| 67 |
fmt.Printf("Downloaded %s to %s\n", resp.Request.URL(), resp.Filename) |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
func ExampleClient_DoBatch() { |
| 72 |
|
| 73 |
reqs := make([]*Request, 0) |
| 74 |
for i := 0; i < 10; i++ { |
| 75 |
url := fmt.Sprintf("http://example.com/example%d.zip", i+1) |
| 76 |
req, err := NewRequest("/tmp", url) |
| 77 |
if err != nil { |
| 78 |
panic(err) |
| 79 |
} |
| 80 |
reqs = append(reqs, req) |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
client := NewClient() |
| 85 |
respch := client.DoBatch(4, reqs...) |
| 86 |
|
| 87 |
|
| 88 |
for resp := range respch { |
| 89 |
if err := resp.Err(); err != nil { |
| 90 |
panic(err) |
| 91 |
} |
| 92 |
|
| 93 |
fmt.Printf("Downloaded %s to %s\n", resp.Request.URL(), resp.Filename) |
| 94 |
} |
| 95 |
} |