Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

♻️ Refactor the Agile Module #144

Merged
merged 6 commits into from
Sep 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 10 additions & 42 deletions jira/agile/api_client_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ import (
"bytes"
"context"
"encoding/json"
"errors"
"github.com/ctreminiom/go-atlassian/jira/agile/internal"
"github.com/ctreminiom/go-atlassian/pkg/infra/models"
"github.com/ctreminiom/go-atlassian/service/agile"
"github.com/ctreminiom/go-atlassian/service/common"
"io"
"io/ioutil"
Expand Down Expand Up @@ -64,9 +62,9 @@ type Client struct {
HTTP common.HttpClient
Site *url.URL
Auth common.Authentication
Board agile.Board
Epic agile.Epic
Sprint agile.Sprint
Board *internal.BoardService
Epic *internal.EpicService
Sprint *internal.SprintService
}

func (c *Client) NewFormRequest(ctx context.Context, method, apiEndpoint, contentType string, payload io.Reader) (*http.Request, error) {
Expand Down Expand Up @@ -107,61 +105,32 @@ func (c *Client) NewRequest(ctx context.Context, method, apiEndpoint string, pay
func (c *Client) Call(request *http.Request, structure interface{}) (*models.ResponseScheme, error) {

response, err := c.HTTP.Do(request)

if err != nil {
return nil, err
}

return c.TransformTheHTTPResponse(response, structure)
}

func (c *Client) TransformTheHTTPResponse(response *http.Response, structure interface{}) (*models.ResponseScheme, error) {

responseTransformed := &models.ResponseScheme{
Response: response,
Code: response.StatusCode,
Endpoint: response.Request.URL.String(),
Method: response.Request.Method,
}

if !(response.StatusCode >= 200 && response.StatusCode < 300) {
return responseTransformed, models.ErrInvalidStatusCodeError
}

responseAsBytes, err := ioutil.ReadAll(response.Body)
if err != nil {
return responseTransformed, err
}

if structure != nil {
if err = json.Unmarshal(responseAsBytes, &structure); err != nil {
return responseTransformed, err
}
}

_, err = responseTransformed.Bytes.Write(responseAsBytes)
if err != nil {
return nil, err
}

return responseTransformed, nil
}

func (c *Client) TransformTheHTTPResponse(response *http.Response, structure interface{}) (*models.ResponseScheme, error) {

if response == nil {
return nil, errors.New("validation failed, please provide a http.Response pointer")
}

responseTransformed := &models.ResponseScheme{}
responseTransformed.Code = response.StatusCode
responseTransformed.Endpoint = response.Request.URL.String()
responseTransformed.Method = response.Request.Method
responseTransformed.Bytes.Write(responseAsBytes)

var wasSuccess = response.StatusCode >= 200 && response.StatusCode < 300
if !wasSuccess {

return responseTransformed, errors.New("TODO")
}

responseAsBytes, err := ioutil.ReadAll(response.Body)
if err != nil {
return responseTransformed, err
return responseTransformed, models.ErrInvalidStatusCodeError
}

if structure != nil {
Expand All @@ -170,7 +139,6 @@ func (c *Client) TransformTheHTTPResponse(response *http.Response, structure int
}
}

responseTransformed.Bytes.Write(responseAsBytes)
return responseTransformed, nil
}

Expand Down
26 changes: 13 additions & 13 deletions jira/agile/api_client_impl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"bytes"
"encoding/json"
"errors"
"github.com/ctreminiom/go-atlassian/jira/agile/internal"
"github.com/ctreminiom/go-atlassian/pkg/infra/models"
"github.com/ctreminiom/go-atlassian/service/agile"
"github.com/ctreminiom/go-atlassian/service/common"
"github.com/ctreminiom/go-atlassian/service/mocks"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -41,9 +41,9 @@ func TestClient_Call(t *testing.T) {
HTTP common.HttpClient
Site *url.URL
Authentication common.Authentication
Board agile.Board
Epic agile.Epic
Sprint agile.Sprint
Board *internal.BoardService
Epic *internal.EpicService
Sprint *internal.SprintService
}

type args struct {
Expand Down Expand Up @@ -253,9 +253,9 @@ func TestClient_TransformTheHTTPResponse(t *testing.T) {
HTTP common.HttpClient
Site *url.URL
Authentication common.Authentication
Board agile.Board
Epic agile.Epic
Sprint agile.Sprint
Board *internal.BoardService
Epic *internal.EpicService
Sprint *internal.SprintService
}

type args struct {
Expand Down Expand Up @@ -321,7 +321,7 @@ func TestClient_TransformTheHTTPResponse(t *testing.T) {
func TestClient_TransformStructToReader(t *testing.T) {

expectedBytes, err := json.Marshal(&models.BoardScheme{
Name: "Board Sample",
Name: "BoardConnector Sample",
Type: "Scrum",
})

Expand All @@ -333,9 +333,9 @@ func TestClient_TransformStructToReader(t *testing.T) {
HTTP common.HttpClient
Site *url.URL
Authentication common.Authentication
Board agile.Board
Epic agile.Epic
Sprint agile.Sprint
Board *internal.BoardService
Epic *internal.EpicService
Sprint *internal.SprintService
}

type args struct {
Expand All @@ -354,7 +354,7 @@ func TestClient_TransformStructToReader(t *testing.T) {
name: "when the parameters are correct",
args: args{
structure: &models.BoardScheme{
Name: "Board Sample",
Name: "BoardConnector Sample",
Type: "Scrum",
},
},
Expand All @@ -366,7 +366,7 @@ func TestClient_TransformStructToReader(t *testing.T) {
name: "when the payload provided is not a pointer",
args: args{
structure: models.BoardScheme{
Name: "Board Sample",
Name: "BoardConnector Sample",
Type: "Scrum",
},
},
Expand Down
Loading