Skip to content

Commit

Permalink
fix: use client.Do to unmarshal json responses
Browse files Browse the repository at this point in the history
  • Loading branch information
iamjem committed Nov 19, 2021
1 parent 4768e69 commit 71f2d08
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 28 deletions.
17 changes: 6 additions & 11 deletions customer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ package jira

import (
"context"
"encoding/json"
"fmt"
"net/http"
)

// CustomerService handles ServiceDesk customers for the Jira instance / API.
Expand Down Expand Up @@ -43,7 +42,7 @@ type CustomerList struct {
//
// https://developer.atlassian.com/cloud/jira/service-desk/rest/api-group-customer/#api-rest-servicedeskapi-customer-post
func (c *CustomerService) CreateWithContext(ctx context.Context, email, displayName string) (*Customer, *Response, error) {
apiEndpoint := "rest/servicedeskapi/customer"
const apiEndpoint = "rest/servicedeskapi/customer"

payload := struct {
Email string `json:"email"`
Expand All @@ -53,19 +52,15 @@ func (c *CustomerService) CreateWithContext(ctx context.Context, email, displayN
DisplayName: displayName,
}

req, err := c.client.NewRequestWithContext(ctx, "POST", apiEndpoint, payload)
req, err := c.client.NewRequestWithContext(ctx, http.MethodPost, apiEndpoint, payload)
if err != nil {
return nil, nil, err
}
resp, err := c.client.Do(req, nil)
if err != nil {
return nil, resp, NewJiraError(resp, err)
}
defer resp.Body.Close()

responseCustomer := new(Customer)
if err := json.NewDecoder(resp.Body).Decode(responseCustomer); err != nil {
return nil, resp, fmt.Errorf("could not unmarshall the data into struct")
resp, err := c.client.Do(req, responseCustomer)
if err != nil {
return nil, resp, NewJiraError(resp, err)
}

return responseCustomer, resp, nil
Expand Down
21 changes: 4 additions & 17 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package jira

import (
"context"
"encoding/json"
"fmt"
)

Expand Down Expand Up @@ -84,18 +83,12 @@ func (r *RequestService) CreateWithContext(ctx context.Context, requester string
return nil, nil, err
}

resp, err := r.client.Do(req, nil)
responseRequest := new(Request)
resp, err := r.client.Do(req, responseRequest)
if err != nil {
return nil, resp, NewJiraError(resp, err)
}

defer resp.Body.Close()

responseRequest := new(Request)
if err := json.NewDecoder(resp.Body).Decode(responseRequest); err != nil {
return nil, resp, fmt.Errorf("could not unmarshall the data into struct")
}

return responseRequest, resp, nil
}

Expand All @@ -115,18 +108,12 @@ func (r *RequestService) CreateCommentWithContext(ctx context.Context, issueIDOr
return nil, nil, err
}

resp, err := r.client.Do(req, nil)
responseComment := new(RequestComment)
resp, err := r.client.Do(req, responseComment)
if err != nil {
return nil, resp, NewJiraError(resp, err)
}

defer resp.Body.Close()

responseComment := new(RequestComment)
if err := json.NewDecoder(resp.Body).Decode(responseComment); err != nil {
return nil, resp, fmt.Errorf("could not unmarshall the data into struct")
}

return responseComment, resp, nil
}

Expand Down

0 comments on commit 71f2d08

Please sign in to comment.