forked from rashahacks/jsmon-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
getDomainStatus.go
101 lines (83 loc) · 2.57 KB
/
getDomainStatus.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package main
import (
"encoding/json"
"fmt"
"net/http"
"strings"
"io"
)
func getAllDomainsStatus(input string, size int){
endpoint := fmt.Sprintf("%s/getAllAutomationResults", apiBaseURL)
url := fmt.Sprintf("%s?showonly=%s&inputType=domain&input=%s&size=%d&start=%d&sortOrder=%s&sortBy=%s", endpoint, "domainStatus", input, size, 0, "desc", "createdAt")
// create request
req, err := http.NewRequest("GET", url, nil)
if err != nil {
fmt.Printf("Failed to create request")
return
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("X-Jsmon-Key", strings.TrimSpace(getAPIKey()))
// send 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 response
body, err := io.ReadAll(resp.Body)
if err != nil {
fmt.Printf("Failed to read response body: %v\n", err)
return
}
// parse response
var response map[string]interface{}
err = json.Unmarshal(body, &response)
if err != nil {
fmt.Printf("Failed to unmarshal JSON response: %v\n", err)
return
}
// fmt.Println(response, "HELLP")
// access urls map
results, err := extractResults(response)
if err != nil {
fmt.Println(err)
return
}
// fmt.Println("SOME OUTPUT FROM RESULTS", results)
domainStatus := processExtractedDomains(results)
// fmt.Println("domain Status", domainStatus)
for _, domain := range domainStatus {
fmt.Println(domain)
}
}
func extractResults(response map[string]interface{}) ([]interface{}, error) {
if results, ok := response["results"].([]interface{}); ok {
return results, nil
}
return nil, fmt.Errorf("no data found in response")
}
func processExtractedDomains(results []interface{}) []string {
output := make([]string, 0)
for _, result := range results {
if resultMap, ok := result.(map[string]interface{}); ok {
// Access 'extractedDomainsStatus'
if extractedDomainsStatus, ok := resultMap["extractedDomainsStatus"].([]interface{}); ok {
domainStatusResult := processDomainStatus(extractedDomainsStatus)
output = append(output, domainStatusResult...)
}
}
}
return output
}
func processDomainStatus(extractedDomainsStatus []interface{}) []string {
output := make([]string, 0)
for _, domainArray := range extractedDomainsStatus {
if domains, ok := domainArray.(map[string]interface{}); ok {
record := fmt.Sprintf("%s | %s | %s", domains["domainName"], domains["status"], domains["expiryDate"])
fmt.Println(record)
}
}
return output
}