File: resolver.go

package info (click to toggle)
golang-github-manyminds-api2go 1.0-RC2%2Bgit20161229.31.dc368bb-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 484 kB
  • ctags: 488
  • sloc: sh: 23; makefile: 3
file content (42 lines) | stat: -rw-r--r-- 1,033 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package api2go

import "net/http"

type callbackResolver struct {
	callback func(r http.Request) string
	r        http.Request
}

// NewCallbackResolver handles each resolve via
// your provided callback func
func NewCallbackResolver(callback func(http.Request) string) URLResolver {
	return &callbackResolver{callback: callback}
}

// GetBaseURL calls the callback given in the constructor method
// to implement `URLResolver`
func (c callbackResolver) GetBaseURL() string {
	return c.callback(c.r)
}

// SetRequest to implement `RequestAwareURLResolver`
func (c *callbackResolver) SetRequest(r http.Request) {
	c.r = r
}

// staticResolver is only used
// for backwards compatible reasons
// and might be removed in the future
type staticResolver struct {
	baseURL string
}

func (s staticResolver) GetBaseURL() string {
	return s.baseURL
}

// NewStaticResolver returns a simple resolver that
// will always answer with the same url
func NewStaticResolver(baseURL string) URLResolver {
	return &staticResolver{baseURL: baseURL}
}