-
Notifications
You must be signed in to change notification settings - Fork 7
/
rest_client.go
64 lines (54 loc) · 1.84 KB
/
rest_client.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
package ne
import (
"context"
"fmt"
"net/http"
"regexp"
"github.com/equinix/rest-go"
)
//RestClient describes REST implementation of Network Edge Client
type RestClient struct {
*rest.Client
}
//NewClient creates new REST Network Edge client with a given baseURL, context and httpClient
func NewClient(ctx context.Context, baseURL string, httpClient *http.Client) *RestClient {
rest := rest.NewClient(ctx, baseURL, httpClient)
rest.SetHeader("User-agent", "equinix/ne-go")
return &RestClient{rest}
}
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
// Unexported package methods
//_______________________________________________________________________
const (
changeTypeCreate = "Add"
changeTypeUpdate = "Update"
changeTypeDelete = "Delete"
)
type headerProvider interface {
Header() http.Header
}
func getLocationHeaderValue(provider headerProvider) (*string, error) {
locationValues, ok := provider.Header()["Location"]
if !ok {
return nil, fmt.Errorf("location header not found")
}
if len(locationValues) != 1 {
return nil, fmt.Errorf("only one location header value is expected")
}
return &locationValues[0], nil
}
func parseResourceIDFromLocationHeader(header string) (*string, error) {
re := regexp.MustCompile(".+/([^/]+)$")
res := re.FindAllStringSubmatch(header, -1)
if len(res) < 1 || len(res[0]) != 2 {
return nil, fmt.Errorf("could not parse resource identifier from location header value %q", header)
}
return &res[0][1], nil
}
func getResourceIDFromLocationHeader(provider headerProvider) (*string, error) {
locHeaderValue, err := getLocationHeaderValue(provider)
if err != nil {
return nil, err
}
return parseResourceIDFromLocationHeader(*locHeaderValue)
}