master
go 58 lines 1.17 KB
Raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 package dockerhub
4
5 import (
6 "fmt"
7 "net/http"
8 "net/url"
9 "path"
10
11 "github.com/netdata/netdata/go/plugins/pkg/web"
12 )
13
14 type repository struct {
15 User string
16 Name string
17 Status int
18 StarCount int `json:"star_count"`
19 PullCount int `json:"pull_count"`
20 LastUpdated string `json:"last_updated"`
21 }
22
23 func newAPIClient(client *http.Client, request web.RequestConfig) *apiClient {
24 return &apiClient{httpClient: client, request: request}
25 }
26
27 type apiClient struct {
28 httpClient *http.Client
29 request web.RequestConfig
30 }
31
32 func (a apiClient) getRepository(repoName string) (*repository, error) {
33 req, err := a.createRequest(repoName)
34 if err != nil {
35 return nil, fmt.Errorf("error on creating http request : %v", err)
36 }
37
38 var repo repository
39 if err := web.DoHTTP(a.httpClient).RequestJSON(req, &repo); err != nil {
40 return nil, err
41 }
42
43 return &repo, nil
44 }
45
46 func (a apiClient) createRequest(urlPath string) (*http.Request, error) {
47 req := a.request.Copy()
48
49 u, err := url.Parse(req.URL)
50 if err != nil {
51 return nil, err
52 }
53
54 u.Path = path.Join(u.Path, urlPath)
55 req.URL = u.String()
56
57 return web.NewHTTPRequest(req)
58 }