UNCLASSIFIED - NO CUI

Skip to content
Snippets Groups Projects

Resolve "Don't pass query params into api functions"

Merged Mark Howard requested to merge 29-don-t-pass-query-params-into-api-functions into master
All threads resolved!
11 files
+ 165
247
Compare changes
  • Side-by-side
  • Inline
Files
11
@@ -19,15 +19,13 @@ type JiraAgileServerRESTAPI interface {
}
type apiImpl struct {
onRequest func()
apiUrl string
client utils.HTTPClient
apiUrl string
}
//nolint:unparam // In case we might need the header from response.
func (a *apiImpl) getItems(path string, attrs map[string]string,
dstPtr interface{}) (http.Header, error) {
a.onRequest()
dstPtr interface{}) error {
url := a.apiUrl + path
req, err := http.NewRequest(http.MethodGet, url, nil)
@@ -35,7 +33,7 @@ func (a *apiImpl) getItems(path string, attrs map[string]string,
if err != nil {
utils.Logger.Errorf("Request Creation Error : %s", err.Error())
return nil, fmt.Errorf("Request Creation Error : %w", err)
return fmt.Errorf("Request Creation Error : %w", err)
}
// add the headers
@@ -48,7 +46,24 @@ func (a *apiImpl) getItems(path string, attrs map[string]string,
}
req.URL.RawQuery = q.Encode()
return utils.FetchJSON(req, dstPtr)
//nolint:bodyclose // This gets closed by utils.ParseBody.
resp, err := a.client.Request(req)
if err != nil {
utils.Logger.Errorf("Request Error : %s", err.Error())
return fmt.Errorf("Request Error : %w", err)
}
err = a.client.ParseBody(resp, dstPtr)
if err != nil {
utils.Logger.Errorf("Error Parsing Body : %s", err.Error())
return fmt.Errorf("Request Error : %w", err)
}
return nil
}
func (a *apiImpl) GetAllBoards() []*Board {
@@ -64,7 +79,7 @@ func (a *apiImpl) GetAllBoards() []*Board {
for startAt, hasNext := 0, true; hasNext; startAt += perPage {
var pageBoards pageResults
attrs["startAt"] = strconv.Itoa(startAt)
if _, err := a.getItems("/board", attrs, &pageBoards); err != nil {
if err := a.getItems("/board", attrs, &pageBoards); err != nil {
return boards
}
boards = append(boards, pageBoards.Values...)
@@ -91,7 +106,7 @@ func (a *apiImpl) GetAllIssuesForBoard(board *Board) []*Issue {
var pageIssues pageResults
attrs["startAt"] = strconv.Itoa(startAt)
path := fmt.Sprintf("/board/%d/issue", board.ID)
if _, err := a.getItems(path, attrs, &pageIssues); err != nil {
if err := a.getItems(path, attrs, &pageIssues); err != nil {
return issues
}
issues = append(issues, pageIssues.Issues...)
@@ -101,9 +116,9 @@ func (a *apiImpl) GetAllIssuesForBoard(board *Board) []*Issue {
return issues
}
func NewJiraAgileServerRESTAPI(onRequest func()) JiraAgileServerRESTAPI {
func NewJiraAgileServerRESTAPI(client utils.HTTPClient) JiraAgileServerRESTAPI {
return &apiImpl{
apiUrl: fmt.Sprintf("%s/rest/agile/1.0", config.JIRA_URL),
onRequest: onRequest,
apiUrl: fmt.Sprintf("%s/rest/agile/1.0", config.JIRA_URL),
client: client,
}
}
Loading