Commit 5c80895c authored by Timo Furrer's avatar Timo Furrer
Browse files

Merge branch 'bytes-do-requests' into 'main'

Migrate bytes endpoints to new `do` pattern

See merge request gitlab-org/api/client-go!2738
parents bcbfbb96 5376529f
Loading
Loading
Loading
Loading
Loading
+5 −13
Original line number Diff line number Diff line
@@ -2,7 +2,6 @@ package gitlab

import (
	"bytes"
	"fmt"
	"io"
	"net/http"
)
@@ -100,19 +99,12 @@ func (s *DependencyListExportService) GetDependencyListExport(id int64, options
}

func (s *DependencyListExportService) DownloadDependencyListExport(id int64, options ...RequestOptionFunc) (io.Reader, *Response, error) {
	// GET /dependency_list_exports/:id/download
	downloadExportPath := fmt.Sprintf("dependency_list_exports/%d/download", id)

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

	var sbomBuffer bytes.Buffer
	resp, err := s.client.Do(req, &sbomBuffer)
	buf, resp, err := do[bytes.Buffer](s.client,
		withPath("dependency_list_exports/%d/download", id),
		withRequestOpts(options...),
	)
	if err != nil {
		return nil, resp, err
	}

	return &sbomBuffer, resp, nil
	return &buf, resp, nil
}
+4 −20
Original line number Diff line number Diff line
@@ -148,28 +148,12 @@ func (s *GenericPackagesService) PublishPackageFile(pid any, packageName, packag
// GitLab docs:
// https://docs.gitlab.com/user/packages/generic_packages/#download-a-single-file
func (s *GenericPackagesService) DownloadPackageFile(pid any, packageName, packageVersion, fileName string, options ...RequestOptionFunc) ([]byte, *Response, error) {
	project, err := parseID(pid)
	if err != nil {
		return nil, nil, err
	}
	u := fmt.Sprintf(
		"projects/%s/packages/generic/%s/%s/%s",
		PathEscape(project),
		PathEscape(packageName),
		PathEscape(packageVersion),
		PathEscape(fileName),
	buf, resp, err := do[bytes.Buffer](s.client,
		withPath("projects/%s/packages/generic/%s/%s/%s", ProjectID{pid}, packageName, packageVersion, fileName),
		withRequestOpts(options...),
	)

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

	var f bytes.Buffer
	resp, err := s.client.Do(req, &f)
	if err != nil {
		return nil, resp, err
	}

	return f.Bytes(), resp, err
	return buf.Bytes(), resp, nil
}
+6 −2
Original line number Diff line number Diff line
@@ -963,8 +963,12 @@ func (c *Client) NewRequestToURL(method string, u *url.URL, opt any, options []R
		}
	}

	// Set the request specific headers.
	maps.Copy(req.Header, reqHeaders)
	// Set the request specific headers if they don't yet exist.
	for k, v := range reqHeaders {
		if _, ok := req.Header[k]; !ok {
			req.Header[k] = v
		}
	}

	return req, nil
}
+5 −15
Original line number Diff line number Diff line
@@ -19,7 +19,6 @@ package gitlab
import (
	"bytes"
	"errors"
	"fmt"
	"io"
	"mime/multipart"
	"net/http"
@@ -65,23 +64,14 @@ func (s *GroupImportExportService) ScheduleExport(gid any, options ...RequestOpt
// GitLab API docs:
// https://docs.gitlab.com/api/group_import_export/#export-download
func (s *GroupImportExportService) ExportDownload(gid any, options ...RequestOptionFunc) (*bytes.Reader, *Response, error) {
	group, err := parseID(gid)
	if err != nil {
		return nil, nil, err
	}

	req, err := s.client.NewRequest(http.MethodGet, fmt.Sprintf("groups/%s/export/download", group), nil, options)
	if err != nil {
		return nil, nil, err
	}

	exportDownload := new(bytes.Buffer)
	resp, err := s.client.Do(req, exportDownload)
	buf, resp, err := do[bytes.Buffer](s.client,
		withPath("groups/%s/export/download", GroupID{gid}),
		withRequestOpts(options...),
	)
	if err != nil {
		return nil, resp, err
	}

	return bytes.NewReader(exportDownload.Bytes()), resp, err
	return bytes.NewReader(buf.Bytes()), resp, nil
}

// GroupImportFileOptions represents the available ImportFile() options.
+5 −18
Original line number Diff line number Diff line
@@ -2,7 +2,6 @@ package gitlab

import (
	"bytes"
	"fmt"
	"net/http"
	"time"
)
@@ -99,25 +98,13 @@ func (s *GroupRelationsExportService) ListExportStatus(gid any, opt *ListGroupRe
}

func (s *GroupRelationsExportService) ExportDownload(gid any, opt *GroupRelationsDownloadOptions, options ...RequestOptionFunc) (*bytes.Reader, *Response, error) {
	group, err := parseID(gid)
	if err != nil {
		return nil, nil, err
	}

	u := fmt.Sprintf("groups/%s/export_relations/download",
		PathEscape(group),
	buf, resp, err := do[bytes.Buffer](s.client,
		withPath("groups/%s/export_relations/download", GroupID{gid}),
		withAPIOpts(opt),
		withRequestOpts(options...),
	)

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

	groupRelationsExportDownload := new(bytes.Buffer)
	resp, err := s.client.Do(req, groupRelationsExportDownload)
	if err != nil {
		return nil, resp, err
	}

	return bytes.NewReader(groupRelationsExportDownload.Bytes()), resp, err
	return bytes.NewReader(buf.Bytes()), resp, nil
}
Loading