github.com/walkingsparrow/docker@v1.4.2-0.20151218153551-b708a2249bfa/api/client/lib/image_search.go (about)

     1  package lib
     2  
     3  import (
     4  	"encoding/json"
     5  	"net/http"
     6  	"net/url"
     7  
     8  	"github.com/docker/docker/api/types"
     9  	"github.com/docker/docker/api/types/registry"
    10  )
    11  
    12  // ImageSearch makes the docker host to search by a term in a remote registry.
    13  // The list of results is not sorted in any fashion.
    14  func (cli *Client) ImageSearch(options types.ImageSearchOptions, privilegeFunc RequestPrivilegeFunc) ([]registry.SearchResult, error) {
    15  	var results []registry.SearchResult
    16  	query := url.Values{}
    17  	query.Set("term", options.Term)
    18  
    19  	resp, err := cli.tryImageSearch(query, options.RegistryAuth)
    20  	if resp.statusCode == http.StatusUnauthorized {
    21  		newAuthHeader, privilegeErr := privilegeFunc()
    22  		if privilegeErr != nil {
    23  			return results, privilegeErr
    24  		}
    25  		resp, err = cli.tryImageSearch(query, newAuthHeader)
    26  	}
    27  	if err != nil {
    28  		return results, err
    29  	}
    30  	defer ensureReaderClosed(resp)
    31  
    32  	err = json.NewDecoder(resp.body).Decode(&results)
    33  	return results, err
    34  }
    35  
    36  func (cli *Client) tryImageSearch(query url.Values, registryAuth string) (*serverResponse, error) {
    37  	headers := map[string][]string{"X-Registry-Auth": {registryAuth}}
    38  	return cli.get("/images/search", query, headers)
    39  }