Like what you see? Have a play with our trial version.

Error rendering macro 'rw-search'

null

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
titleGo
collapsetrue
package main

import (
    "bytes"
    "encoding/json"
    "fmt"
    "io/ioutil"
    "math/rand"
    "net/http"
    "time"
)

func main() {
	host := "http://localhost:8080/Yellowfin"
    restUsername := "admin@yellowfin.com.au"
    restPassword := "test"
    userToRetrieve := "admin@yellowfin.com.au"

    token, err := generateToken(host, restUsername, restPassword)
	if err != nil {
		fmt.Println("Error generating token:", err)
		return
	}

	fmt.Println("Retrieving User:", userToRetrieve)

	client := &http.Client{}
	req, err := http.NewRequest("GET", host+"/api/rpc/users/user-details-by-username/"+userToRetrieve, nil)
	if err != nil {
		fmt.Println("Error creating request:", err)
		return
	}

	nonce := rand.Int63()

	req.Header.Set("Authorization", fmt.Sprintf("YELLOWFIN ts=%d, nonce=%d, token=%s", time.Now().UnixMilli(), nonce, token))
	req.Header.Set("Accept", "application/vnd.yellowfin.api-v1+json")
	req.Header.Set("Content-Type", "application/json")

	resp, err := client.Do(req)
	if err != nil {
		fmt.Println("Error sending request:", err)
		return
	}
	defer resp.Body.Close()

	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		fmt.Println("Error reading response body:", err)
		return
	}

	fmt.Println(string(body))
}

func generateToken(host, restUsername, restPassword string) (string, error) {
	// Generate nonce
	nonce := rand.Int63()

	// Create request body
	requestBody, err := json.Marshal(map[string]string{
        "userName": restUsername,
        "password": restPassword,
	})
	if err != nil {
		fmt.Println("Error marshaling request body:", err)
		return "", err
	}

	// Create HTTP client
	client := &http.Client{}

	// Create HTTP request
	request, err := http.NewRequest("POST", host+"/api/refresh-tokens", bytes.NewBuffer(requestBody))
	if err != nil {
		fmt.Println("Error creating request:", err)
		return "", err
	}

	// Add request headers
	request.Header.Set("Authorization", fmt.Sprintf("YELLOWFIN ts=%d, nonce=%d", time.Now().UnixMilli(), nonce))
	request.Header.Set("Accept", "application/vnd.yellowfin.api-v1+json")
	request.Header.Set("Content-Type", "application/json")

	// Send HTTP request
	response, err := client.Do(request)
	if err != nil {
		fmt.Println("Error sending request:", err)
		return "", err
	}
	defer response.Body.Close()

	// Read response body
	responseBody, err := ioutil.ReadAll(response.Body)
	if err != nil {
		fmt.Println("Error reading response body:", err)
		return "", err
	}

	// Parse JSON response
	var jsonResponse map[string]interface{}
	err = json.Unmarshal(responseBody, &jsonResponse)
	if err != nil {
		fmt.Println("Error parsing JSON response:", err)
		return "", err
	}

	// Get access token from response
	accessToken, ok := jsonResponse["_embedded"].(map[string]interface{})["accessToken"].(map[string]interface{})["securityToken"].(string)
	if !ok {
		fmt.Println("Token not retrieved")
		return "", fmt.Errorf("Token not retrieved successfully")
	}

	return accessToken, nil
}

...