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

Feature/confluence http improvements #228

Merged
merged 24 commits into from
Jul 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
3ba9c67
:art: Migrated the client interface from Client to Connector on the C…
ctreminiom Jul 27, 2023
971f129
:art: Migrated the client interface from Client to Connector on the C…
ctreminiom Jul 27, 2023
855be35
:art: Migrated the client interface from Client to Connector on the C…
ctreminiom Jul 27, 2023
5cc726b
:art: Migrated the client interface from Client to Connector on the C…
ctreminiom Jul 27, 2023
4d46e20
:art: Migrated the client interface from Client to Connector on the C…
ctreminiom Jul 27, 2023
ab1b484
:art: Migrated the client interface from Client to Connector on the C…
ctreminiom Jul 27, 2023
327c269
:art: Migrated the client interface from Client to Connector on the C…
ctreminiom Jul 27, 2023
5da96a9
:art: Migrated the client interface from Client to Connector on the C…
ctreminiom Jul 27, 2023
659bed7
:art: Migrated the client interface from Client to Connector on the C…
ctreminiom Jul 29, 2023
f2391f1
:art: Migrated the client interface from Client to Connector on the C…
ctreminiom Jul 29, 2023
a48f00c
:art: Migrated the client interface from Client to Connector on the C…
ctreminiom Jul 29, 2023
bf05187
:art: Migrated the client interface from Client to Connector on the C…
ctreminiom Jul 29, 2023
54c52f3
:art: Migrated the client interface from Client to Connector on the C…
ctreminiom Jul 29, 2023
cc4ba16
:art: Migrated the client interface from Client to Connector on the C…
ctreminiom Jul 29, 2023
26b0e1f
:art: Migrated the client interface from Client to Connector on the C…
ctreminiom Jul 29, 2023
de2049f
:art: Migrated the client interface from Client to Connector on the C…
ctreminiom Jul 29, 2023
fb61519
:art: Migrated the client interface from Client to Connector on the C…
ctreminiom Jul 29, 2023
a895c51
:art: Migrated the client interface from Client to Connector on the C…
ctreminiom Jul 29, 2023
685f6ed
:art: Migrated the client interface from Client to Connector on the C…
ctreminiom Jul 29, 2023
e695741
:art: Migrated the client interface from Client to Connector on the C…
ctreminiom Jul 29, 2023
b9e5db1
:art: Migrated the client interface from Client to Connector on the C…
ctreminiom Jul 29, 2023
c77acc5
:art: Migrated the client interface from Client to Connector on the C…
ctreminiom Jul 29, 2023
798f382
:bug: Fixed the Beaver token statement.
ctreminiom Jul 30, 2023
eb206e3
:bug: Fixed the Beaver token statement.
ctreminiom Jul 30, 2023
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
125 changes: 60 additions & 65 deletions confluence/api_client_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"io"
"net/http"
"net/url"
"reflect"
"strings"
)

Expand All @@ -21,18 +20,22 @@ func New(httpClient common.HttpClient, site string) (*Client, error) {
httpClient = http.DefaultClient
}

if site == "" {
return nil, models.ErrNoSiteError
}

if !strings.HasSuffix(site, "/") {
site += "/"
}

siteAsURL, err := url.Parse(site)
u, err := url.Parse(site)
if err != nil {
return nil, err
}

client := &Client{
HTTP: httpClient,
Site: siteAsURL,
Site: u,
}

contentSubServices := &internal.ContentSubServices{
Expand Down Expand Up @@ -72,68 +75,58 @@ type Client struct {
Analytics *internal.AnalyticsService
}

func (c *Client) NewFormRequest(ctx context.Context, method, apiEndpoint, contentType string, payload io.Reader) (*http.Request, error) {
func (c *Client) NewRequest(ctx context.Context, method, urlStr, type_ string, body interface{}) (*http.Request, error) {

relativePath, err := url.Parse(apiEndpoint)
rel, err := url.Parse(urlStr)
if err != nil {
return nil, err
}

var endpoint = c.Site.ResolveReference(relativePath).String()

request, err := http.NewRequestWithContext(ctx, method, endpoint, payload)
if err != nil {
return nil, err
}

request.Header.Add("Content-Type", contentType)
request.Header.Add("Accept", "application/json")
request.Header.Set("X-Atlassian-Token", "no-check")

if c.Auth.HasBasicAuth() {
request.SetBasicAuth(c.Auth.GetBasicAuth())
}
u := c.Site.ResolveReference(rel)

if c.Auth.HasUserAgent() {
request.Header.Set("User-Agent", c.Auth.GetUserAgent())
buf := new(bytes.Buffer)
if body != nil {
if err = json.NewEncoder(buf).Encode(body); err != nil {
return nil, err
}
}

if c.Auth.GetBearerToken() != "" {
request.Header.Add("Authorization", fmt.Sprintf("Bearer %v", c.Auth.GetBearerToken()))
// If the body interface is a *bytes.Buffer type
// it means the NewRequest() requires to handle the RFC 1867 ISO
if attachBuffer, ok := body.(*bytes.Buffer); ok {
buf = attachBuffer
}

return request, nil
}

func (c *Client) NewRequest(ctx context.Context, method, apiEndpoint string, payload io.Reader) (*http.Request, error) {

relativePath, err := url.Parse(apiEndpoint)
req, err := http.NewRequestWithContext(ctx, method, u.String(), buf)
if err != nil {
return nil, err
}

var endpoint = c.Site.ResolveReference(relativePath).String()
req.Header.Set("Accept", "application/json")

request, err := http.NewRequestWithContext(ctx, method, endpoint, payload)
if err != nil {
return nil, err
if body != nil {
req.Header.Set("Content-Type", "application/json")
}

request.Header.Set("Accept", "application/json")

if payload != nil {
request.Header.Set("Content-Type", "application/json")
if type_ != "" {
// When the type_ is provided, it means the request needs to be created to handle files
req.Header.Set("Content-Type", type_)
req.Header.Set("X-Atlassian-Token", "no-check")
}

if c.Auth.HasBasicAuth() {
request.SetBasicAuth(c.Auth.GetBasicAuth())
req.SetBasicAuth(c.Auth.GetBasicAuth())
}

if c.Auth.HasUserAgent() {
request.Header.Set("User-Agent", c.Auth.GetUserAgent())
req.Header.Set("User-Agent", c.Auth.GetUserAgent())
}

return request, nil
if c.Auth.GetBearerToken() != "" && !c.Auth.HasBasicAuth() {
req.Header.Add("Authorization", fmt.Sprintf("Bearer %v", c.Auth.GetBearerToken()))
}

return req, nil
}

func (c *Client) Call(request *http.Request, structure interface{}) (*models.ResponseScheme, error) {
Expand All @@ -143,12 +136,14 @@ func (c *Client) Call(request *http.Request, structure interface{}) (*models.Res
return nil, err
}

return c.TransformTheHTTPResponse(response, structure)
return c.processResponse(response, structure)
}

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

defer response.Body.Close()

responseTransformed := &models.ResponseScheme{
res := &models.ResponseScheme{
Response: response,
Code: response.StatusCode,
Endpoint: response.Request.URL.String(),
Expand All @@ -157,39 +152,39 @@ func (c *Client) TransformTheHTTPResponse(response *http.Response, structure int

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

responseTransformed.Bytes.Write(responseAsBytes)
res.Bytes.Write(responseAsBytes)

wasSuccess := response.StatusCode >= 200 && response.StatusCode < 300

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

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

return responseTransformed, nil
}
case http.StatusNotFound:
return res, models.ErrNotFound

func (c *Client) TransformStructToReader(structure interface{}) (io.Reader, error) {
case http.StatusUnauthorized:
return res, models.ErrUnauthorized

if structure == nil {
return nil, models.ErrNilPayloadError
}
case http.StatusInternalServerError:
return res, models.ErrInternalError

case http.StatusBadRequest:
return res, models.ErrBadRequestError

if reflect.ValueOf(structure).Type().Kind() == reflect.Struct {
return nil, models.ErrNonPayloadPointerError
default:
return res, models.ErrInvalidStatusCodeError
}
}

structureAsBodyBytes, err := json.Marshal(structure)
if err != nil {
return nil, err
if structure != nil {
if err = json.Unmarshal(responseAsBytes, &structure); err != nil {
return res, err
}
}

return bytes.NewReader(structureAsBodyBytes), nil
return res, nil
}
Loading