@@ -31,21 +31,38 @@ func New(registryURL string) *Client {
3131 return & Client {
3232 registryURL : registryURL ,
3333 httpClient : & http.Client {
34- Timeout : 30 * time .Second ,
34+ Timeout : 5 * time .Minute ,
3535 },
3636 }
3737}
3838
39- func (c * Client ) sendRequest (ctx context.Context , method , endpoint string , body io.Reader ) (* http.Response , error ) {
40- url , err := url .JoinPath (c .registryURL , endpoint )
39+ func setAuth (adminAccessToken string ) func (r * http.Request ) {
40+ return func (r * http.Request ) {
41+ r .Header .Set ("Authorization" , adminAccessToken )
42+ }
43+ }
44+
45+ func getPluginURL (pluginName string ) string {
46+ return fmt .Sprintf ("plugins/%s" , pluginName )
47+ }
48+
49+ func getPluginReleaseURL (pluginName , version string ) string {
50+ return fmt .Sprintf ("%s/versions/%s" , getPluginURL (pluginName ), version )
51+ }
52+
53+ func (c * Client ) sendRequest (ctx context.Context , method , endpoint string , body io.Reader , modifyRequestFns ... func (r * http.Request )) (* http.Response , error ) {
54+ apiEndpoint , err := url .JoinPath (c .registryURL , endpoint )
4155 if err != nil {
4256 return nil , err
4357 }
44- req , err := http .NewRequestWithContext (ctx , method , url , body )
58+ req , err := http .NewRequestWithContext (ctx , method , apiEndpoint , body )
4559 if err != nil {
4660 return nil , err
4761 }
4862 req .Header .Set ("Accept" , "application/json; charset=utf-8" )
63+ for _ , f := range modifyRequestFns {
64+ f (req )
65+ }
4966 return c .httpClient .Do (req )
5067}
5168
@@ -79,8 +96,8 @@ func (c *Client) GetPlugins(ctx context.Context) ([]string, error) {
7996 return plugins , nil
8097}
8198
82- func (c * Client ) GetPlugin (ctx context.Context , plugin string ) (* registry.Plugin , error ) {
83- resp , err := c .sendRequest (ctx , http .MethodGet , fmt . Sprintf ( "plugins/%s" , plugin ), nil )
99+ func (c * Client ) GetPlugin (ctx context.Context , pluginName string ) (* registry.Plugin , error ) {
100+ resp , err := c .sendRequest (ctx , http .MethodGet , getPluginURL ( pluginName ), nil )
84101 if err != nil {
85102 return nil , err
86103 }
@@ -92,8 +109,8 @@ func (c *Client) GetPlugin(ctx context.Context, plugin string) (*registry.Plugin
92109 return & p , nil
93110}
94111
95- func (c * Client ) GetPluginRelease (ctx context.Context , plugin , version string ) (* registry.PluginRelease , error ) {
96- resp , err := c .sendRequest (ctx , http .MethodGet , fmt . Sprintf ( "plugins/%s/versions/%s" , plugin , version ), nil )
112+ func (c * Client ) GetPluginRelease (ctx context.Context , pluginName , version string ) (* registry.PluginRelease , error ) {
113+ resp , err := c .sendRequest (ctx , http .MethodGet , getPluginReleaseURL ( pluginName , version ), nil )
97114 if err != nil {
98115 return nil , err
99116 }
@@ -122,3 +139,38 @@ func (c *Client) SendBatchRequest(ctx context.Context, batch *registry.BatchRequ
122139 }
123140 return & br , nil
124141}
142+
143+ func (c * Client ) UpdatePlugins (ctx context.Context , adminAccessToken string ) error {
144+ return c .UpdatePluginRelease (ctx , adminAccessToken , "" , "" )
145+ }
146+
147+ func (c * Client ) UpdatePlugin (ctx context.Context , adminAccessToken , pluginName string ) error {
148+ return c .UpdatePluginRelease (ctx , adminAccessToken , pluginName , "" )
149+ }
150+
151+ func (c * Client ) UpdatePluginRelease (ctx context.Context , adminAccessToken , pluginName , version string ) error {
152+ var apiURL string
153+ switch {
154+ case pluginName == "" && version == "" :
155+ apiURL = "plugins"
156+ case pluginName == "" && version != "" :
157+ return fmt .Errorf ("plugin name is required when version is specified" )
158+ case pluginName != "" && version == "" :
159+ apiURL = getPluginURL (pluginName )
160+ case pluginName != "" && version != "" :
161+ apiURL = getPluginReleaseURL (pluginName , version )
162+ }
163+ resp , err := c .sendRequest (ctx , http .MethodPut , apiURL , nil , setAuth (adminAccessToken ))
164+ if err != nil {
165+ return err
166+ }
167+ var updateResponse map [string ]bool
168+ err = c .decodeResponse (resp , & updateResponse )
169+ if err != nil {
170+ return err
171+ }
172+ if ! updateResponse ["ok" ] {
173+ return fmt .Errorf ("update plugin %s@%s failed: reason unknown" , pluginName , version )
174+ }
175+ return nil
176+ }
0 commit comments