Skip to content

Commit

Permalink
Merge pull request #144 from ctreminiom/feature/agile-refactor-v2
Browse files Browse the repository at this point in the history
♻️ Refactor the Agile Module
  • Loading branch information
ctreminiom authored Sep 17, 2022
2 parents 41ff408 + 18e72ad commit 6040361
Show file tree
Hide file tree
Showing 14 changed files with 875 additions and 601 deletions.
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

0 comments on commit 6040361

Please sign in to comment.