-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
60 lines (50 loc) · 1.33 KB
/
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
package main
import (
"bytes"
"encoding/json"
"errors"
"io"
"net/http"
)
type JiraInterface interface {
CreateIssue(body *CreateIssueRequest) (*CreateIssueResponse, error)
}
type Jira struct {
baseURL string
token string
email string
}
func NewJira(baseURL, token, email string) JiraInterface {
return &Jira{baseURL: baseURL, token: token, email: email}
}
func (j *Jira) CreateIssue(body *CreateIssueRequest) (*CreateIssueResponse, error) {
endpoint := j.baseURL + "issue"
bodyByte, err := json.Marshal(body)
if err != nil {
return nil, err
}
client := &http.Client{}
request, _ := http.NewRequest(http.MethodPost, endpoint, bytes.NewBuffer(bodyByte))
request.Header = getHeader()
request.SetBasicAuth(j.email, j.token)
response, _ := client.Do(request)
if response == nil {
return nil, errors.New("nil response")
}
if response.StatusCode == http.StatusCreated {
createIssueResponse := &CreateIssueResponse{}
bodyBytes, _ := io.ReadAll(response.Body)
err = json.Unmarshal(bodyBytes, &createIssueResponse)
if err != nil {
return nil, errors.New("response unmarshal error")
}
return createIssueResponse, nil
}
return nil, errors.New("status code different from 201")
}
func getHeader() http.Header {
return http.Header{
"Content-Type": {"application/json"},
"Accept": {"application/json"},
}
}