implement functions to augment the context
This commit is contained in:
@@ -5,7 +5,26 @@ import (
|
||||
"image"
|
||||
)
|
||||
|
||||
type key int
|
||||
|
||||
const (
|
||||
auth key = iota
|
||||
)
|
||||
|
||||
// Source defines the interface for image sources
|
||||
type Source interface {
|
||||
Get(ctx context.Context, path string) (image.Image, error)
|
||||
}
|
||||
|
||||
// WithAuthorization puts the authorization in the context.
|
||||
func WithAuthorization(parent context.Context, authorization string) context.Context {
|
||||
return context.WithValue(parent, auth, authorization)
|
||||
}
|
||||
|
||||
func authorization(ctx context.Context) string {
|
||||
val := ctx.Value(auth)
|
||||
if val == nil {
|
||||
return ""
|
||||
}
|
||||
return val.(string)
|
||||
}
|
||||
|
||||
@@ -23,11 +23,6 @@ type WebDav struct {
|
||||
baseURL string
|
||||
}
|
||||
|
||||
const (
|
||||
// WebDavAuth is the parameter name for the autorization token
|
||||
WebDavAuth = "Authorization"
|
||||
)
|
||||
|
||||
// Get downloads the file from a webdav service
|
||||
func (s WebDav) Get(ctx context.Context, file string) (image.Image, error) {
|
||||
u, _ := url.Parse(s.baseURL)
|
||||
@@ -37,8 +32,8 @@ func (s WebDav) Get(ctx context.Context, file string) (image.Image, error) {
|
||||
return nil, fmt.Errorf("could not get the image \"%s\" error: %s", file, err.Error())
|
||||
}
|
||||
|
||||
auth, ok := authorization(ctx)
|
||||
if !ok {
|
||||
auth := authorization(ctx)
|
||||
if auth == "" {
|
||||
return nil, fmt.Errorf("could not get image \"%s\" error: authorization is missing", file)
|
||||
}
|
||||
req.Header.Add("Authorization", auth)
|
||||
@@ -59,8 +54,3 @@ func (s WebDav) Get(ctx context.Context, file string) (image.Image, error) {
|
||||
}
|
||||
return img, nil
|
||||
}
|
||||
|
||||
func authorization(ctx context.Context) (string, bool) {
|
||||
auth, ok := ctx.Value(WebDavAuth).(string)
|
||||
return auth, ok
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user