github.com/optim-corp/cios-golang-sdk@v0.5.1/sdk/rest_client.go (about) 1 package ciossdk 2 3 import ( 4 "fmt" 5 "regexp" 6 "time" 7 8 "github.com/dgrijalva/jwt-go" 9 "github.com/fcfcqloow/go-advance/check" 10 cnv "github.com/fcfcqloow/go-advance/convert" 11 "github.com/optim-corp/cios-golang-sdk/cios" 12 sdkmodel "github.com/optim-corp/cios-golang-sdk/model" 13 srvaccount "github.com/optim-corp/cios-golang-sdk/sdk/service/account" 14 srvauth "github.com/optim-corp/cios-golang-sdk/sdk/service/authorization" 15 srvcontract "github.com/optim-corp/cios-golang-sdk/sdk/service/contract" 16 srvdevice "github.com/optim-corp/cios-golang-sdk/sdk/service/device" 17 srvfilestorage "github.com/optim-corp/cios-golang-sdk/sdk/service/filestorage" 18 srvgeography "github.com/optim-corp/cios-golang-sdk/sdk/service/geography" 19 srvlicense "github.com/optim-corp/cios-golang-sdk/sdk/service/license" 20 srvpubsub "github.com/optim-corp/cios-golang-sdk/sdk/service/pubsub" 21 srvvideo "github.com/optim-corp/cios-golang-sdk/sdk/service/video" 22 ciosutil "github.com/optim-corp/cios-golang-sdk/util/cios" 23 ) 24 25 type CiosClient interface { 26 fmt.Stringer 27 Auth() Auth 28 PubSub() PubSub 29 Account() Account 30 DeviceAssetManagement() DeviceAssetManagement 31 DeviceManagement() DeviceManagement 32 FileStorage() FileStorage 33 Geography() Geography 34 License() License 35 Contract() Contract 36 Video() VideoStreaming 37 Debug(debug bool) CiosClient 38 RequestScope(scope string) CiosClient 39 TokenExp() int64 40 SetTokenExp(tokenExp int64) 41 42 _accessToken(accessToken string) CiosClient 43 } 44 type ciosClient struct { 45 pubSub PubSub 46 account Account 47 deviceAssetManagement DeviceAssetManagement 48 deviceManagement DeviceManagement 49 fileStorage FileStorage 50 geography Geography 51 auth Auth 52 license License 53 contract Contract 54 video VideoStreaming 55 tokenExp int64 56 cfg *cios.Configuration 57 } 58 59 func (self *ciosClient) SetTokenExp(tokenExp int64) { 60 self.tokenExp = tokenExp 61 } 62 63 func (self *ciosClient) TokenExp() int64 { 64 return self.tokenExp 65 } 66 67 func NewCiosClient(config CiosClientConfig) CiosClient { 68 instance := new(ciosClient) 69 client := createClient(config.CustomClient, cios.ServerConfigurations{ 70 {URL: config.Urls.AuthUrl}, 71 {URL: config.Urls.AccountsUrl}, 72 {URL: config.Urls.StorageUrl}, 73 {URL: config.Urls.MessagingUrl}, 74 {URL: config.Urls.DeviceManagementUrl}, 75 {URL: config.Urls.DeviceAssetManagementUrl}, 76 {URL: config.Urls.ContractUrl}, 77 {URL: config.Urls.LicenseUrl}, 78 {URL: config.Urls.LocationUrl}, 79 {URL: config.Urls.VideoStreamingUrl}, 80 }, config.Debug) 81 82 instance.cfg = client.GetConfig() 83 account := srvaccount.NewCiosAccount(client, config.Urls.AccountsUrl, getWithHostFunc(sdkmodel.ACCOUNT_INDEX)) 84 contract := srvcontract.NewCiosContract(client, config.Urls.ContractUrl, getWithHostFunc(sdkmodel.CONTRACT_INDEX)) 85 license := srvlicense.NewCiosLicense(client, config.Urls.LicenseUrl, getWithHostFunc(sdkmodel.LICENSE_INDEX)) 86 deviceManagement := srvdevice.NewCiosDeviceManagement(client, config.Urls.DeviceManagementUrl, getWithHostFunc(sdkmodel.DEVICE_INDEX)) 87 deviceAssetManagement := srvdevice.NewCiosDeviceAssetManagement(client, config.Urls.DeviceAssetManagementUrl, getWithHostFunc(sdkmodel.DEVICE_ASSET_INDEX)) 88 fileStorage := srvfilestorage.NewCiosFileStorage(client, config.Urls.StorageUrl, getWithHostFunc(sdkmodel.FILE_STORAGE_INDEX)) 89 geography := srvgeography.NewCiosGeography(client, config.Urls.LocationUrl, getWithHostFunc(sdkmodel.LOCATION_INDEX)) 90 auth := srvauth.NewCiosAuth(client, config.Urls.AuthUrl, getWithHostFunc(sdkmodel.AUTH_INDEX)) 91 pubsub := srvpubsub.NewCiosPubSub(client, config.Urls.MessagingUrl, getWithHostFunc(sdkmodel.MESSAGING_INDEX)) 92 video := srvvideo.NewCiosVideoStreaming(client, config.Urls.VideoStreamingUrl, getWithHostFunc(sdkmodel.VIDEO_STREAMING_INDEX)) 93 94 // AuthConfig 95 if config.AuthConfig != nil { 96 auth.SetClientId(config.AuthConfig.ClientID) 97 auth.SetClientSecret(config.AuthConfig.ClientSecret) 98 auth.SetAssertion(config.AuthConfig.Assertion) 99 auth.SetRef(config.AuthConfig.RefreshToken) 100 auth.SetScope(config.AuthConfig.Scope) 101 } 102 // WebSocketConfig 103 104 if !check.IsNil(config.WebSocketConfig) { 105 pubsub.SetWsReadTimeout(config.WebSocketConfig.ReadTimeoutMilliSec) 106 pubsub.SetWsWriteTimeout(config.WebSocketConfig.WriteTimeoutMilliSec) 107 } 108 refFunc := func() error { 109 if config.AutoRefresh && config.AuthConfig != nil { 110 if instance.tokenExp == 0 || instance.tokenExp-60 <= time.Now().Unix() { 111 switch config.AuthConfig._type { 112 case sdkmodel.CLIENT_TYPE: 113 token, _, _, _, err := instance.auth.GetAccessTokenOnClient() 114 if err != nil { 115 return err 116 } 117 instance._accessToken(token) 118 case sdkmodel.REFRESH_TYPE: 119 token, _, _, _, err := instance.auth.GetAccessTokenByRefreshToken() 120 if err != nil { 121 return err 122 } 123 instance._accessToken(token) 124 case sdkmodel.DEVICE_TYPE: 125 token, _, _, _, err := instance.auth.GetAccessTokenOnDevice() 126 if err != nil { 127 return err 128 } 129 instance._accessToken(token) 130 default: 131 } 132 } 133 } 134 return nil 135 } 136 137 video.SetRefresh(refFunc) 138 account.SetRefresh(refFunc) 139 license.SetRefresh(refFunc) 140 contract.SetRefresh(refFunc) 141 geography.SetRefresh(refFunc) 142 fileStorage.SetRefresh(refFunc) 143 deviceManagement.SetRefresh(refFunc) 144 deviceAssetManagement.SetRefresh(refFunc) 145 pubsub.SetRefresh(refFunc) 146 147 instance.contract = contract 148 instance.license = license 149 instance.account = account 150 instance.deviceManagement = deviceManagement 151 instance.deviceAssetManagement = deviceAssetManagement 152 instance.fileStorage = fileStorage 153 instance.geography = geography 154 instance.auth = auth 155 instance.pubSub = pubsub 156 instance.video = video 157 158 return instance 159 } 160 161 func (self *ciosClient) PubSub() PubSub { 162 return self.pubSub 163 } 164 165 func (self *ciosClient) Account() Account { 166 return self.account 167 } 168 169 func (self *ciosClient) DeviceAssetManagement() DeviceAssetManagement { 170 return self.deviceAssetManagement 171 } 172 173 func (self *ciosClient) DeviceManagement() DeviceManagement { 174 return self.deviceManagement 175 } 176 177 func (self *ciosClient) FileStorage() FileStorage { 178 return self.fileStorage 179 } 180 181 func (self *ciosClient) Geography() Geography { 182 return self.geography 183 } 184 185 func (self *ciosClient) Auth() Auth { 186 return self.auth 187 } 188 189 func (self *ciosClient) License() License { 190 return self.license 191 } 192 193 func (self *ciosClient) Contract() Contract { 194 return self.contract 195 } 196 197 func (self *ciosClient) Video() VideoStreaming { 198 return self.video 199 } 200 201 func (self *ciosClient) Debug(debug bool) CiosClient { 202 if !check.IsNil(self.cfg) { 203 self.cfg.Debug = debug 204 } 205 self.pubSub.SetDebug(debug) 206 return self 207 } 208 func (self *ciosClient) _accessToken(accessToken string) CiosClient { 209 accessToken = regexp.MustCompile(`^bearer|Bearer| `).ReplaceAllString(accessToken, "") 210 token, _, err := new(jwt.Parser).ParseUnverified(accessToken, jwt.MapClaims{}) 211 if err == nil { 212 if claims, ok := token.Claims.(jwt.MapClaims); ok { 213 self.tokenExp = cnv.MustInt64(claims["exp"]) 214 } 215 } 216 self.pubSub.SetToken(accessToken) 217 self.video.SetToken(accessToken) 218 if !check.IsNil(self.cfg) { 219 self.cfg.AddDefaultHeader("Authorization", ciosutil.ParseAccessToken(accessToken)) 220 } 221 return self 222 } 223 func (self *ciosClient) RequestScope(scope string) CiosClient { 224 self.auth.SetScope(scope) 225 return self 226 } 227 228 func (self ciosClient) String() string { 229 // TODO: ToString 230 return "" 231 }