image_search.go 1.2 KB

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