in connectors/grafana-plugin/pkg/plugin/iotdb_resource_handler.go [54:109]
func getVariables(authorization string, httpClient *http.Client) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
var url = r.FormValue("url")
var sql = r.FormValue("sql")
if r.Method != http.MethodGet {
http.NotFound(w, r)
return
}
var queryReq = &queryReq{Sql: sql}
qpJson, _ := json.Marshal(queryReq)
reader := bytes.NewReader(qpJson)
client := &http.Client{}
request, _ := http.NewRequest(http.MethodPost, url+"/grafana/v1/variable", reader)
request.Header.Set("Content-Type", "application/json")
request.Header.Add("Authorization", authorization)
rsp, _ := client.Do(request)
body, err := io.ReadAll(rsp.Body)
if err != nil {
log.DefaultLogger.Error("Data source is not working properly", err)
}
var dataResp []string
err = json.Unmarshal(body, &dataResp)
if err != nil {
log.DefaultLogger.Error("Parsing JSON error", err)
var resultResp queryResp
json.Unmarshal(body, &resultResp)
defer rsp.Body.Close()
j, err := json.Marshal(resultResp)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
_, err = w.Write(j)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
} else {
defer rsp.Body.Close()
j, err := json.Marshal(dataResp)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
_, err = w.Write(j)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
}
return http.HandlerFunc(fn)
}