Add client implementation of distribution interface

Adds functionality to create a Repository client which connects to a remote endpoint.

Signed-off-by: Derek McGowan <derek@mcgstyle.net> (github: dmcgowan)
This commit is contained in:
Derek McGowan 2015-04-17 13:32:51 -07:00
parent 24f607cdf4
commit 4c8e4dc373
6 changed files with 1793 additions and 0 deletions

View file

@ -1,9 +1,14 @@
package client
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"github.com/docker/distribution/digest"
"github.com/docker/distribution/registry/api/v2"
)
// RepositoryNotFoundError is returned when making an operation against a
@ -77,3 +82,35 @@ type UnexpectedHTTPStatusError struct {
func (e *UnexpectedHTTPStatusError) Error() string {
return fmt.Sprintf("Received unexpected HTTP status: %s", e.Status)
}
// UnexpectedHTTPResponseError is returned when an expected HTTP status code
// is returned, but the content was unexpected and failed to be parsed.
type UnexpectedHTTPResponseError struct {
ParseErr error
Response []byte
}
func (e *UnexpectedHTTPResponseError) Error() string {
shortenedResponse := string(e.Response)
if len(shortenedResponse) > 15 {
shortenedResponse = shortenedResponse[:12] + "..."
}
return fmt.Sprintf("Error parsing HTTP response: %s: %q", e.ParseErr.Error(), shortenedResponse)
}
func parseHTTPErrorResponse(response *http.Response) error {
var errors v2.Errors
body, err := ioutil.ReadAll(response.Body)
if err != nil {
return err
}
decoder := json.NewDecoder(bytes.NewReader(body))
err = decoder.Decode(&errors)
if err != nil {
return &UnexpectedHTTPResponseError{
ParseErr: err,
Response: body,
}
}
return &errors
}