feat: basic sdk complete
This commit is contained in:
1 parent
216c5e53ab
commit
c39dce1380
2 files changed
+84
No files matched your search
@@ -0,0 +1,81 @@
|
|||||||
|
package gosdk
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
|
"io"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Configuration struct {
|
||||||
|
BackendUrl string
|
||||||
|
Secret string
|
||||||
|
ServiceId string
|
||||||
|
BypassingUids []string
|
||||||
|
UseStubs bool
|
||||||
|
StubsDefault bool
|
||||||
|
}
|
||||||
|
|
||||||
|
type requestBody struct {
|
||||||
|
Secret string `json:"signingSecret"`
|
||||||
|
ServiceId string `json:"name"`
|
||||||
|
Uid string `json:"uid"`
|
||||||
|
ProductId string `json:"productID"`
|
||||||
|
ProductType string `json:"type"`
|
||||||
|
}
|
||||||
|
|
||||||
|
var conf *Configuration
|
||||||
|
var usingStubs bool
|
||||||
|
var failCountSinceLastSuccess int
|
||||||
|
|
||||||
|
func Init(config Configuration) {
|
||||||
|
conf = &config
|
||||||
|
usingStubs = false
|
||||||
|
failCountSinceLastSuccess = 0
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if a given users owns a product. Make sure to always
|
||||||
|
func CheckOwned(uid string, productId string, productType string) bool {
|
||||||
|
if conf == nil {
|
||||||
|
panic("CheckOwned called prior to Store SDK initialization")
|
||||||
|
}
|
||||||
|
|
||||||
|
if usingStubs || conf.UseStubs {
|
||||||
|
return conf.StubsDefault
|
||||||
|
}
|
||||||
|
|
||||||
|
// Bypass check
|
||||||
|
for _, v := range conf.BypassingUids {
|
||||||
|
if v == uid {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create request
|
||||||
|
data, err := json.Marshal(requestBody{
|
||||||
|
Secret: conf.Secret,
|
||||||
|
ServiceId: conf.ServiceId,
|
||||||
|
Uid: uid,
|
||||||
|
ProductId: productId,
|
||||||
|
ProductType: productType,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
res, err := http.Post(conf.BackendUrl+"/sdk/api/check", "application/json", bytes.NewBuffer(data))
|
||||||
|
if err != nil {
|
||||||
|
if failCountSinceLastSuccess > 10 {
|
||||||
|
usingStubs = true
|
||||||
|
}
|
||||||
|
failCountSinceLastSuccess += 1
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
failCountSinceLastSuccess = 0
|
||||||
|
|
||||||
|
body, err := io.ReadAll(res.Body)
|
||||||
|
if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return string(body) == "true"
|
||||||
|
}
|
||||||
Reference in new issue
Block a user