github.com/isi-lincoln/grab@v2.0.1-0.20200331080741-9f014744ee41+incompatible/example_client_test.go (about)

     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  // This example uses DoChannel to create a Producer/Consumer model for
    24  // downloading multiple files concurrently. This is similar to how DoBatch uses
    25  // DoChannel under the hood except that it allows the caller to continually send
    26  // new requests until they wish to close the request channel.
    27  func ExampleClient_DoChannel() {
    28  	// create a request and a buffered response channel
    29  	reqch := make(chan *Request)
    30  	respch := make(chan *Response, 10)
    31  
    32  	// start 4 workers
    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  		// send requests
    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  		// wait for workers to finish
    56  		wg.Wait()
    57  		close(respch)
    58  	}()
    59  
    60  	// check each response
    61  	for resp := range respch {
    62  		// block until complete
    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  	// create multiple download requests
    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  	// start downloads with 4 workers
    84  	client := NewClient()
    85  	respch := client.DoBatch(4, reqs...)
    86  
    87  	// check each response
    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  }