MCPcopy
hub / github.com/apache/devlake / NewApiClient

Function NewApiClient

backend/helpers/pluginhelper/api/api_client.go:105–186  ·  view source on GitHub ↗

NewApiClient creates a new synchronize ApiClient

(
	ctx gocontext.Context,
	endpoint string,
	headers map[string]string,
	timeout time.Duration,
	proxy string,
	br context.BasicRes,
)

Source from the content-addressed store, hash-verified

103
104// NewApiClient creates a new synchronize ApiClient
105func NewApiClient(
106 ctx gocontext.Context,
107 endpoint string,
108 headers map[string]string,
109 timeout time.Duration,
110 proxy string,
111 br context.BasicRes,
112) (*ApiClient, errors.Error) {
113 cfg := br.GetConfigReader()
114 log := br.GetLogger()
115
116 // endpoint blacklist
117 endpointCidrBlacklist := cfg.GetString("ENDPOINT_CIDR_BLACKLIST")
118 if endpointCidrBlacklist != "" {
119 err := checkCidrBlacklist(endpointCidrBlacklist, endpoint, log)
120 if err != nil {
121 return nil, err
122 }
123 }
124
125 apiClient := &ApiClient{}
126 apiClient.Setup(
127 endpoint,
128 headers,
129 timeout,
130 )
131 // create the Transport
132 apiClient.client.Transport = &http.Transport{}
133
134 // set insecureSkipVerify
135 insecureSkipVerify := cfg.GetBool("IN_SECURE_SKIP_VERIFY")
136 if insecureSkipVerify {
137 apiClient.client.Transport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
138 }
139
140 if proxy != "" {
141 err := apiClient.SetProxy(proxy)
142 if err != nil {
143 return nil, errors.Convert(err)
144 }
145 // check connectivity
146 res, err := apiClient.Get("/", nil, nil)
147 if err != nil {
148 return nil, err
149 }
150 if res.StatusCode == http.StatusBadGateway {
151 return nil, errors.BadInput.New(fmt.Sprintf("fail to connect to %v via %v", endpoint, proxy))
152 }
153 } else {
154 // check connectivity
155 parsedUrl, err := url.Parse(endpoint)
156 if err != nil {
157 return nil, errors.BadInput.Wrap(err, fmt.Sprintf("Invalid URL: %s", endpoint))
158 }
159 if parsedUrl.Scheme == "" {
160 return nil, errors.BadInput.New("Invalid URL scheme")
161 }
162 err = utils.CheckDNS(parsedUrl.Hostname())

Callers 1

Calls 12

SetupMethod · 0.95
SetProxyMethod · 0.95
GetMethod · 0.95
SetContextMethod · 0.95
checkCidrBlacklistFunction · 0.85
GetStringMethod · 0.80
GetBoolMethod · 0.80
WrapMethod · 0.80
GetConfigReaderMethod · 0.65
GetLoggerMethod · 0.65
NewMethod · 0.65
ConvertMethod · 0.45

Tested by

no test coverage detected