-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
52 lines (45 loc) · 1.06 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
package openapi
import (
"net/http"
"net/url"
"strings"
httptransport "github.com/go-openapi/runtime/client"
sdinsurehttp "github.com/sdinsure/agent/pkg/http"
)
func MustNew(endpoint string, basePath string, client *http.Client) *httptransport.Runtime {
tr, err := New(endpoint, basePath, client)
if err != nil {
panic(err)
}
return tr
}
func New(endpoint string, basePath string, client *http.Client) (*httptransport.Runtime, error) {
if client == nil {
client = sdinsurehttp.NewHttpClient()
}
defaultScheme := "http"
defaultHost := endpoint
if hasScheme(endpoint) {
hostUrl, err := url.Parse(endpoint)
if err != nil {
return nil, err
}
if len(hostUrl.Scheme) > 0 {
defaultScheme = hostUrl.Scheme
}
defaultHost = hostUrl.Host
}
httptransportclient := httptransport.NewWithClient(
defaultHost,
basePath,
[]string{defaultScheme},
client,
)
return httptransportclient, nil
}
func hasScheme(endpoint string) bool {
if strings.HasPrefix(endpoint, "http://") || strings.HasPrefix(endpoint, "https://") {
return true
}
return false
}