-
Notifications
You must be signed in to change notification settings - Fork 6
/
client.go
93 lines (82 loc) · 2.51 KB
/
client.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
package client
import (
"bytes"
"context"
"encoding/json"
"github.com/Clever/wag/samples/gen-go-deprecated/models"
"net/http"
"net/url"
"strconv"
"strings"
"time"
discovery "github.com/Clever/discovery-go"
)
var _ = json.Marshal
var _ = strings.Replace
var _ = strconv.FormatInt
var _ = bytes.Compare
// WagClient is used to make requests to the swagger-test service.
type WagClient struct {
basePath string
requestDoer doer
transport *http.Transport
timeout time.Duration
// Keep the retry doer around so that we can set the number of retries
retryDoer *retryDoer
defaultTimeout time.Duration
}
var _ Client = (*WagClient)(nil)
// New creates a new client. The base path and http transport are configurable.
func New(basePath string) *WagClient {
base := baseDoer{}
tracing := tracingDoer{d: base}
retry := retryDoer{d: tracing, defaultRetries: 1}
return &WagClient{requestDoer: &retry, retryDoer: &retry, defaultTimeout: 10 * time.Second,
transport: &http.Transport{}, basePath: basePath}
}
// NewFromDiscovery creates a client from the discovery environment variables. This method requires
// the three env vars: SERVICE_SWAGGER_TEST_HTTP_(HOST/PORT/PROTO) to be set. Otherwise it returns an error.
func NewFromDiscovery() (*WagClient, error) {
url, err := discovery.URL("swagger-test", "default")
if err != nil {
url, err = discovery.URL("swagger-test", "http") // Added fallback to maintain reverse compatibility
if err != nil {
return nil, err
}
}
return New(url), nil
}
// WithRetries returns a new client that retries all GET operations until they either succeed or fail the
// number of times specified.
func (c *WagClient) WithRetries(retries int) *WagClient {
c.retryDoer.defaultRetries = retries
return c
}
// WithTimeout returns a new client that has the specified timeout on all operations. To make a single request
// have a timeout use context.WithTimeout as described here: https://godoc.org/golang.org/x/net/context#WithTimeout.
func (c *WagClient) WithTimeout(timeout time.Duration) *WagClient {
c.defaultTimeout = timeout
return c
}
// JoinByFormat joins a string array by a known format:
// csv: comma separated value (default)
// ssv: space separated value
// tsv: tab separated value
// pipes: pipe (|) separated value
func JoinByFormat(data []string, format string) string {
if len(data) == 0 {
return ""
}
var sep string
switch format {
case "ssv":
sep = " "
case "tsv":
sep = "\t"
case "pipes":
sep = "|"
default:
sep = ","
}
return strings.Join(data, sep)
}