-
Notifications
You must be signed in to change notification settings - Fork 45
/
api.go
93 lines (80 loc) · 2.96 KB
/
api.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package api
import (
"time"
runtimeclient "github.com/go-openapi/runtime/client"
"github.com/elastic/cloud-sdk-go/pkg/auth"
"github.com/elastic/cloud-sdk-go/pkg/client"
)
// API contains all of the API clients and authentication objects necessary
// for the EC API
type API struct {
V1API *client.Rest
AuthWriter auth.Writer
}
// AuthWriter wraps the runtime.ClientAuthInfoWriter interface adding a method
// to Auth generic http.Request.
// This type alias is used to maintain API compatibility.
type AuthWriter auth.Writer
// NewAPI initializes the API clients from an API config that it receives
func NewAPI(c Config) (*API, error) {
c.fillDefaults()
if err := c.Validate(); err != nil {
return nil, err
}
t, err := NewTransport(c.Client.Transport, TransportConfig{
SkipTLSVerify: c.SkipTLSVerify,
ErrorDevice: c.ErrorDevice,
VerboseSettings: c.VerboseSettings,
Timeout: c.Timeout,
UserAgent: c.UserAgent,
Retries: c.Retries,
RetryBackoff: c.RetryBackoff,
})
if err != nil {
return nil, err
}
c.Client.Transport = t
// Sadly, all the client parameters take the DefaultTimeout from the runtime
// client if not specified in the call as a query parameter, modifying this
// value effectively affects all of the related clients.
runtimeclient.DefaultTimeout = c.Timeout
// If retries are set, then we don't want the framework cancellation to kick in
// while we're still retrying requests, to enable that, we're multiplying the
// specified timeout by the # of retries, accounting for the backoffs as well.
if c.Retries > 0 {
if c.RetryBackoff.Microseconds() <= 0 {
c.RetryBackoff = defaultBackoff
}
runtimeclient.DefaultTimeout = c.Timeout*time.Duration(c.Retries) + (c.RetryBackoff * time.Duration(c.Retries))
// We leave the http client with no timeout, since we don't want yet another
// layer of context cancellatins to step in.
c.Client.Timeout = 0
}
transport, err := NewCloudClientRuntime(c)
if err != nil {
return nil, err
}
var api = API{AuthWriter: c.AuthWriter, V1API: client.New(transport, nil)}
if !c.SkipLogin {
if err := LoginUser(&api, c.ErrorDevice); err != nil {
return nil, err
}
}
return &api, nil
}