Page History
...
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
package rest.code.examples; import java.io.IOException; import java.util.Random; import org.apache.hc.client5.http.fluent.Content; import org.apache.hc.client5.http.fluent.Request; import com.google.gson.JsonArray; import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonParser; /** * Create a user using the Yellowfin REST API */ public class ListUserGroups { public static void main(String[] args) throws Exception { System.out.println("List Groups"); String host = "http://localhost:8080/Yellowfin"; String restUsername = "admin@yellowfin.com.au"; String restPassword = "test"; String token = generateToken(host, restUsername, restPassword); Content c = Request.get(host + "/api/user-groups") .addHeader("Authorization", "YELLOWFIN ts=" + System.currentTimeMillis() + " , nonce=" + new Random().nextLong() + ", token=" + token) .addHeader("Accept", "application/vnd.yellowfin.api-v1+json") .addHeader("Content-Type", "application/json") .execute().returnContent(); JsonObject jsonObject = new JsonParser().parse(c.asString()).getAsJsonObject(); JsonElement groupList = jsonObject.get("items"); JsonArray groups = groupList.getAsJsonArray(); System.out.println(groups.size() + " groups retrieved."); for (int i=0; i < groups.size(); i++ ) { JsonObject group = groups.getAsJsonArray().get(i).getAsJsonObject(); System.out.println("Group " + group.get("userGroupId").getAsInt() + ": " + group.get("shortDescription").getAsString()); } } /* * This function generates an access token for a user that will grant them access to * call REST API endpoints. */ public static String generateToken(String host, String username, String password) throws IOException { Content c = Request.post(host + "/api/refresh-tokens") .addHeader("Authorization", "YELLOWFIN ts=" + System.currentTimeMillis() + " , nonce=" + new Random().nextLong()) .addHeader("Accept", "application/vnd.yellowfin.api-v1+json") .addHeader("Content-Type", "application/json") .bodyString("{ \"userName\": \""+ username + "\",\"password\": \""+ password + "\"}", null) .execute().returnContent(); JsonObject jsonObject = new JsonParser().parse(c.asString()).getAsJsonObject(); JsonElement accessToken = jsonObject.getAsJsonObject("_embedded").getAsJsonObject("accessToken").get("securityToken"); if (accessToken!=null) { System.out.println("Access Token: " + accessToken); } else { System.out.println("Token not retrieved successfully"); System.exit(-1); } return accessToken.getAsString(); } } |
...
Code Block | ||||
---|---|---|---|---|
| ||||
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" token, err := generateToken(host, restUsername, restPassword) if err != nil { fmt.Println("Error generating token:", err) return } fmt.Println("List Groups") client := &http.Client{} req, err := http.NewRequest("GET", host+"/api/user-groups", 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 } var jsonResponse map[string]interface{} err = json.Unmarshal(body, &jsonResponse) if err != nil { fmt.Println("Error parsing JSON response:", err) return } groupList := jsonResponse["items"].([]interface{}) groups := make([]map[string]interface{}, len(groupList)) for i, v := range groupList { groups[i] = v.(map[string]interface{}) } fmt.Printf("%d groups retrieved.\n", len(groups)) for _, group := range groups { fmt.Printf("Group %v: %v\n", group["userGroupId"], group["shortDescription"]) } } func generateToken(host, username, password string) (string, error) { nonce := rand.Int63() requestBody, err := json.Marshal(map[string]string{ "userName": username, "password": password, }) if err != nil { return "", err } client := &http.Client{} req, err := http.NewRequest("POST", host+"/api/refresh-tokens", bytes.NewBuffer(requestBody)) if err != nil { return "", err } req.Header.Set("Authorization", fmt.Sprintf("YELLOWFIN ts=%d, nonce=%d", time.Now().UnixMilli(), nonce)) 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 { return "", err } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) if err != nil { return "", err } var jsonResponse map[string]interface{} err = json.Unmarshal(body, &jsonResponse) if err != nil { return "", err } accessToken, ok := jsonResponse["_embedded"].(map[string]interface{})["accessToken"].(map[string]interface{})["securityToken"].(string) if !ok { return "", fmt.Errorf("Token not retrieved successfully") } return accessToken, nil } |
...