-
Notifications
You must be signed in to change notification settings - Fork 15
/
request.go
70 lines (55 loc) · 1.66 KB
/
request.go
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package api
import (
"context"
"fmt"
)
const (
// EndpointURL represents the default API Endpoint URL.
EndpointURL = "https://api.exoscale.com/"
// Prefix represents the current API prefix.
Prefix = "v2"
)
const defaultReqEndpointEnv = "api"
// ReqEndpoint represents an Exoscale API request endpoint.
type ReqEndpoint struct {
env string
zone string
}
// NewReqEndpoint returns a new Exoscale API request endpoint from an environment and zone.
func NewReqEndpoint(env, zone string) ReqEndpoint {
re := ReqEndpoint{
env: env,
zone: zone,
}
if re.env == "" {
re.env = defaultReqEndpointEnv
}
return re
}
// Env returns the Exoscale API endpoint environment.
func (r *ReqEndpoint) Env() string {
return r.env
}
// Zone returns the Exoscale API endpoint zone.
func (r *ReqEndpoint) Zone() string {
return r.zone
}
// Host returns the Exoscale API endpoint host FQDN.
func (r *ReqEndpoint) Host() string {
return fmt.Sprintf("%s-%s.exoscale.com", r.env, r.zone)
}
// WithEndpoint returns an augmented context instance containing the Exoscale endpoint to send
// the request to.
func WithEndpoint(ctx context.Context, endpoint ReqEndpoint) context.Context {
return context.WithValue(ctx, ReqEndpoint{}, endpoint)
}
// WithZone is a shorthand to WithEndpoint where only the endpoint zone has to be specified.
// If a request endpoint is already set in the specified context instance, the value currently
// set for the environment will be reused.
func WithZone(ctx context.Context, zone string) context.Context {
var env string
if v, ok := ctx.Value(ReqEndpoint{}).(ReqEndpoint); ok {
env = v.Env()
}
return WithEndpoint(ctx, NewReqEndpoint(env, zone))
}