Add `GetIssue()` and `GetProject()` methods to `JiraClient`

main
Jasper Bok 2023-07-14 19:10:07 +02:00
parent 75c00825a8
commit 3f58d01cdf
1 changed files with 50 additions and 0 deletions

View File

@ -1,6 +1,9 @@
package jiraclient
import (
"encoding/json"
"fmt"
"io"
"net/http"
)
@ -22,3 +25,50 @@ type JiraClient struct {
Domain string
Client *http.Client
}
func (c JiraClient) get(url string) ([]byte, error) {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return []byte{}, err
}
req.SetBasicAuth(c.Username, c.Token)
resp, err := c.Client.Do(req)
if err != nil {
return []byte{}, err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
return body, err
}
func (c JiraClient) GetIssue(keyOrId string) (Issue, error) {
var issue Issue
url := fmt.Sprintf("https://%s.atlassian.net/rest/api/2/issue/%s", c.Domain, keyOrId)
body, err := c.get(url)
if err != nil {
return issue, err
}
err = json.Unmarshal(body, &issue)
return issue, err
}
func (c JiraClient) GetProject(keyOrId string) (Project, error) {
var project Project
url := fmt.Sprintf("https://%s.atlassian.net/rest/api/2/project/%s", c.Domain, keyOrId)
body, err := c.get(url)
if err != nil {
return project, err
}
err = json.Unmarshal(body, &project)
return project, err
}