From c39dce1380e25b851ed54fa4751603759b8c2f43 Mon Sep 17 00:00:00 2001 From: Janis Hutz Date: Sat, 26 Sep 2026 16:08:30 +0200 Subject: [PATCH] feat: basic sdk complete --- go.mod | 3 +++ main.go | 81 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+) create mode 100644 go.mod create mode 100644 main.go diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..225efb0 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module git.janishutz.com/janishutz-store/go-sdk + +go 1.27.1 diff --git a/main.go b/main.go new file mode 100644 index 0000000..1a5fa6d --- /dev/null +++ b/main.go @@ -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" +}