package main import ( "bytes" "encoding/json" "fmt" "io" "net/http" "os" "strings" "time" ) func required(name string) string { value := os.Getenv(name) if value == "" { panic("Missing required environment variable " + name) } return value } func env(name, fallback string) string { value := os.Getenv(name) if value == "" { return fallback } return value } func main() { identityURL := env("DECISIOQ_IDENTITY_URL", "https://identity.vinquery.com/connect/token") catalogURL := strings.TrimRight(env("DECISIOQ_DKS_URL", "https://dks.vinquery.com"), "/") decisionServiceURL := strings.TrimRight(env("DECISIOQ_DDE_URL", "https://dde.vinquery.com"), "/") correlationID := fmt.Sprintf("auto-auct-044-go-%d", time.Now().Unix()) tokenPayload, _ := json.Marshal(map[string]string{ "clientId": required("DECISIOQ_CLIENT_ID"), "clientSecret": required("DECISIOQ_CLIENT_SECRET"), "audience": env("DECISIOQ_AUDIENCE", "vinquery:api:decisioq"), }) tokenBody := do("POST", identityURL, tokenPayload, "", correlationID) var tokenResponse struct { JwtToken string `json:"jwtToken"` } if err := json.Unmarshal(tokenBody, &tokenResponse); err != nil { panic(err) } do("GET", catalogURL+"/decisioncatalog/decisions/AUTO-AUCT-044", nil, tokenResponse.JwtToken, correlationID) payload, err := os.ReadFile("auto-auct-044-execute.json") if err != nil { panic(err) } result := do("POST", decisionServiceURL+"/api/v1/decide", payload, tokenResponse.JwtToken, correlationID) fmt.Println(string(result)) } func do(method, url string, body []byte, token, correlationID string) []byte { var reader io.Reader if body != nil { reader = bytes.NewReader(body) } req, err := http.NewRequest(method, url, reader) if err != nil { panic(err) } req.Header.Set("Content-Type", "application/json") req.Header.Set("X-Correlation-Id", correlationID) if token != "" { req.Header.Set("Authorization", "Bearer "+token) } resp, err := http.DefaultClient.Do(req) if err != nil { panic(err) } defer resp.Body.Close() responseBody, _ := io.ReadAll(resp.Body) if resp.StatusCode >= 400 { panic(fmt.Sprintf("HTTP %d: %s", resp.StatusCode, string(responseBody))) } return responseBody }