summaryrefslogtreecommitdiff
path: root/cloudflare/http.go
blob: f258f61a23d92ada5e8f4e7615c5c82c7ac44fd3 (plain)
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// This is a simple example
package cloudflare

import 	(
	"log"
	"fmt"
	"io/ioutil"
	"net/http"
	"bytes"
)

/*
curl --request POST \
  --url https://api.cloudflare.com/client/v4/zones/zone_identifier/dns_records \
  --header 'Content-Type: application/json' \
  --header 'X-Auth-Email: ' \
  --data '{
  "content": "198.51.100.4",
  "name": "example.com",
  "proxied": false,
  "type": "A",
  "comment": "Domain verification record",
  "tags": [
    "owner:dns-team"
  ],
  "ttl": 3600
}'
*/

func httpPut(dnsRow *RRT) (string, string) {
	var url string = cloudflareURL + dnsRow.ZoneID + "/dns_records/" + dnsRow.ID
	var authKey string = dnsRow.Auth
	var email string = dnsRow.Email

	var tmp string
	tmp = makeJSON(dnsRow)
	data := []byte(tmp)

	log.Println("http PUT url =", url)
	// log.Println("http PUT data =", data)
	// spew.Dump(data)
	pretty, _ := FormatJSON(string(data))
	log.Println("http PUT data =", pretty)

	req, err := http.NewRequest(http.MethodPut, url, bytes.NewBuffer(data))

	// Set headers
	req.Header.Set("Content-Type", "application/json")
	req.Header.Set("X-Auth-Key", authKey)
	req.Header.Set("X-Auth-Email", email)

	client := &http.Client{}
	resp, err := client.Do(req)
	if err != nil {
		log.Println(err)
		return tmp, fmt.Sprintf("blah err =", err)
	}
	defer resp.Body.Close()

	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		log.Println(err)
		return tmp, fmt.Sprintf("blah err =", err)
	}
	// log.Println("http PUT body =", body)
	// spew.Dump(body)

	return tmp, string(body)
}

func curlPost(dnsRow *RRT) string {
	var authKey string = dnsRow.Auth
	var email string = dnsRow.Email

	url := dnsRow.url
	tmp := dnsRow.data

	log.Println("curl() START")
	log.Println("curl() authkey = ", authKey)
	log.Println("curl() email   = ", email)
	log.Println("curl() url     = ", url)
	data := []byte(tmp)
	req, err := http.NewRequest(http.MethodPost, url, bytes.NewBuffer(data))

	// Set headers
	req.Header.Set("Content-Type", "application/json")
	req.Header.Set("X-Auth-Key", authKey)
	req.Header.Set("X-Auth-Email", email)

	client := &http.Client{}
	resp, err := client.Do(req)
	if err != nil {
		log.Println(err)
		return ""
	}
	defer resp.Body.Close()

	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		log.Println(err)
		return ""
	}
	// log.Println("http PUT body =", body)
	// spew.Dump(body)

	log.Println("result =", string(body))
	log.Println("curl() END")
	pretty, _ := FormatJSON(string(body))
	return pretty
}