Skip to content

Commit ab3cb55

Browse files
committed
Moved some package in their appropriate place.
httpclient is now under arduino/httpclient. Some configuration values have been moved under 'configuration' package, and the related subroutines have been refactored.
1 parent 546a5a7 commit ab3cb55

File tree

8 files changed

+59
-93
lines changed

8 files changed

+59
-93
lines changed

httpclient/httpclient_transport.go renamed to arduino/httpclient/httpclient.go

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,27 +15,47 @@
1515

1616
package httpclient
1717

18-
import "net/http"
18+
import (
19+
"net/http"
20+
"net/url"
1921

20-
type httpClientRoundTripper struct {
21-
transport http.RoundTripper
22-
config *Config
23-
}
22+
"github.com/arduino/arduino-cli/configuration"
23+
)
2424

25-
func newHTTPClientTransport(config *Config) http.RoundTripper {
26-
proxy := http.ProxyURL(config.Proxy)
25+
// Config is the configuration of the http client
26+
type Config struct {
27+
UserAgent string
28+
Proxy *url.URL
29+
}
2730

28-
transport := &http.Transport{
29-
Proxy: proxy,
31+
// New returns a default http client for use in the arduino-cli
32+
func New() (*http.Client, error) {
33+
userAgent := configuration.UserAgent(configuration.Settings)
34+
proxy, err := configuration.NetworkProxy(configuration.Settings)
35+
if err != nil {
36+
return nil, err
3037
}
38+
return NewWithConfig(&Config{UserAgent: userAgent, Proxy: proxy}), nil
39+
}
3140

32-
return &httpClientRoundTripper{
33-
transport: transport,
34-
config: config,
41+
// NewWithConfig creates a http client for use in the arduino-cli, with a given configuration
42+
func NewWithConfig(config *Config) *http.Client {
43+
return &http.Client{
44+
Transport: &httpClientRoundTripper{
45+
transport: &http.Transport{
46+
Proxy: http.ProxyURL(config.Proxy),
47+
},
48+
userAgent: config.UserAgent,
49+
},
3550
}
3651
}
3752

53+
type httpClientRoundTripper struct {
54+
transport http.RoundTripper
55+
userAgent string
56+
}
57+
3858
func (h *httpClientRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
39-
req.Header.Add("User-Agent", h.config.UserAgent)
59+
req.Header.Add("User-Agent", h.userAgent)
4060
return h.transport.RoundTrip(req)
4161
}
File renamed without changes.

arduino/resources/download.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import (
2121
"time"
2222

2323
"github.com/arduino/arduino-cli/arduino"
24-
"github.com/arduino/arduino-cli/httpclient"
24+
"github.com/arduino/arduino-cli/arduino/httpclient"
2525
paths "github.com/arduino/go-paths-helper"
2626
"go.bug.st/downloader/v2"
2727
)

arduino/resources/helpers_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import (
2222
"strings"
2323
"testing"
2424

25-
"github.com/arduino/arduino-cli/httpclient"
25+
"github.com/arduino/arduino-cli/arduino/httpclient"
2626
"github.com/arduino/go-paths-helper"
2727
"github.com/stretchr/testify/require"
2828
"go.bug.st/downloader/v2"

cli/updater/updater.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ import (
2020
"strings"
2121
"time"
2222

23+
"github.com/arduino/arduino-cli/arduino/httpclient"
2324
"github.com/arduino/arduino-cli/cli/feedback"
2425
"github.com/arduino/arduino-cli/cli/globals"
2526
"github.com/arduino/arduino-cli/configuration"
26-
"github.com/arduino/arduino-cli/httpclient"
2727
"github.com/arduino/arduino-cli/i18n"
2828
"github.com/arduino/arduino-cli/inventory"
2929
"github.com/fatih/color"

commands/board/list.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ import (
2828
"github.com/arduino/arduino-cli/arduino"
2929
"github.com/arduino/arduino-cli/arduino/cores/packagemanager"
3030
"github.com/arduino/arduino-cli/arduino/discovery"
31+
"github.com/arduino/arduino-cli/arduino/httpclient"
3132
"github.com/arduino/arduino-cli/commands"
32-
"github.com/arduino/arduino-cli/httpclient"
3333
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
3434
"github.com/pkg/errors"
3535
"github.com/segmentio/stats/v4"

httpclient/httpclient_config.go renamed to configuration/network.go

Lines changed: 22 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -13,47 +13,20 @@
1313
// Arduino software without disclosing the source code of your own applications.
1414
// To purchase a commercial license, send an email to license@arduino.cc.
1515

16-
package httpclient
16+
package configuration
1717

1818
import (
1919
"fmt"
2020
"net/url"
2121
"runtime"
2222

2323
"github.com/arduino/arduino-cli/cli/globals"
24-
"github.com/arduino/arduino-cli/configuration"
24+
"github.com/spf13/viper"
2525
)
2626

27-
// Config is the configuration of the http client
28-
type Config struct {
29-
UserAgent string
30-
Proxy *url.URL
31-
}
32-
33-
// DefaultConfig returns the default http client config
34-
func DefaultConfig() (*Config, error) {
35-
var proxy *url.URL
36-
var err error
37-
if configuration.Settings.IsSet("network.proxy") {
38-
proxyConfig := configuration.Settings.GetString("network.proxy")
39-
if proxyConfig == "" {
40-
// empty configuration
41-
// this workaround must be here until viper can UnSet properties:
42-
// https://github.com/spf13/viper/pull/519
43-
} else if proxy, err = url.Parse(proxyConfig); err != nil {
44-
return nil, fmt.Errorf(tr("Invalid network.proxy '%[1]s': %[2]s"), proxyConfig, err)
45-
}
46-
}
47-
48-
return &Config{
49-
UserAgent: UserAgent(),
50-
Proxy: proxy,
51-
}, nil
52-
}
53-
54-
// UserAgent returns the user agent for the cli http client
55-
func UserAgent() string {
56-
subComponent := configuration.Settings.GetString("network.user_agent_ext")
27+
// UserAgent returns the user agent for the cli (mainly used by http clients)
28+
func UserAgent(settings *viper.Viper) string {
29+
subComponent := settings.GetString("network.user_agent_ext")
5730
if subComponent != "" {
5831
subComponent = " " + subComponent
5932
}
@@ -65,3 +38,20 @@ func UserAgent() string {
6538
runtime.GOARCH, runtime.GOOS, runtime.Version(),
6639
globals.VersionInfo.Commit)
6740
}
41+
42+
// NetworkProxy returns the proxy configuration (mainly used by http clients)
43+
func NetworkProxy(settings *viper.Viper) (*url.URL, error) {
44+
if !settings.IsSet("network.proxy") {
45+
return nil, nil
46+
}
47+
if proxyConfig := settings.GetString("network.proxy"); proxyConfig == "" {
48+
// empty configuration
49+
// this workaround must be here until viper can UnSet properties:
50+
// https://github.com/spf13/viper/pull/519
51+
return nil, nil
52+
} else if proxy, err := url.Parse(proxyConfig); err != nil {
53+
return nil, fmt.Errorf(tr("Invalid network.proxy '%[1]s': %[2]s"), proxyConfig, err)
54+
} else {
55+
return proxy, nil
56+
}
57+
}

httpclient/httpclient.go

Lines changed: 0 additions & 44 deletions
This file was deleted.

0 commit comments

Comments
 (0)