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

fix: confluence anonymous access is failing after few requests #146

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
21 changes: 18 additions & 3 deletions lib/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"fmt"
"io"
"net/http"

"github.com/rs/zerolog/log"
)

type ICredentials interface {
Expand All @@ -20,14 +22,19 @@ type IAuthorizationHeader interface {
GetAuthorizationHeader() string
}

func HttpRequest(method string, url string, autherization IAuthorizationHeader) ([]byte, *http.Response, error) {
type RetrySettings struct {
MaxRetries int
ErrorCodes []int
}

func HttpRequest(method string, url string, authorization IAuthorizationHeader, retry RetrySettings) ([]byte, *http.Response, error) {
request, err := http.NewRequest(method, url, nil)
if err != nil {
return nil, nil, fmt.Errorf("unexpected error creating an http request %w", err)
}

if autherization.GetAuthorizationHeader() != "" {
request.Header.Set("Authorization", autherization.GetAuthorizationHeader())
if authorization.GetAuthorizationHeader() != "" {
request.Header.Set("Authorization", authorization.GetAuthorizationHeader())
}

client := &http.Client{}
Expand All @@ -39,6 +46,14 @@ func HttpRequest(method string, url string, autherization IAuthorizationHeader)
defer response.Body.Close()

if response.StatusCode < 200 || response.StatusCode >= 300 {
if retry.MaxRetries > 0 {
for _, code := range retry.ErrorCodes {
if response.StatusCode == code {
log.Warn().Msgf("retrying http request %v", url)
return HttpRequest(method, url, authorization, RetrySettings{MaxRetries: retry.MaxRetries - 1, ErrorCodes: retry.ErrorCodes})
}
}
}
return nil, response, fmt.Errorf("error calling http url \"%v\". status code: %v", url, response)
}

Expand Down
6 changes: 3 additions & 3 deletions plugins/confluence.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ func (p *ConfluencePlugin) getSpaces() ([]ConfluenceSpaceResult, error) {

func (p *ConfluencePlugin) getSpacesRequest(start int) (*ConfluenceSpaceResponse, error) {
url := fmt.Sprintf("%s/rest/api/space?start=%d", p.URL, start)
body, _, err := lib.HttpRequest(http.MethodGet, url, p)
body, _, err := lib.HttpRequest(http.MethodGet, url, p, lib.RetrySettings{})
if err != nil {
return nil, fmt.Errorf("unexpected error creating an http request %w", err)
}
Expand Down Expand Up @@ -206,7 +206,7 @@ func (p *ConfluencePlugin) getPages(space ConfluenceSpaceResult) (*ConfluencePag

func (p *ConfluencePlugin) getPagesRequest(space ConfluenceSpaceResult, start int) (*ConfluencePageResult, error) {
url := fmt.Sprintf("%s/rest/api/space/%s/content?start=%d", p.URL, space.Key, start)
body, _, err := lib.HttpRequest(http.MethodGet, url, p)
body, _, err := lib.HttpRequest(http.MethodGet, url, p, lib.RetrySettings{})

if err != nil {
return nil, fmt.Errorf("unexpected error creating an http request %w", err)
Expand Down Expand Up @@ -253,7 +253,7 @@ func (p *ConfluencePlugin) getItem(page ConfluencePage, space ConfluenceSpaceRes
url = fmt.Sprintf("%s/rest/api/content/%s?status=historical&version=%d&expand=body.storage,version,history.previousVersion", p.URL, page.ID, version)
}

request, _, err := lib.HttpRequest(http.MethodGet, url, p)
request, _, err := lib.HttpRequest(http.MethodGet, url, p, lib.RetrySettings{MaxRetries: 3, ErrorCodes: []int{500}})
if err != nil {
return nil, 0, fmt.Errorf("unexpected error creating an http request %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion plugins/paligo.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ func (p *PaligoClient) request(endpoint string, lim *rate.Limiter) ([]byte, erro
}

url := fmt.Sprintf("https://%s.paligoapp.com/api/v2/%s", p.Instance, endpoint)
body, response, err := lib.HttpRequest("GET", url, p.auth)
body, response, err := lib.HttpRequest("GET", url, p.auth, lib.RetrySettings{})
if err != nil {
if err := reserveRateLimit(response, lim, err); err != nil {
return nil, err
Expand Down
Loading