@@ -23,31 +23,32 @@ import (
23
23
"net/http"
24
24
)
25
25
26
- // GenericPackageStatusValue represents a GitLab Package Status.
27
- type GenericPackageStatusValue string
28
-
29
- // These constants represent all valid package statuses.
30
- const (
31
- Default GenericPackageStatusValue = "default"
32
- Hidden GenericPackageStatusValue = "hidden"
33
- )
34
-
35
- // GenericPackagesService handles communication with the packages related methods
36
- // of the GitLab API.
26
+ // GenericPackagesService handles communication with the packages related
27
+ // methods of the GitLab API.
37
28
//
38
- // GitLab docs: https://docs.gitlab.com/ee/user/packages/generic_packages/index.html
29
+ // GitLab docs:
30
+ // https://docs.gitlab.com/ee/user/packages/generic_packages/index.html
39
31
type GenericPackagesService struct {
40
32
client * Client
41
33
}
42
34
43
- // DownloadPackageFile allows you to download the package file.
35
+ // PublishPackageFileOptions represents the available PublishPackageFile()
36
+ // options.
44
37
//
45
38
// GitLab docs:
46
39
// https://docs.gitlab.com/ee/user/packages/generic_packages/index.html#download-package-file
47
- func (s * GenericPackagesService ) DownloadPackageFile (pid interface {}, packageName , packageVersion , fileName string , options ... RequestOptionFunc ) ([]byte , * Response , error ) {
40
+ type PublishPackageFileOptions struct {
41
+ Status * GenericPackageStatusValue `url:"status,omitempty" json:"status,omitempty"`
42
+ }
43
+
44
+ // PublishPackageFile uploads a file to a project's package registry.
45
+ //
46
+ // GitLab docs:
47
+ // https://docs.gitlab.com/ee/user/packages/generic_packages/index.html#download-package-file
48
+ func (s * GenericPackagesService ) PublishPackageFile (pid interface {}, packageName , packageVersion , fileName string , content io.Reader , opt * PublishPackageFileOptions , options ... RequestOptionFunc ) (* Response , error ) {
48
49
project , err := parseID (pid )
49
50
if err != nil {
50
- return nil , nil , err
51
+ return nil , err
51
52
}
52
53
u := fmt .Sprintf (
53
54
"projects/%s/packages/generic/%s/%s/%s" ,
@@ -57,37 +58,29 @@ func (s *GenericPackagesService) DownloadPackageFile(pid interface{}, packageNam
57
58
pathEscape (fileName ),
58
59
)
59
60
60
- req , err := s .client .NewRequest (http .MethodGet , u , nil , options )
61
- if err != nil {
62
- return nil , nil , err
63
- }
64
-
65
- var f bytes.Buffer
66
- resp , err := s .client .Do (req , & f )
61
+ // We need to create the request as a GET request to make sure the options
62
+ // are set correctly. After the request is created we will overwrite both
63
+ // the method and the body.
64
+ req , err := s .client .NewRequest (http .MethodGet , u , opt , options )
67
65
if err != nil {
68
- return nil , resp , err
66
+ return nil , err
69
67
}
70
68
71
- return f .Bytes (), resp , err
72
- }
69
+ // Overwrite the method and body.
70
+ req .Method = http .MethodPut
71
+ req .SetBody (content )
73
72
74
- // PublishPackageFileOptions represents the available PublishPackageFile() options.
75
- //
76
- // GitLab docs:
77
- // https://docs.gitlab.com/ee/user/packages/generic_packages/index.html#download-package-file
78
- type PublishPackageFileOptions struct {
79
- Status * GenericPackageStatusValue `url:"status,omitempty" json:"status,omitempty"`
73
+ return s .client .Do (req , nil )
80
74
}
81
75
82
- // PublishPackageFile uploads a file to a project's Package Registry.
83
- // Returns the package URL, the response body, the Response, and any error.
76
+ // DownloadPackageFile allows you to download the package file.
84
77
//
85
78
// GitLab docs:
86
79
// https://docs.gitlab.com/ee/user/packages/generic_packages/index.html#download-package-file
87
- func (s * GenericPackagesService ) PublishPackageFile (pid interface {}, packageName , packageVersion , fileName string , content io. ReadCloser , opt * PublishPackageFileOptions , options ... RequestOptionFunc ) (string , []byte , * Response , error ) {
80
+ func (s * GenericPackagesService ) DownloadPackageFile (pid interface {}, packageName , packageVersion , fileName string , options ... RequestOptionFunc ) ([]byte , * Response , error ) {
88
81
project , err := parseID (pid )
89
82
if err != nil {
90
- return "" , nil , nil , err
83
+ return nil , nil , err
91
84
}
92
85
u := fmt .Sprintf (
93
86
"projects/%s/packages/generic/%s/%s/%s" ,
@@ -97,22 +90,16 @@ func (s *GenericPackagesService) PublishPackageFile(pid interface{}, packageName
97
90
pathEscape (fileName ),
98
91
)
99
92
100
- // This is currently the only way to use a PUT request to upload a non-JSON file.
101
- // We invoke NewRequest with MethodGet so the body is not marshalled to JSON;
102
- // WithUploadFile modifies the request to MethodPut
103
- options = append (options , WithUploadFile (content ))
104
- req , err := s .client .NewRequest (http .MethodGet , u , opt , options )
93
+ req , err := s .client .NewRequest (http .MethodGet , u , nil , options )
105
94
if err != nil {
106
- return "" , nil , nil , err
95
+ return nil , nil , err
107
96
}
108
97
109
98
var f bytes.Buffer
110
99
resp , err := s .client .Do (req , & f )
111
100
if err != nil {
112
- return "" , nil , resp , err
101
+ return nil , resp , err
113
102
}
114
103
115
- // ${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/generic/my_package/0.0.1/file.txt'
116
- downloadURL := fmt .Sprintf ("%s%s" , s .client .BaseURL (), u )
117
- return downloadURL , f .Bytes (), resp , err
104
+ return f .Bytes (), resp , err
118
105
}
0 commit comments