internal/registry/client.go (48 lines of code) (raw):

// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one // or more contributor license agreements. Licensed under the Elastic License; // you may not use this file except in compliance with the Elastic License. package registry import ( "fmt" "io" "net/http" "net/url" ) const ( productionURL = "https://epr.elastic.co" ) var ( // Production is a pre-configured production client Production = NewClient(productionURL) ) // Client is responsible for exporting dashboards from Kibana. type Client struct { baseURL string } // NewClient creates a new instance of the client. func NewClient(baseURL string) *Client { return &Client{ baseURL: baseURL, } } func (c *Client) get(resourcePath string) (int, []byte, error) { base, err := url.Parse(c.baseURL) if err != nil { return 0, nil, fmt.Errorf("could not parse base URL: %v: %w", c.baseURL, err) } rel, err := url.Parse(resourcePath) if err != nil { return 0, nil, fmt.Errorf("could not create relative URL from resource path: %v: %w", resourcePath, err) } u := base.JoinPath(rel.EscapedPath()) u.RawQuery = rel.RawQuery req, err := http.NewRequest(http.MethodGet, u.String(), nil) if err != nil { return 0, nil, fmt.Errorf("could not create request to Package Registry API resource: %s: %w", resourcePath, err) } client := http.Client{} resp, err := client.Do(req) if err != nil { return 0, nil, fmt.Errorf("could not send request to Package Registry API: %w", err) } defer resp.Body.Close() body, err := io.ReadAll(resp.Body) if err != nil { return resp.StatusCode, nil, fmt.Errorf("could not read response body: %w", err) } return resp.StatusCode, body, nil }