Commit a3429ad2 authored by Timo Furrer's avatar Timo Furrer 🏙️
Browse files

refactor: use do function for requests

parent 19926c26
Loading
Loading
Loading
Loading
+7 −16
Original line number Diff line number Diff line
@@ -14,10 +14,7 @@

package gitlab

import (
	"fmt"
	"net/http"
)
import "net/http"

type (
	DependencyProxyServiceInterface interface {
@@ -41,16 +38,10 @@ type (
var _ DependencyProxyServiceInterface = (*DependencyProxyService)(nil)

func (s *DependencyProxyService) PurgeGroupDependencyProxy(gid any, options ...RequestOptionFunc) (*Response, error) {
	group, err := parseID(gid)
	if err != nil {
		return nil, err
	}
	u := fmt.Sprintf("groups/%s/dependency_proxy/cache", PathEscape(group))

	req, err := s.client.NewRequest(http.MethodDelete, u, nil, options)
	if err != nil {
		return nil, err
	}

	return s.client.Do(req, nil)
	_, resp, err := do[none](s.client,
		withMethod(http.MethodDelete),
		withPath("groups/%s/dependency_proxy/cache", GroupID{gid}),
		withRequestOpts(options...),
	)
	return resp, err
}
+46 −145
Original line number Diff line number Diff line
@@ -17,7 +17,6 @@
package gitlab

import (
	"fmt"
	"net/http"
	"time"
)
@@ -114,18 +113,10 @@ func (k DeployToken) String() string {
}

func (s *DeployTokensService) ListAllDeployTokens(options ...RequestOptionFunc) ([]*DeployToken, *Response, error) {
	req, err := s.client.NewRequest(http.MethodGet, "deploy_tokens", nil, options)
	if err != nil {
		return nil, nil, err
	}

	var ts []*DeployToken
	resp, err := s.client.Do(req, &ts)
	if err != nil {
		return nil, resp, err
	}

	return ts, resp, nil
	return do[[]*DeployToken](s.client,
		withPath("deploy_tokens"),
		withRequestOpts(options...),
	)
}

// ListProjectDeployTokensOptions represents the available ListProjectDeployTokens()
@@ -138,45 +129,18 @@ type ListProjectDeployTokensOptions struct {
}

func (s *DeployTokensService) ListProjectDeployTokens(pid any, opt *ListProjectDeployTokensOptions, options ...RequestOptionFunc) ([]*DeployToken, *Response, error) {
	project, err := parseID(pid)
	if err != nil {
		return nil, nil, err
	}
	u := fmt.Sprintf("projects/%s/deploy_tokens", PathEscape(project))

	req, err := s.client.NewRequest(http.MethodGet, u, opt, options)
	if err != nil {
		return nil, nil, err
	}

	var ts []*DeployToken
	resp, err := s.client.Do(req, &ts)
	if err != nil {
		return nil, resp, err
	}

	return ts, resp, nil
	return do[[]*DeployToken](s.client,
		withPath("projects/%s/deploy_tokens", ProjectID{pid}),
		withAPIOpts(opt),
		withRequestOpts(options...),
	)
}

func (s *DeployTokensService) GetProjectDeployToken(pid any, deployToken int64, options ...RequestOptionFunc) (*DeployToken, *Response, error) {
	project, err := parseID(pid)
	if err != nil {
		return nil, nil, err
	}
	u := fmt.Sprintf("projects/%s/deploy_tokens/%d", PathEscape(project), deployToken)

	req, err := s.client.NewRequest(http.MethodGet, u, nil, options)
	if err != nil {
		return nil, nil, err
	}

	t := new(DeployToken)
	resp, err := s.client.Do(req, t)
	if err != nil {
		return nil, resp, err
	}

	return t, resp, nil
	return do[*DeployToken](s.client,
		withPath("projects/%s/deploy_tokens/%d", ProjectID{pid}, deployToken),
		withRequestOpts(options...),
	)
}

// CreateProjectDeployTokenOptions represents the available CreateProjectDeployToken() options.
@@ -191,39 +155,21 @@ type CreateProjectDeployTokenOptions struct {
}

func (s *DeployTokensService) CreateProjectDeployToken(pid any, opt *CreateProjectDeployTokenOptions, options ...RequestOptionFunc) (*DeployToken, *Response, error) {
	project, err := parseID(pid)
	if err != nil {
		return nil, nil, err
	}
	u := fmt.Sprintf("projects/%s/deploy_tokens", PathEscape(project))

	req, err := s.client.NewRequest(http.MethodPost, u, opt, options)
	if err != nil {
		return nil, nil, err
	}

	t := new(DeployToken)
	resp, err := s.client.Do(req, t)
	if err != nil {
		return nil, resp, err
	}

	return t, resp, nil
	return do[*DeployToken](s.client,
		withMethod(http.MethodPost),
		withPath("projects/%s/deploy_tokens", ProjectID{pid}),
		withAPIOpts(opt),
		withRequestOpts(options...),
	)
}

func (s *DeployTokensService) DeleteProjectDeployToken(pid any, deployToken int64, options ...RequestOptionFunc) (*Response, error) {
	project, err := parseID(pid)
	if err != nil {
		return nil, err
	}
	u := fmt.Sprintf("projects/%s/deploy_tokens/%d", PathEscape(project), deployToken)

	req, err := s.client.NewRequest(http.MethodDelete, u, nil, options)
	if err != nil {
		return nil, err
	}

	return s.client.Do(req, nil)
	_, resp, err := do[none](s.client,
		withMethod(http.MethodDelete),
		withPath("projects/%s/deploy_tokens/%d", ProjectID{pid}, deployToken),
		withRequestOpts(options...),
	)
	return resp, err
}

// ListGroupDeployTokensOptions represents the available ListGroupDeployTokens()
@@ -236,45 +182,18 @@ type ListGroupDeployTokensOptions struct {
}

func (s *DeployTokensService) ListGroupDeployTokens(gid any, opt *ListGroupDeployTokensOptions, options ...RequestOptionFunc) ([]*DeployToken, *Response, error) {
	group, err := parseID(gid)
	if err != nil {
		return nil, nil, err
	}
	u := fmt.Sprintf("groups/%s/deploy_tokens", PathEscape(group))

	req, err := s.client.NewRequest(http.MethodGet, u, opt, options)
	if err != nil {
		return nil, nil, err
	}

	var ts []*DeployToken
	resp, err := s.client.Do(req, &ts)
	if err != nil {
		return nil, resp, err
	}

	return ts, resp, nil
	return do[[]*DeployToken](s.client,
		withPath("groups/%s/deploy_tokens", GroupID{gid}),
		withAPIOpts(opt),
		withRequestOpts(options...),
	)
}

func (s *DeployTokensService) GetGroupDeployToken(gid any, deployToken int64, options ...RequestOptionFunc) (*DeployToken, *Response, error) {
	group, err := parseID(gid)
	if err != nil {
		return nil, nil, err
	}
	u := fmt.Sprintf("groups/%s/deploy_tokens/%d", PathEscape(group), deployToken)

	req, err := s.client.NewRequest(http.MethodGet, u, nil, options)
	if err != nil {
		return nil, nil, err
	}

	t := new(DeployToken)
	resp, err := s.client.Do(req, t)
	if err != nil {
		return nil, resp, err
	}

	return t, resp, nil
	return do[*DeployToken](s.client,
		withPath("groups/%s/deploy_tokens/%d", GroupID{gid}, deployToken),
		withRequestOpts(options...),
	)
}

// CreateGroupDeployTokenOptions represents the available CreateGroupDeployToken() options.
@@ -289,37 +208,19 @@ type CreateGroupDeployTokenOptions struct {
}

func (s *DeployTokensService) CreateGroupDeployToken(gid any, opt *CreateGroupDeployTokenOptions, options ...RequestOptionFunc) (*DeployToken, *Response, error) {
	group, err := parseID(gid)
	if err != nil {
		return nil, nil, err
	}
	u := fmt.Sprintf("groups/%s/deploy_tokens", PathEscape(group))

	req, err := s.client.NewRequest(http.MethodPost, u, opt, options)
	if err != nil {
		return nil, nil, err
	}

	t := new(DeployToken)
	resp, err := s.client.Do(req, t)
	if err != nil {
		return nil, resp, err
	}

	return t, resp, nil
	return do[*DeployToken](s.client,
		withMethod(http.MethodPost),
		withPath("groups/%s/deploy_tokens", GroupID{gid}),
		withAPIOpts(opt),
		withRequestOpts(options...),
	)
}

func (s *DeployTokensService) DeleteGroupDeployToken(gid any, deployToken int64, options ...RequestOptionFunc) (*Response, error) {
	group, err := parseID(gid)
	if err != nil {
		return nil, err
	}
	u := fmt.Sprintf("groups/%s/deploy_tokens/%d", PathEscape(group), deployToken)

	req, err := s.client.NewRequest(http.MethodDelete, u, nil, options)
	if err != nil {
		return nil, err
	}

	return s.client.Do(req, nil)
	_, resp, err := do[none](s.client,
		withMethod(http.MethodDelete),
		withPath("groups/%s/deploy_tokens/%d", GroupID{gid}, deployToken),
		withRequestOpts(options...),
	)
	return resp, err
}
+144 −614

File changed.

Preview size limit exceeded, changes collapsed.

+9 −32
Original line number Diff line number Diff line
@@ -16,12 +16,6 @@

package gitlab

import (
	"fmt"
	"net/http"
	"net/url"
)

type (
	// DockerfileTemplatesServiceInterface defines all the API methods for the DockerfileTemplatesService
	DockerfileTemplatesServiceInterface interface {
@@ -74,33 +68,16 @@ type ListDockerfileTemplatesOptions struct {
}

func (s *DockerfileTemplatesService) ListTemplates(opt *ListDockerfileTemplatesOptions, options ...RequestOptionFunc) ([]*DockerfileTemplateListItem, *Response, error) {
	req, err := s.client.NewRequest(http.MethodGet, "templates/dockerfiles", opt, options)
	if err != nil {
		return nil, nil, err
	}

	var gs []*DockerfileTemplateListItem
	resp, err := s.client.Do(req, &gs)
	if err != nil {
		return nil, resp, err
	}

	return gs, resp, nil
	return do[[]*DockerfileTemplateListItem](s.client,
		withPath("templates/dockerfiles"),
		withAPIOpts(opt),
		withRequestOpts(options...),
	)
}

func (s *DockerfileTemplatesService) GetTemplate(key string, options ...RequestOptionFunc) (*DockerfileTemplate, *Response, error) {
	u := fmt.Sprintf("templates/dockerfiles/%s", url.PathEscape(key))

	req, err := s.client.NewRequest(http.MethodGet, u, nil, options)
	if err != nil {
		return nil, nil, err
	}

	g := new(DockerfileTemplate)
	resp, err := s.client.Do(req, g)
	if err != nil {
		return nil, resp, err
	}

	return g, resp, nil
	return do[*DockerfileTemplate](s.client,
		withPath("templates/dockerfiles/%s", key),
		withRequestOpts(options...),
	)
}
+27 −88
Original line number Diff line number Diff line
@@ -16,10 +16,7 @@

package gitlab

import (
	"fmt"
	"net/http"
)
import "net/http"

type (
	// ErrorTrackingServiceInterface defines all the API methods for the ErrorTrackingService
@@ -98,24 +95,10 @@ func (p ErrorTrackingSettings) String() string {
}

func (s *ErrorTrackingService) GetErrorTrackingSettings(pid any, options ...RequestOptionFunc) (*ErrorTrackingSettings, *Response, error) {
	project, err := parseID(pid)
	if err != nil {
		return nil, nil, err
	}
	u := fmt.Sprintf("projects/%s/error_tracking/settings", PathEscape(project))

	req, err := s.client.NewRequest(http.MethodGet, u, nil, options)
	if err != nil {
		return nil, nil, err
	}

	ets := new(ErrorTrackingSettings)
	resp, err := s.client.Do(req, ets)
	if err != nil {
		return nil, resp, err
	}

	return ets, resp, nil
	return do[*ErrorTrackingSettings](s.client,
		withPath("projects/%s/error_tracking/settings", ProjectID{pid}),
		withRequestOpts(options...),
	)
}

// EnableDisableErrorTrackingOptions represents the available
@@ -129,24 +112,12 @@ type EnableDisableErrorTrackingOptions struct {
}

func (s *ErrorTrackingService) EnableDisableErrorTracking(pid any, opt *EnableDisableErrorTrackingOptions, options ...RequestOptionFunc) (*ErrorTrackingSettings, *Response, error) {
	project, err := parseID(pid)
	if err != nil {
		return nil, nil, err
	}
	u := fmt.Sprintf("projects/%s/error_tracking/settings", PathEscape(project))

	req, err := s.client.NewRequest(http.MethodPatch, u, opt, options)
	if err != nil {
		return nil, nil, err
	}

	ets := new(ErrorTrackingSettings)
	resp, err := s.client.Do(req, &ets)
	if err != nil {
		return nil, resp, err
	}

	return ets, resp, nil
	return do[*ErrorTrackingSettings](s.client,
		withMethod(http.MethodPatch),
		withPath("projects/%s/error_tracking/settings", ProjectID{pid}),
		withAPIOpts(opt),
		withRequestOpts(options...),
	)
}

// ListClientKeysOptions represents the available ListClientKeys() options.
@@ -158,45 +129,19 @@ type ListClientKeysOptions struct {
}

func (s *ErrorTrackingService) ListClientKeys(pid any, opt *ListClientKeysOptions, options ...RequestOptionFunc) ([]*ErrorTrackingClientKey, *Response, error) {
	project, err := parseID(pid)
	if err != nil {
		return nil, nil, err
	}
	u := fmt.Sprintf("projects/%s/error_tracking/client_keys", PathEscape(project))

	req, err := s.client.NewRequest(http.MethodGet, u, opt, options)
	if err != nil {
		return nil, nil, err
	}

	var cks []*ErrorTrackingClientKey
	resp, err := s.client.Do(req, &cks)
	if err != nil {
		return nil, resp, err
	}

	return cks, resp, nil
	return do[[]*ErrorTrackingClientKey](s.client,
		withPath("projects/%s/error_tracking/client_keys", ProjectID{pid}),
		withAPIOpts(opt),
		withRequestOpts(options...),
	)
}

func (s *ErrorTrackingService) CreateClientKey(pid any, options ...RequestOptionFunc) (*ErrorTrackingClientKey, *Response, error) {
	project, err := parseID(pid)
	if err != nil {
		return nil, nil, err
	}
	u := fmt.Sprintf("projects/%s/error_tracking/client_keys", PathEscape(project))

	req, err := s.client.NewRequest(http.MethodPost, u, nil, options)
	if err != nil {
		return nil, nil, err
	}

	ck := new(ErrorTrackingClientKey)
	resp, err := s.client.Do(req, ck)
	if err != nil {
		return nil, resp, err
	}

	return ck, resp, nil
	return do[*ErrorTrackingClientKey](s.client,
		withMethod(http.MethodPost),
		withPath("projects/%s/error_tracking/client_keys", ProjectID{pid}),
		withRequestOpts(options...),
	)
}

// DeleteClientKey removes a client key from the project.
@@ -204,16 +149,10 @@ func (s *ErrorTrackingService) CreateClientKey(pid any, options ...RequestOption
// GitLab API docs:
// https://docs.gitlab.com/api/error_tracking/#delete-a-client-key
func (s *ErrorTrackingService) DeleteClientKey(pid any, keyID int64, options ...RequestOptionFunc) (*Response, error) {
	project, err := parseID(pid)
	if err != nil {
		return nil, err
	}
	u := fmt.Sprintf("projects/%s/error_tracking/client_keys/%d", PathEscape(project), keyID)

	req, err := s.client.NewRequest(http.MethodDelete, u, nil, options)
	if err != nil {
		return nil, err
	}

	return s.client.Do(req, nil)
	_, resp, err := do[none](s.client,
		withMethod(http.MethodDelete),
		withPath("projects/%s/error_tracking/client_keys/%d", ProjectID{pid}, keyID),
		withRequestOpts(options...),
	)
	return resp, err
}
Loading