-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
80 lines (61 loc) · 1.63 KB
/
main.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
71
72
73
74
75
76
77
78
79
80
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"os"
)
// get env vars
var envUserId = os.Getenv("IGG_ID")
var envApiToken = os.Getenv("IGG_API_TOKEN")
// Indiegogo struct for donors names from response
type Indiegogo struct {
Response []struct {
By string `json:"by"`
} `json:"response"`
}
var record Indiegogo
func main() {
updateNames(envUserId, envApiToken)
http.HandleFunc("/", handler)
log.Fatal(http.ListenAndServe("localhost:8000", nil))
}
// handler returns JSON with indiegogo donors
func handler(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
http.Error(w, "Unsupported HTTP Method", http.StatusMethodNotAllowed)
return
}
if record.Response == nil {
http.Error(w, "Error receiving JSON.", http.StatusInternalServerError)
return
}
donors, err := json.Marshal(record.Response)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(donors)
return
}
// Urlfmt is the IndieGoGo endpoint url with string verbs to insert env vars
const Urlfmt = "https://api.indiegogo.com/1.1/campaigns/%s/contributions.json?api_token=%s&per_page=200"
func updateNames(id, token string) {
var url = fmt.Sprintf(Urlfmt, id, token)
// Send the request via a client
// Do sends an HTTP request and
// returns an HTTP response
resp, err := http.Get(url)
if err != nil {
log.Fatal("Get: ", err)
return
}
// Defer the closing of the body
defer resp.Body.Close()
// Use json.Decode for reading streams of JSON data
if err := json.NewDecoder(resp.Body).Decode(&record); err != nil {
log.Println(err)
}
}