forked from UKCloud/govcloudair
-
Notifications
You must be signed in to change notification settings - Fork 3
/
helper.go
56 lines (41 loc) · 1.18 KB
/
helper.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
package govcloudair
import (
"bytes"
"encoding/xml"
"fmt"
"log"
"net/http"
"net/url"
types "github.com/kradalby/govcloudair/types/v56"
)
func GetVersionHeader(version types.ApiVersionType) (key, value string) {
return "Accept", fmt.Sprintf("application/*+xml;version=%s", version)
}
func ExecuteRequest(payload, path, type_, contentType string, client *Client) (Task, error) {
s, _ := url.ParseRequestURI(path)
log.Printf("[TRACE] URL: %s", path)
log.Printf("[TRACE] Type: %s", type_)
log.Printf("[TRACE] ContentType: %s", contentType)
var req *http.Request
switch type_ {
case "POST":
log.Printf("[TRACE] XML: \n %s", payload)
b := bytes.NewBufferString(xml.Header + payload)
req = client.NewRequest(map[string]string{}, type_, *s, b)
default:
req = client.NewRequest(map[string]string{}, type_, *s, nil)
}
if contentType != "" {
req.Header.Add("Content-Type", contentType)
}
resp, err := checkResp(client.Http.Do(req))
if err != nil {
return Task{}, err
}
task := NewTask(client)
if err = decodeBody(resp, task.Task); err != nil {
return Task{}, fmt.Errorf("error decoding Task response: %s", err)
}
// The request was successful
return *task, nil
}