From 3f58d01cdf76207e80711ef9bfbd161e79c9c02b Mon Sep 17 00:00:00 2001 From: Jasper Bok Date: Fri, 14 Jul 2023 19:10:07 +0200 Subject: [PATCH] Add `GetIssue()` and `GetProject()` methods to `JiraClient` --- client.go | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/client.go b/client.go index c5784ac..a2bbed9 100644 --- a/client.go +++ b/client.go @@ -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 +}