forked from rashahacks/jsmon-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetResultsByJsmonId.go
82 lines (72 loc) · 2.61 KB
/
getResultsByJsmonId.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package main
import (
"fmt"
"io/ioutil"
"net/http"
"strings"
"encoding/json"
)
// Function to fetch automation results for a given jsmonId
func getAutomationResultsByJsmonId(jsmonId string) {
// Define the API base URL and endpoint, appending the jsmonId as a query parameter
endpoint := fmt.Sprintf("%s/getAllAutomationResults?inputType=jsmonid&input=%s&showonly=all", apiBaseURL, jsmonId)
// Create a new HTTP request with the GET method
req, err := http.NewRequest("GET", endpoint, nil) // No need for request body in GET
if err != nil {
fmt.Printf("Failed to create request: %v\n", err)
return
}
// Set necessary headers
req.Header.Set("Content-Type", "application/json")
req.Header.Set("X-Jsmon-Key", strings.TrimSpace(getAPIKey())) // Trim any whitespace from the API key
// Create an HTTP client and make the request
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
fmt.Printf("Failed to send request: %v\n", err)
return
}
defer resp.Body.Close()
// Read the response body
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Printf("Failed to read response: %v\n", err)
return
}
// Check if the response is successful (Status Code: 2xx)
if resp.StatusCode >= 200 && resp.StatusCode < 300 {
// Print the response with all fields related to the jsmonId
var result interface{}
err = json.Unmarshal(body, &result)
if err != nil {
fmt.Println("Error parsing JSON:", err)
return
}
// Assert that result is of type map[string]interface{}
if resMap, ok := result.(map[string]interface{}); ok {
// Access the "results" key
if results, ok := resMap["results"].([]interface{}); ok {
if len(results) > 0 {
// Access the first element
firstElement := results[0]
// Print the first element as a pretty JSON string
prettyJSON, err := json.MarshalIndent(firstElement, "", " ")
if err != nil {
fmt.Println("Error formatting JSON:", err)
return
}
fmt.Println(string(prettyJSON))
} else {
fmt.Println("Results array is empty.")
}
} else {
fmt.Println("results is not of type []interface{}")
}
} else {
fmt.Println("result is not of type map[string]interface{}")
}
} else {
fmt.Printf("Error: Received status code %d\n", resp.StatusCode)
fmt.Println("Response:", string(body)) // Print the response even if it's an error
}
}