-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsdk.go
138 lines (116 loc) · 3.3 KB
/
sdk.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package unicornsdk
import (
"fmt"
"github.com/imroc/req/v3"
"github.com/spf13/viper"
"sync"
"time"
)
type APIError struct {
Detail string `json:"detail"`
}
func (e *APIError) Error() string {
return fmt.Sprintf("API Error: %s", e.Detail)
}
type PlatForm string
const (
WINDOWS PlatForm = "WINDOWS"
ANDROID PlatForm = "ANDROID"
IOS PlatForm = "IOS"
OSX PlatForm = "OSX"
)
func (self PlatForm) String() string {
return string(self)
}
var instance *UnicornSdk
var once sync.Once
var client = req.C()
func GetSdkInstance() *UnicornSdk {
once.Do(func() {
instance = &UnicornSdk{}
v := viper.New()
baseURL := "https://us.unicorn-bot.com"
defaultTimeout := 30 * time.Second
// default config
instance.v = v
//client.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true})
client.SetBaseURL(baseURL).
SetCommonError(&APIError{}).
SetTimeout(defaultTimeout).
SetCookieJar(nil).
OnBeforeRequest(func(client *req.Client, req *req.Request) error {
req.SetBearerAuthToken(GetSdkInstance().GetAcessToken())
return nil
}).
OnAfterResponse(func(client *req.Client, resp *req.Response) error {
if resp.Err != nil {
// resp.Err represents the underlying error, e.g. network error, or unmarshal error (SetResult or SetError was invoked before).
// Append dump content to original underlying error to help troubleshoot if request has been sent.
if dump := resp.Dump(); dump != "" {
resp.Err = fmt.Errorf("%s\nraw content:\n%s", resp.Err.Error(), resp.Dump())
}
return nil // Skip the following logic if there is an underlying error.
}
// Return a human-readable error if server api returned an error message.
if err, ok := resp.Error().(*APIError); ok {
if err.Error() == "Not authenticated" {
resp.Err = NotAuthenticated{err.Detail}
} else {
resp.Err = err
}
return nil
}
// Corner case: neither an error response nor a success response (e.g. status code < 200),
// dump content to help troubleshoot.
if !resp.IsSuccess() {
resp.Err = fmt.Errorf("bad response, raw content:\n%s", resp.Dump())
return nil
}
return nil
})
})
return instance
}
type UnicornSdk struct {
v *viper.Viper
}
func (self *UnicornSdk) Auth(access_token string) *UnicornSdk {
self.v.Set("access_token", access_token)
return self
}
func (self *UnicornSdk) GetAcessToken() string {
return self.v.GetString("access_token")
}
func (self *UnicornSdk) SetProxysForSdk(sdk_proxy string) *UnicornSdk {
self.v.Set("sdk_proxy", sdk_proxy)
client.SetProxyURL(sdk_proxy)
return self
}
func (self *UnicornSdk) SetApiUrl(apiUrl string) *UnicornSdk {
self.v.Set("api_url", apiUrl)
client.SetBaseURL(apiUrl)
return self
}
func (self *UnicornSdk) SetDebug(enable bool) *UnicornSdk {
if enable {
client.EnableDebugLog()
client.EnableDumpAll()
} else {
client.DisableDebugLog()
client.DisableDumpAll()
}
return self
}
func (self *UnicornSdk) SetTimeout(d time.Duration) *UnicornSdk {
client.SetTimeout(d)
return self
}
func (self *UnicornSdk) SetRootCertFromString(pemContent string) *UnicornSdk {
client.SetRootCertFromString(pemContent)
return self
}
func (self *UnicornSdk) CreateDeviceSession() *DeviceSession {
deviceSession := &DeviceSession{}
deviceSession.bundle = viper.New()
return deviceSession
}