Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

chore: basic azure (#11424)

Danny Xu0b7980c128fc
diff --git a/backend/plugin/vcs/azure/azure.go b/backend/plugin/vcs/azure/azure.go
index 0629d3ae63..873fb15189 100644
--- a/backend/plugin/vcs/azure/azure.go
+++ b/backend/plugin/vcs/azure/azure.go
@@ -173,7 +173,7 @@ func (p *Provider) getChangesByCommit(ctx context.Context, externalRepositoryID,
values := &url.Values{}
values.Set("api-version", "7.0")
url := fmt.Sprintf("%s/commits/%s/changes?%s", apiURL, url.PathEscape(commitID), values.Encode())
- code, body, err := internal.Get(ctx, url, p.authToken)
+ code, body, err := internal.Get(ctx, url, p.getAuthorization())
if err != nil {
return nil, errors.Wrapf(err, "GET %s", url)
}
@@ -231,7 +231,7 @@ func (p *Provider) FetchAllRepositoryList(ctx context.Context) ([]*vcs.Repositor
for _, organization := range organizations {
if err := func() error {
url := fmt.Sprintf("https://dev.azure.com/%s/_apis/git/repositories?%s", url.PathEscape(organization), urlParams.Encode())
- code, body, err := internal.Get(ctx, url, p.authToken)
+ code, body, err := internal.Get(ctx, url, p.getAuthorization())
if err != nil {
return errors.Wrapf(err, "GET %s", url)
}
@@ -286,7 +286,7 @@ func (p *Provider) getAuthenticatedProfilePublicAlias(ctx context.Context) (stri
values.Set("api-version", "7.0")
url := fmt.Sprintf("https://app.vssps.visualstudio.com/_apis/profile/profiles/me?%s", values.Encode())
- code, body, err := internal.Get(ctx, url, p.authToken)
+ code, body, err := internal.Get(ctx, url, p.getAuthorization())
if err != nil {
return "", errors.Wrapf(err, "GET %s", url)
}
@@ -315,7 +315,7 @@ func (p *Provider) listOrganizationsForMember(ctx context.Context, memberID stri
urlParams.Set("api-version", "7.0")
url := fmt.Sprintf("https://app.vssps.visualstudio.com/_apis/accounts?%s", urlParams.Encode())
- code, body, err := internal.Get(ctx, url, p.authToken)
+ code, body, err := internal.Get(ctx, url, p.getAuthorization())
if err != nil {
return nil, errors.Wrapf(err, "GET %s", url)
}
@@ -374,7 +374,7 @@ func (p *Provider) ReadFileContent(ctx context.Context, repositoryID, filePath s
values.Set("versionDescriptor.version", refInfo.RefName)
url := fmt.Sprintf("%s/items?%s", apiURL, values.Encode())
- code, body, err := internal.Get(ctx, url, p.authToken)
+ code, body, err := internal.Get(ctx, url, p.getAuthorization())
if err != nil {
return "", errors.Wrapf(err, "GET %s", url)
}
@@ -406,7 +406,7 @@ func (p *Provider) GetBranch(ctx context.Context, repositoryID, branchName strin
urlParams.Set("api-version", "7.0")
url := fmt.Sprintf("%s/stats/branches?%s", apiURL, urlParams.Encode())
- code, body, err := internal.Get(ctx, url, p.authToken)
+ code, body, err := internal.Get(ctx, url, p.getAuthorization())
if err != nil {
return nil, errors.Wrapf(err, "GET %s", url)
}
@@ -452,7 +452,7 @@ func (p *Provider) ListPullRequestFile(ctx context.Context, repositoryID, pullRe
urlParams.Set("api-version", "7.0")
url := fmt.Sprintf("%s/pullrequests/%s?%s", apiURL, pullRequestID, urlParams.Encode())
- code, resp, err := internal.Get(ctx, url, p.authToken)
+ code, resp, err := internal.Get(ctx, url, p.getAuthorization())
if err != nil {
return nil, errors.Wrapf(err, "GET %s", url)
}
@@ -497,7 +497,7 @@ func (p *Provider) CreateWebhook(ctx context.Context, externalRepositoryID strin
urlParams := &url.Values{}
urlParams.Set("api-version", "7.0")
url := fmt.Sprintf("https://dev.azure.com/%s/_apis/hooks/subscriptions?%s", url.PathEscape(organizationName), urlParams.Encode())
- code, body, err := internal.Post(ctx, url, p.authToken, payload)
+ code, body, err := internal.Post(ctx, url, p.getAuthorization(), payload)
if err != nil {
return "", errors.Wrapf(err, "failed to create webhook")
}
@@ -530,7 +530,7 @@ func (p *Provider) DeleteWebhook(ctx context.Context, _, webhookID string) error
values.Set("api-version", "7.0")
url := fmt.Sprintf("https://dev.azure.com/%s/_apis/hooks/subscriptions/%s?%s", url.PathEscape(organizationName), url.PathEscape(webhookID), values.Encode())
- code, body, err := internal.Delete(ctx, url, p.authToken)
+ code, body, err := internal.Delete(ctx, url, p.getAuthorization())
if err != nil {
return errors.Wrapf(err, "failed to send delete webhook request")
}
@@ -551,3 +551,7 @@ func getRepositoryAPIURL(repositoryID string) (string, error) {
return fmt.Sprintf("https://dev.azure.com/%s/%s/_apis/git/repositories/%s", url.PathEscape(organizationName), url.PathEscape(projectName), url.PathEscape(repositoryID)), nil
}
+
+func (p *Provider) getAuthorization() string {
+ return fmt.Sprintf("Basic %s", p.authToken)
+}
diff --git a/backend/plugin/vcs/bitbucket/bitbucket.go b/backend/plugin/vcs/bitbucket/bitbucket.go
index 698e06f80a..b77c9c639f 100644
--- a/backend/plugin/vcs/bitbucket/bitbucket.go
+++ b/backend/plugin/vcs/bitbucket/bitbucket.go
@@ -90,7 +90,7 @@ type CommitDiffStat struct {
}
func (p *Provider) fetchPaginatedDiffFileList(ctx context.Context, url string) (diffs []*CommitDiffStat, next string, err error) {
- code, body, err := internal.Get(ctx, url, p.authToken)
+ code, body, err := internal.Get(ctx, url, p.getAuthorization())
if err != nil {
return nil, "", errors.Wrapf(err, "GET %s", url)
}
@@ -167,7 +167,7 @@ func (p *Provider) FetchAllRepositoryList(ctx context.Context) ([]*vcs.Repositor
// the paginated results along with a string indicating the URL of the next page
// (if exists).
func (p *Provider) fetchPaginatedRepositoryList(ctx context.Context, url string) (repos []*Repository, next string, err error) {
- code, body, err := internal.Get(ctx, url, p.authToken)
+ code, body, err := internal.Get(ctx, url, p.getAuthorization())
if err != nil {
return nil, "", errors.Wrapf(err, "GET %s", url)
}
@@ -208,7 +208,7 @@ func (p *Provider) fetchPaginatedRepositoryList(ctx context.Context, url string)
// Docs: https://developer.atlassian.com/cloud/bitbucket/rest/api-group-source/#raw-file-contents
func (p *Provider) ReadFileContent(ctx context.Context, repositoryID, filePath string, refInfo vcs.RefInfo) (string, error) {
url := fmt.Sprintf("%s/repositories/%s/src/%s/%s", p.APIURL(p.instanceURL), repositoryID, url.PathEscape(refInfo.RefName), url.PathEscape(filePath))
- code, body, err := internal.Get(ctx, url, p.authToken)
+ code, body, err := internal.Get(ctx, url, p.getAuthorization())
if err != nil {
return "", errors.Wrapf(err, "GET %s", url)
}
@@ -242,7 +242,7 @@ type Branch struct {
// Docs: https://developer.atlassian.com/cloud/bitbucket/rest/api-group-refs/#api-repositories-workspace-repo-slug-refs-branches-name-get
func (p *Provider) GetBranch(ctx context.Context, repositoryID, branchName string) (*vcs.BranchInfo, error) {
url := fmt.Sprintf("%s/repositories/%s/refs/branches/%s", p.APIURL(p.instanceURL), repositoryID, branchName)
- code, body, err := internal.Get(ctx, url, p.authToken)
+ code, body, err := internal.Get(ctx, url, p.getAuthorization())
if err != nil {
return nil, errors.Wrapf(err, "GET %s", url)
}
@@ -374,7 +374,7 @@ type Webhook struct {
// Docs: https://developer.atlassian.com/cloud/bitbucket/rest/api-group-repositories/#api-repositories-workspace-repo-slug-hooks-post
func (p *Provider) CreateWebhook(ctx context.Context, repositoryID string, payload []byte) (string, error) {
url := fmt.Sprintf("%s/repositories/%s/hooks", p.APIURL(p.instanceURL), repositoryID)
- code, body, err := internal.Post(ctx, url, p.authToken, payload)
+ code, body, err := internal.Post(ctx, url, p.getAuthorization(), payload)
if err != nil {
return "", errors.Wrapf(err, "POST %s", url)
}
@@ -405,7 +405,7 @@ func (p *Provider) CreateWebhook(ctx context.Context, repositoryID string, paylo
// Docs: https://developer.atlassian.com/cloud/bitbucket/rest/api-group-repositories/#api-repositories-workspace-repo-slug-hooks-uid-delete
func (p *Provider) DeleteWebhook(ctx context.Context, repositoryID, webhookID string) error {
url := fmt.Sprintf("%s/repositories/%s/hooks/%s", p.APIURL(p.instanceURL), repositoryID, webhookID)
- code, body, err := internal.Delete(ctx, url, p.authToken)
+ code, body, err := internal.Delete(ctx, url, p.getAuthorization())
if err != nil {
return errors.Wrapf(err, "DELETE %s", url)
}
@@ -421,3 +421,7 @@ func (p *Provider) DeleteWebhook(ctx context.Context, repositoryID, webhookID st
}
return nil
}
+
+func (p *Provider) getAuthorization() string {
+ return fmt.Sprintf("Bearer %s", p.authToken)
+}
diff --git a/backend/plugin/vcs/github/github.go b/backend/plugin/vcs/github/github.go
index 8ddfa50544..44760596f1 100644
--- a/backend/plugin/vcs/github/github.go
+++ b/backend/plugin/vcs/github/github.go
@@ -188,7 +188,7 @@ func (p *Provider) FetchAllRepositoryList(ctx context.Context) ([]*vcs.Repositor
// with a boolean indicating whether the next page exists.
func (p *Provider) fetchPaginatedRepositoryList(ctx context.Context, page int) (repos []Repository, hasNextPage bool, err error) {
url := fmt.Sprintf("%s/user/repos?page=%d&per_page=%d", p.APIURL(p.instanceURL), page, apiPageSize)
- code, body, err := internal.Get(ctx, url, p.authToken)
+ code, body, err := internal.Get(ctx, url, p.getAuthorization())
if err != nil {
return nil, false, errors.Wrapf(err, "GET %s", url)
}
@@ -220,7 +220,7 @@ func (p *Provider) fetchPaginatedRepositoryList(ctx context.Context, page int) (
// Docs: https://docs.github.com/en/rest/repos/contents#get-repository-content
func (p *Provider) ReadFileContent(ctx context.Context, repositoryID, filePath string, refInfo vcs.RefInfo) (string, error) {
url := fmt.Sprintf("%s/repos/%s/contents/%s?ref=%s", p.APIURL(p.instanceURL), repositoryID, url.QueryEscape(filePath), refInfo.RefName)
- code, body, err := internal.GetWithHeader(ctx, url, p.authToken,
+ code, body, err := internal.GetWithHeader(ctx, url, p.getAuthorization(),
map[string]string{
"Accept": "application/vnd.github.raw",
},
@@ -312,7 +312,7 @@ func (p *Provider) ListPullRequestFile(ctx context.Context, repositoryID, pullRe
// listPaginatedPullRequestFile lists the changed files in the pull request with pagination.
func (p *Provider) listPaginatedPullRequestFile(ctx context.Context, repositoryID, pullRequestID string, page int) ([]PullRequestFile, error) {
requestURL := fmt.Sprintf("%s/repos/%s/pulls/%s/files?per_page=%d&page=%d", p.APIURL(p.instanceURL), repositoryID, pullRequestID, apiPageSize, page)
- code, body, err := internal.Get(ctx, requestURL, p.authToken)
+ code, body, err := internal.Get(ctx, requestURL, p.getAuthorization())
if err != nil {
return nil, errors.Wrapf(err, "GET %s", requestURL)
}
@@ -355,7 +355,7 @@ type ReferenceObject struct {
// Docs: https://docs.github.com/en/rest/git/refs#get-a-reference
func (p *Provider) GetBranch(ctx context.Context, repositoryID, branchName string) (*vcs.BranchInfo, error) {
url := fmt.Sprintf("%s/repos/%s/git/ref/heads/%s", p.APIURL(p.instanceURL), repositoryID, branchName)
- code, body, err := internal.Get(ctx, url, p.authToken)
+ code, body, err := internal.Get(ctx, url, p.getAuthorization())
if err != nil {
return nil, errors.Wrapf(err, "GET %s", url)
}
@@ -396,7 +396,7 @@ type PullRequest struct {
// Docs: https://docs.github.com/en/rest/webhooks/repos#create-a-repository-webhook
func (p *Provider) CreateWebhook(ctx context.Context, repositoryID string, payload []byte) (string, error) {
url := fmt.Sprintf("%s/repos/%s/hooks", p.APIURL(p.instanceURL), repositoryID)
- code, body, err := internal.Post(ctx, url, p.authToken, payload)
+ code, body, err := internal.Post(ctx, url, p.getAuthorization(), payload)
if err != nil {
return "", errors.Wrapf(err, "POST %s", url)
}
@@ -427,7 +427,7 @@ func (p *Provider) CreateWebhook(ctx context.Context, repositoryID string, paylo
// Docs: https://docs.github.com/en/rest/webhooks/repos#delete-a-repository-webhook
func (p *Provider) DeleteWebhook(ctx context.Context, repositoryID, webhookID string) error {
url := fmt.Sprintf("%s/repos/%s/hooks/%s", p.APIURL(p.instanceURL), repositoryID, webhookID)
- code, body, err := internal.Delete(ctx, url, p.authToken)
+ code, body, err := internal.Delete(ctx, url, p.getAuthorization())
if err != nil {
return errors.Wrapf(err, "DELETE %s", url)
}
@@ -443,3 +443,7 @@ func (p *Provider) DeleteWebhook(ctx context.Context, repositoryID, webhookID st
}
return nil
}
+
+func (p *Provider) getAuthorization() string {
+ return fmt.Sprintf("Bearer %s", p.authToken)
+}
diff --git a/backend/plugin/vcs/github/sql_review.go b/backend/plugin/vcs/github/sql_review.go
deleted file mode 100644
index 966c7b1c1b..0000000000
--- a/backend/plugin/vcs/github/sql_review.go
+++ /dev/null
@@ -1,23 +0,0 @@
-package github
-
-import (
- _ "embed"
- "fmt"
-
- "github.com/bytebase/bytebase/backend/plugin/vcs"
-)
-
-// sqlReviewAction is the GitHub action for SQL review in VCS workflow.
-//
-//go:embed bytebase-sql-review.yml
-var sqlReviewAction string
-
-const (
- // SQLReviewActionFilePath is the SQL review action file path.
- SQLReviewActionFilePath = ".github/workflows/bytebase-sql-review.yml"
-)
-
-// SetupSQLReviewCI will setup the SQL review CI content with SQL review endpoint.
-func SetupSQLReviewCI(endpoint string) string {
- return fmt.Sprintf(sqlReviewAction, endpoint, vcs.SQLReviewAPISecretName)
-}
diff --git a/backend/plugin/vcs/gitlab/gitlab.go b/backend/plugin/vcs/gitlab/gitlab.go
index 7441caf98d..30570b0e24 100644
--- a/backend/plugin/vcs/gitlab/gitlab.go
+++ b/backend/plugin/vcs/gitlab/gitlab.go
@@ -204,7 +204,7 @@ func (p *Provider) fetchPaginatedRepositoryList(ctx context.Context, page int) (
// We will use user's token to create webhook in the project, which requires the
// token owner to be at least the project maintainer(40).
url := fmt.Sprintf("%s/projects?membership=true&simple=true&min_access_level=40&page=%d&per_page=%d", p.APIURL(p.instanceURL), page, apiPageSize)
- code, body, err := internal.Get(ctx, url, p.authToken)
+ code, body, err := internal.Get(ctx, url, p.getAuthorization())
if err != nil {
return nil, false, errors.Wrapf(err, "GET %s", url)
}
@@ -262,7 +262,7 @@ type MergeRequestFile struct {
// Docs: https://docs.gitlab.com/ee/api/merge_requests.html#get-single-mr-changes
func (p *Provider) ListPullRequestFile(ctx context.Context, repositoryID, pullRequestID string) ([]*vcs.PullRequestFile, error) {
url := fmt.Sprintf("%s/projects/%s/merge_requests/%s/changes", p.APIURL(p.instanceURL), repositoryID, pullRequestID)
- code, body, err := internal.Get(ctx, url, p.authToken)
+ code, body, err := internal.Get(ctx, url, p.getAuthorization())
if err != nil {
return nil, errors.Wrapf(err, "GET %s", url)
}
@@ -319,7 +319,7 @@ type MergeRequestCreate struct {
// Docs: https://docs.gitlab.com/ee/api/branches.html#get-single-repository-branch
func (p *Provider) GetBranch(ctx context.Context, repositoryID, branchName string) (*vcs.BranchInfo, error) {
url := fmt.Sprintf("%s/projects/%s/repository/branches/%s", p.APIURL(p.instanceURL), repositoryID, branchName)
- code, body, err := internal.Get(ctx, url, p.authToken)
+ code, body, err := internal.Get(ctx, url, p.getAuthorization())
if err != nil {
return nil, errors.Wrapf(err, "GET %s", url)
}
@@ -355,7 +355,7 @@ type MergeRequest struct {
// Docs: https://docs.gitlab.com/ee/api/projects.html#add-project-hook
func (p *Provider) CreateWebhook(ctx context.Context, repositoryID string, payload []byte) (string, error) {
url := fmt.Sprintf("%s/projects/%s/hooks", p.APIURL(p.instanceURL), repositoryID)
- code, body, err := internal.Post(ctx, url, p.authToken, payload)
+ code, body, err := internal.Post(ctx, url, p.getAuthorization(), payload)
if err != nil {
return "", errors.Wrapf(err, "POST %s", url)
}
@@ -391,7 +391,7 @@ func (p *Provider) CreateWebhook(ctx context.Context, repositoryID string, paylo
// Docs: https://docs.gitlab.com/ee/api/projects.html#delete-project-hook
func (p *Provider) DeleteWebhook(ctx context.Context, repositoryID, webhookID string) error {
url := fmt.Sprintf("%s/projects/%s/hooks/%s", p.APIURL(p.instanceURL), repositoryID, webhookID)
- code, body, err := internal.Delete(ctx, url, p.authToken)
+ code, body, err := internal.Delete(ctx, url, p.getAuthorization())
if err != nil {
return errors.Wrapf(err, "DELETE %s", url)
}
@@ -417,7 +417,7 @@ func (p *Provider) readFile(ctx context.Context, repositoryID, filePath string,
// In such cases, the HTTP header "Content-Encoding" will, for example, be changed to "gzip" and makes the value of "Content-Length" untrustworthy.
// We can avoid dealing with this type of problem by using the raw API instead of the typical JSON API.
url := fmt.Sprintf("%s/projects/%s/repository/files/%s/raw?ref=%s", p.APIURL(p.instanceURL), repositoryID, url.QueryEscape(filePath), url.QueryEscape(refInfo.RefName))
- code, body, err := internal.Get(ctx, url, p.authToken)
+ code, body, err := internal.Get(ctx, url, p.getAuthorization())
if err != nil {
return nil, errors.Wrapf(err, "GET %s", url)
}
@@ -437,3 +437,7 @@ func (p *Provider) readFile(ctx context.Context, repositoryID, filePath string,
Content: body,
}, nil
}
+
+func (p *Provider) getAuthorization() string {
+ return fmt.Sprintf("Bearer %s", p.authToken)
+}
diff --git a/backend/plugin/vcs/gitlab/sql_review.go b/backend/plugin/vcs/gitlab/sql_review.go
deleted file mode 100644
index 9a70b98265..0000000000
--- a/backend/plugin/vcs/gitlab/sql_review.go
+++ /dev/null
@@ -1,69 +0,0 @@
-package gitlab
-
-import (
- _ "embed"
- "fmt"
-
- "gopkg.in/yaml.v3"
-
- "github.com/bytebase/bytebase/backend/plugin/vcs"
-)
-
-const (
- // CIFilePath is the local path for GitLab ci file.
- CIFilePath = ".gitlab-ci.yml"
- // SQLReviewCIFilePath is the local path for SQL review CI in GitLab repo.
- SQLReviewCIFilePath = ".gitlab/bytebase-sql-review.yml"
- // sqlReviewCIFileRelativePathInGitLabCI is the keyword for relative path for SQL review CI file in .gitlab-ci.yml.
- sqlReviewCIFileRelativePathKeywordInGitLabCI = "local"
- // gitlabCIIncludeKeyword is the keyword for "include" in .gitlab-ci.yml
- // Docs for GitLab include syntax: https://docs.gitlab.com/ee/ci/yaml/includes.html
- gitlabCIIncludeKeyword = "include"
-)
-
-// sqlReviewCI is the GitLab CI for SQL review in VCS workflow.
-//
-//go:embed bytebase-sql-review.yml
-var sqlReviewCI string
-
-// SetupSQLReviewCI will setup the SQL review CI content with SQL review endpoint.
-func SetupSQLReviewCI(endpoint string) string {
- return fmt.Sprintf(sqlReviewCI, endpoint, vcs.SQLReviewAPISecretName)
-}
-
-// SetupGitLabCI will update the GitLab CI content to add or update the SQL review CI.
-func SetupGitLabCI(gitlabCI map[string]any) (string, error) {
- // Add include for SQL review CI
- var includeList []any
- // Docs for GitLab include syntax: https://docs.gitlab.com/ee/ci/yaml/includes.html
- switch include := gitlabCI[gitlabCIIncludeKeyword].(type) {
- case []any:
- includeList = append(includeList, include...)
- case string, any:
- includeList = append(includeList, include)
- }
-
- if !existSQLReviewCI(includeList) {
- includeList = append(includeList, map[string]string{sqlReviewCIFileRelativePathKeywordInGitLabCI: SQLReviewCIFilePath})
- }
-
- gitlabCI[gitlabCIIncludeKeyword] = includeList
- newContent, err := yaml.Marshal(gitlabCI)
- if err != nil {
- return "", err
- }
-
- return string(newContent), nil
-}
-
-func existSQLReviewCI(include []any) bool {
- for _, data := range include {
- if val, ok := data.(map[string]any); ok {
- if val[sqlReviewCIFileRelativePathKeywordInGitLabCI] == SQLReviewCIFilePath {
- return true
- }
- }
- }
-
- return false
-}
diff --git a/backend/plugin/vcs/gitlab/sql_review_test.go b/backend/plugin/vcs/gitlab/sql_review_test.go
deleted file mode 100644
index d929e1a213..0000000000
--- a/backend/plugin/vcs/gitlab/sql_review_test.go
+++ /dev/null
@@ -1,45 +0,0 @@
-package gitlab
-
-import (
- "testing"
-
- "github.com/stretchr/testify/assert"
- "github.com/stretchr/testify/require"
- "gopkg.in/yaml.v3"
-)
-
-const mockGitLabCIContentYAMLStr = `
-image: busybox:latest
-
-include:
- - local: .gitlab/example.yml
-
-before_script:
- - echo "Before script section"
-
-after_script:
- - echo "After script section"
-`
-
-func Test_SetupGitLabCI(t *testing.T) {
- content := make(map[string]any)
- err := yaml.Unmarshal([]byte(mockGitLabCIContentYAMLStr), &content)
- require.NoError(t, err)
-
- newContent, err := SetupGitLabCI(content)
- require.NoError(t, err)
-
- err = yaml.Unmarshal([]byte(newContent), &content)
- require.NoError(t, err)
-
- assert.NotNil(t, content["image"])
- assert.NotNil(t, content["include"])
- assert.NotNil(t, content["before_script"])
- assert.NotNil(t, content["after_script"])
-
- include, ok := content["include"].([]any)
- assert.Equal(t, ok, true)
-
- sqlReviewCI := existSQLReviewCI(include)
- assert.Equal(t, sqlReviewCI, true)
-}
diff --git a/backend/plugin/vcs/internal/request.go b/backend/plugin/vcs/internal/request.go
index 2dce05b781..dc8abead46 100644
--- a/backend/plugin/vcs/internal/request.go
+++ b/backend/plugin/vcs/internal/request.go
@@ -4,40 +4,39 @@ package internal
import (
"bytes"
"context"
- "fmt"
"io"
"net/http"
"github.com/pkg/errors"
)
-// Post makes a HTTP POST request to the given URL using the token.
-func Post(ctx context.Context, url string, token string, body []byte) (code int, respBody string, err error) {
- return request(ctx, http.MethodPost, url, token, nil, bytes.NewReader(body))
+// Post makes a HTTP POST request to the given URL.
+func Post(ctx context.Context, url string, authorization string, body []byte) (code int, respBody string, err error) {
+ return request(ctx, http.MethodPost, url, authorization, nil, bytes.NewReader(body))
}
-// Get makes a HTTP GET request to the given URL using the token.
-func Get(ctx context.Context, url string, token string) (code int, respBody string, err error) {
- return request(ctx, http.MethodGet, url, token, nil, bytes.NewReader(nil))
+// Get makes a HTTP GET request to the given URL.
+func Get(ctx context.Context, url string, authorization string) (code int, respBody string, err error) {
+ return request(ctx, http.MethodGet, url, authorization, nil, bytes.NewReader(nil))
}
-// GetWithHeader makes a HTTP GET request to the given URL using the token and additional header.
-func GetWithHeader(ctx context.Context, url string, token string, header map[string]string) (code int, respBody string, err error) {
- return request(ctx, http.MethodGet, url, token, header, bytes.NewReader(nil))
+// GetWithHeader makes a HTTP GET request to the given URL wit additional header.
+func GetWithHeader(ctx context.Context, url string, authorization string, header map[string]string) (code int, respBody string, err error) {
+ return request(ctx, http.MethodGet, url, authorization, header, bytes.NewReader(nil))
}
-// Delete makes a HTTP DELETE request to the given URL using the token.
-func Delete(ctx context.Context, url string, token string) (code int, respBody string, err error) {
- return request(ctx, http.MethodDelete, url, token, nil, bytes.NewReader(nil))
+// Delete makes a HTTP DELETE request to the given URL.
+func Delete(ctx context.Context, url string, authorization string) (code int, respBody string, err error) {
+ return request(ctx, http.MethodDelete, url, authorization, nil, bytes.NewReader(nil))
}
-func request(ctx context.Context, method, url string, token string, header map[string]string, requestBody *bytes.Reader) (int, string, error) {
+func request(ctx context.Context, method, url string, authorization string, header map[string]string, requestBody *bytes.Reader) (int, string, error) {
req, err := http.NewRequestWithContext(ctx, method, url, requestBody)
if err != nil {
return 0, "", errors.Wrapf(err, "failed to build delete request for url %s", url)
}
req.Header.Set("Content-Type", "application/json")
- req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token))
+ req.Header.Add("Authorization", authorization)
for k, v := range header {
req.Header.Set(k, v)
}