Exemplo n.º 1
0
// addURITemplate adds rawTemplate to routes using name as the key. Should only
// be used from init().
func addURITemplate(name, rawTemplate string) {
	uriTemplate, err := uritemplates.Parse(rawTemplate)
	if err != nil {
		panic(err)
	}
	routes[name] = uriTemplate
}
Exemplo n.º 2
0
func (r *resource) Template(template string, vars map[string]interface{}) (*url.URL, error) {
	// Build the template object
	tmpl, err := uritemplates.Parse(template)
	if err != nil {
		return nil, err
	}

	// Encode all of the values if required
	for k, v := range vars {
		if s, isString := v.(string); isString {
			vars[k] = restdata.MaybeEncodeName(s)
		}
		if ss, isStringSlice := v.([]string); isStringSlice {
			tt := make([]string, len(ss))
			for i, s := range ss {
				tt[i] = restdata.MaybeEncodeName(s)
			}
			vars[k] = tt
		}
	}

	// Expand the template to produce a string
	expanded, err := tmpl.Expand(vars)
	if err != nil {
		return nil, err
	}

	// Return the parsed URL of the result, relative to ourselves
	return r.URL.Parse(expanded)
}
Exemplo n.º 3
0
// addURITemplate adds rawTemplate to templates using name as the key.
func addURITemplate(templates map[string]*uritemplates.UriTemplate, name string, rawTemplate string) {
	uriTemplate, err := uritemplates.Parse(rawTemplate)
	if err != nil {
		panic(err)
	}
	templates[name] = uriTemplate
}
Exemplo n.º 4
0
func (t *URITemplate) UnmarshalJSON(b []byte) error {
	tpl, err := uritemplates.Parse(string(b))
	if err != nil {
		return err
	}
	*t = URITemplate((*tpl))
	return nil
}
Exemplo n.º 5
0
// Expand will expand the URL template of the link with the given params.
func (l Link) Expand(params P) (string, error) {
	template, err := uritemplates.Parse(l.Href)
	if err != nil {
		return "", err
	}

	return template.Expand(map[string]interface{}(params))
}
Exemplo n.º 6
0
func (a *Artifact) LayerURL(layer *ImageLayer) string {
	tmpl, err := uritemplates.Parse(a.LayerURLTemplate)
	if err != nil {
		return ""
	}
	values := map[string]interface{}{"id": layer.ID}
	expanded, _ := tmpl.Expand(values)
	return expanded
}
Exemplo n.º 7
0
// Find the href of a link by its relationship, expanding any URI Template
// parameters with params. Returns "" if a link doesn't exist.
func (l HyperlinkSet) HrefParams(rel string, params Params) (string, error) {
	link, found := l.links[rel]

	if found {
		template, err := uritemplates.Parse(link.Href)
		if err != nil {
			return "", err
		}

		return template.Expand(params)
	}

	return "", nil
}
Exemplo n.º 8
0
// Expand converts a uri template into a url.URL using the given M map.
func (l Hyperlink) Expand(m M) (*url.URL, error) {
	template, err := uritemplates.Parse(string(l))
	if err != nil {
		return nil, err
	}

	// clone M to map[string]interface{}
	// if we don't do this type assertion will
	// fail on jtacoma/uritemplates
	// see https://github.com/jtacoma/uritemplates/blob/master/uritemplates.go#L189
	mm := make(map[string]interface{}, len(m))
	for k, v := range m {
		mm[k] = v
	}

	expanded, err := template.Expand(mm)
	if err != nil {
		return nil, err
	}

	return url.Parse(expanded)
}
Exemplo n.º 9
0
func DownloadServiceIcons(c Config) ([]ServiceIcon, error) {
	tpl, _ := uritemplates.Parse("https://logo.clearbit.com/{domain}?size=128&format=png")
	nIcons := len(c)
	ic := make(chan ServiceIcon)
	ec := make(chan error)

	icons := make([]ServiceIcon, 0, nIcons)
	for serviceName, serviceCfg := range c {
		url, _ := tpl.Expand(map[string]interface{}{"domain": serviceName})
		icon := ServiceIcon{
			Service: serviceName,
			GUID:    serviceCfg.GUID,
			URL:     url,
		}
		go func(i ServiceIcon) {
			err := i.fetch()
			if err != nil {
				ec <- err
			} else {
				ic <- i
			}
		}(icon)
	}

	for nIcons > 0 {
		select {
		case i := <-ic:
			icons = append(icons, i)
			nIcons--
		case err := <-ec:
			return []ServiceIcon{}, err
		case <-time.After(60 * time.Second):
			return []ServiceIcon{}, fmt.Errorf("Timed out while downloading service icons")
		}
	}
	return icons, nil
}