-
Notifications
You must be signed in to change notification settings - Fork 2
/
api.go
125 lines (106 loc) · 3.78 KB
/
api.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// Package youtrack implements REST API client that can be used with YouTrack as
// described in
// https://www.jetbrains.com/help/youtrack/incloud/Resources-for-Developers.html
package youtrack
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/http/httputil"
"net/url"
)
// Api is the YouTrack API context.
// BaseURL and Token must be set.
type Api struct {
// BaseURL is the URL to the REST API endpoint for a YouTrack Project. It should
// end is a slash. For example: https://goyt.myjetbrains.com/youtrack/api/
BaseURL *url.URL
// Token is the permanent token used to make authenticated requests.
// For more information, see:
// https://www.jetbrains.com/help/youtrack/incloud/authentication-with-permanent-token.html
Token string
// EnableTracing turns on extra logging, including HTTP request/response logging.
// NOTE that the authorization token will be logged when this is enabled.
EnableTracing bool
}
func (api *Api) trace(v ...interface{}) {
if api.EnableTracing {
log.Println(v...)
}
}
// DoRequest makes an authenticated HTTP request to the YouTrack API.
// jsonRequest and jsonResult are both optional, and depend on the request being made. A GET request,
// for example, should probably set jsonRequestBody to nil.
func (api *Api) DoRequest(ctx context.Context, resource *url.URL, method string, jsonRequest, jsonResult interface{}) error {
api.trace(method, resource)
resourceURL := api.BaseURL.ResolveReference(resource)
api.trace("Resolved URL", resourceURL)
reqBody := new(bytes.Buffer)
if jsonRequest != nil {
enc := json.NewEncoder(reqBody)
err := enc.Encode(jsonRequest)
if err != nil {
log.Printf("Failed to encode json reqBody. %s", err)
return err
}
}
// TODO: Move to NewRequestWithContext when 1.11 support no longer required.
// req, err := http.NewRequestWithContext(ctx, method, resourceURL.String(), reqBody)
req, err := http.NewRequest(method, resourceURL.String(), reqBody)
if err != nil {
log.Printf("NewRequest failed. %s", err)
return err
}
req.Header.Set("Authorization", "Bearer "+api.Token)
req.Header.Set("Accept", "application/json")
req.Header.Set("Cache-Control", "no-cache")
req.Header.Set("Content-Type", "application/json")
if api.EnableTracing {
dump, err := httputil.DumpRequestOut(req, true)
if err != nil {
log.Fatalf("Failed to dump request: %s", err)
}
api.trace(fmt.Sprintf("Request Dump\n%s", string(dump)))
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
log.Printf("%s %s request failed. %s", resource, method, err)
return err
}
defer resp.Body.Close()
if api.EnableTracing {
dump, err := httputil.DumpResponse(resp, true)
if err != nil {
log.Fatalf("Failed to dump response: %s", err)
}
api.trace(fmt.Sprintf("Response Dump:\n%s", string(dump)))
}
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
respBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Printf("Error reading body of HTTP response with status %d. %s", resp.StatusCode, err)
}
return fmt.Errorf("GET %s failed with status code %d. Body: %s", resource, resp.StatusCode, string(respBody))
}
if jsonResult != nil {
dec := json.NewDecoder(resp.Body)
err = dec.Decode(jsonResult)
if err != nil {
log.Printf("Failed to decode json result. %s", err)
return err
}
}
return nil
}
// Get makes an authenticated GET request to the YouTrack API.
func (api *Api) Get(ctx context.Context, resource *url.URL, jsonResult interface{}) error {
return api.DoRequest(ctx, resource, http.MethodGet, nil, jsonResult)
}
// Post makes an authenticated POST request to the YouTrack API.
func (api *Api) Post(ctx context.Context, resource *url.URL, jsonRequest, jsonResult interface{}) error {
return api.DoRequest(ctx, resource, http.MethodPost, jsonRequest, jsonResult)
}