/
rest_client.go
111 lines (99 loc) · 3.07 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
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
111
package app
import (
"net/http"
"encoding/json"
"bytes"
"fmt"
"os"
"errors"
"strings"
. "github.com/pivotal-sydney/whiteboardbot/model"
"io/ioutil"
)
type RestClient interface {
Post(request WhiteboardRequest) (itemId string, ok bool)
GetStandupItems(standupId int) (items StandupItems, ok bool)
GetStandup(standupId string) (standup Standup, ok bool)
}
type RealRestClient struct{}
func (RealRestClient) Post(request WhiteboardRequest) (itemId string, ok bool) {
json, _ := json.Marshal(request)
fmt.Printf("Posting entry to whiteboard:\n%v\n", string(json))
http.DefaultClient.CheckRedirect = noRedirect
url := os.Getenv("WB_HOST_URL")
if len(request.Id) > 0 {
url += "/items/" + request.Id
} else {
url += fmt.Sprintf("/standups/%v/items", request.Item.StandupId)
}
httpRequest, err := http.NewRequest(toHttpVerb(request.Method), url, bytes.NewReader(json))
httpRequest.Header.Add("Content-Type", "application/json")
resp, err := http.DefaultClient.Do(httpRequest)
fmt.Printf("Whiteboard Request: %v\n\n", httpRequest)
fmt.Printf("Whiteboard Response: %v, Err: %v\n, Url: %v\n\n", resp, err, url)
ok = resp != nil && resp.StatusCode == http.StatusFound
defer resp.Body.Close()
if ok {
itemId = resp.Header.Get("Item-Id")
}
if (len(itemId) == 0) {
itemId = request.Id
}
return
}
func (RealRestClient) GetStandupItems(standupId int) (items StandupItems, ok bool) {
url := fmt.Sprintf("%v/standups/%v/items", os.Getenv("WB_HOST_URL"), standupId)
httpRequest, _ := http.NewRequest("GET", url, nil)
httpRequest.Header.Add("Accept", "application/json")
resp, err := http.DefaultClient.Do(httpRequest)
defer resp.Body.Close()
ok = err == nil && resp != nil && resp.StatusCode == http.StatusOK
if ok {
jsonBlob, err := ioutil.ReadAll(resp.Body)
ok = err == nil
if ok {
err = json.Unmarshal(jsonBlob, &items)
ok = err == nil
}
}
return
}
func (RealRestClient) GetStandup(standupId string) (standup Standup, ok bool) {
url := fmt.Sprintf("%v/standups/%v", os.Getenv("WB_HOST_URL"), standupId)
httpRequest, _ := http.NewRequest("GET", url, nil)
httpRequest.Header.Add("Accept", "application/json")
resp, err := http.DefaultClient.Do(httpRequest)
defer resp.Body.Close()
ok = err == nil && resp != nil && resp.StatusCode == http.StatusOK
if ok {
jsonBlob, err := ioutil.ReadAll(resp.Body)
ok = err == nil
if ok {
err = json.Unmarshal(jsonBlob, &standup)
ok = err == nil
}
}
return
}
func PostEntryToWhiteboard(restClient RestClient, entryType EntryType) (itemId string, ok bool) {
var request = createRequest(entryType, entryType.GetEntry() != nil && len(entryType.GetEntry().Id) > 0)
itemId, ok = restClient.Post(request)
return
}
func createRequest(entryType EntryType, existingEntry bool) WhiteboardRequest {
if existingEntry {
return entryType.MakeUpdateRequest()
} else {
return entryType.MakeCreateRequest()
}
}
func noRedirect(req *http.Request, via []*http.Request) error {
return errors.New("Don't redirect!")
}
func toHttpVerb(method string) string {
if len(method) > 0 {
return strings.ToUpper(method)
} else {
return "POST"
}
}