|
| 1 | +package api |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "encoding/json" |
| 6 | + "github.com/supertokens/supertokens-golang/recipe/dashboard/dashboardmodels" |
| 7 | + "github.com/supertokens/supertokens-golang/supertokens" |
| 8 | + "net/http" |
| 9 | +) |
| 10 | + |
| 11 | +type analyticsPostResponse struct { |
| 12 | + Status string `json:"status"` |
| 13 | +} |
| 14 | + |
| 15 | +type analyticsPostRequestBody struct { |
| 16 | + Email *string `json:"email"` |
| 17 | + DashboardVersion *string `json:"dashboardVersion"` |
| 18 | +} |
| 19 | + |
| 20 | +func AnalyticsPost(apiInterface dashboardmodels.APIInterface, options dashboardmodels.APIOptions) (analyticsPostResponse, error) { |
| 21 | + supertokensInstance, instanceError := supertokens.GetInstanceOrThrowError() |
| 22 | + |
| 23 | + if supertokens.IsRunningInTestMode() { |
| 24 | + return analyticsPostResponse{ |
| 25 | + Status: "OK", |
| 26 | + }, nil |
| 27 | + } |
| 28 | + |
| 29 | + if instanceError != nil { |
| 30 | + return analyticsPostResponse{}, instanceError |
| 31 | + } |
| 32 | + |
| 33 | + if supertokensInstance.Telemetry != nil && !*supertokensInstance.Telemetry { |
| 34 | + return analyticsPostResponse{ |
| 35 | + Status: "OK", |
| 36 | + }, nil |
| 37 | + } |
| 38 | + |
| 39 | + body, err := supertokens.ReadFromRequest(options.Req) |
| 40 | + |
| 41 | + if err != nil { |
| 42 | + return analyticsPostResponse{}, err |
| 43 | + } |
| 44 | + |
| 45 | + var readBody analyticsPostRequestBody |
| 46 | + err = json.Unmarshal(body, &readBody) |
| 47 | + if err != nil { |
| 48 | + return analyticsPostResponse{}, err |
| 49 | + } |
| 50 | + |
| 51 | + if readBody.Email == nil { |
| 52 | + return analyticsPostResponse{}, supertokens.BadInputError{ |
| 53 | + Msg: "Required parameter 'email' is missing", |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + if readBody.DashboardVersion == nil { |
| 58 | + return analyticsPostResponse{}, supertokens.BadInputError{ |
| 59 | + Msg: "Required parameter 'dashboardVersion' is missing", |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + data := map[string]interface{}{ |
| 64 | + "websiteDomain": supertokensInstance.AppInfo.WebsiteDomain.GetAsStringDangerous(), |
| 65 | + "apiDomain": supertokensInstance.AppInfo.APIDomain.GetAsStringDangerous(), |
| 66 | + "appName": supertokensInstance.AppInfo.AppName, |
| 67 | + "sdk": "golang", |
| 68 | + "sdkVersion": supertokens.VERSION, |
| 69 | + "email": *readBody.Email, |
| 70 | + "dashboardVersion": *readBody.DashboardVersion, |
| 71 | + } |
| 72 | + |
| 73 | + querier, err := supertokens.GetNewQuerierInstanceOrThrowError("") |
| 74 | + if err != nil { |
| 75 | + return analyticsPostResponse{}, err |
| 76 | + } |
| 77 | + |
| 78 | + response, err := querier.SendGetRequest("/telemetry", nil) |
| 79 | + if err != nil { |
| 80 | + // We don't send telemetry events if this fails |
| 81 | + return analyticsPostResponse{ |
| 82 | + Status: "OK", |
| 83 | + }, nil |
| 84 | + } |
| 85 | + |
| 86 | + exists := response["exists"].(bool) |
| 87 | + |
| 88 | + if exists { |
| 89 | + data["telemetryId"] = response["telemetryId"].(string) |
| 90 | + } |
| 91 | + |
| 92 | + numberOfUsers, err := supertokens.GetUserCount(nil) |
| 93 | + if err != nil { |
| 94 | + // We don't send telemetry events if this fails |
| 95 | + return analyticsPostResponse{ |
| 96 | + Status: "OK", |
| 97 | + }, nil |
| 98 | + } |
| 99 | + |
| 100 | + data["numberOfUsers"] = numberOfUsers |
| 101 | + |
| 102 | + jsonData, err := json.Marshal(data) |
| 103 | + if err != nil { |
| 104 | + return analyticsPostResponse{}, err |
| 105 | + } |
| 106 | + |
| 107 | + url := "https://api.supertokens.com/0/st/telemetry" |
| 108 | + req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData)) |
| 109 | + if err != nil { |
| 110 | + return analyticsPostResponse{ |
| 111 | + Status: "OK", |
| 112 | + }, nil |
| 113 | + } |
| 114 | + req.Header.Set("content-type", "application/json; charset=utf-8") |
| 115 | + req.Header.Set("api-version", "3") |
| 116 | + client := &http.Client{} |
| 117 | + client.Do(req) |
| 118 | + |
| 119 | + return analyticsPostResponse{ |
| 120 | + Status: "OK", |
| 121 | + }, nil |
| 122 | +} |
0 commit comments