mirror of
https://github.com/thunderstorm99/addressWatcher.git
synced 2025-01-31 04:41:42 +00:00
introduced a function to check the API
This commit is contained in:
parent
8d9a5cddb5
commit
0a84c9751b
50
main.go
50
main.go
@ -88,25 +88,61 @@ func scheduler(tick *time.Ticker, c config) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func task(c config, previousAmount float64) float64 {
|
func task(c config, previousAmount float64) float64 {
|
||||||
amount, err := parseAmount(c)
|
b, err := checkAPI()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("can't parse amount, error is: %s\n", err)
|
log.Fatalln(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
previousAmount, err = compareAmount(previousAmount, amount, c)
|
if b == true {
|
||||||
if err != nil {
|
amount, err := parseAmount(c)
|
||||||
log.Fatalf("can't compare amount, error is: %s\n", err)
|
if err != nil {
|
||||||
|
log.Fatalf("can't parse amount, error is: %s\n", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
previousAmount, err = compareAmount(previousAmount, amount, c)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("can't compare amount, error is: %s\n", err)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
log.Println("API seems down. Skipping this round")
|
||||||
}
|
}
|
||||||
return previousAmount
|
return previousAmount
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func checkAPI() (bool, error) {
|
||||||
|
url := "https://chainz.cryptoid.info/explorer/api.dws?q=summary"
|
||||||
|
request, err := http.NewRequest(http.MethodGet, url, nil)
|
||||||
|
if err != nil {
|
||||||
|
return false, fmt.Errorf("can't request %s, error is: %s", url, err)
|
||||||
|
}
|
||||||
|
request.Header.Set("Content-Type", "application/json")
|
||||||
|
|
||||||
|
// request response
|
||||||
|
client := &http.Client{}
|
||||||
|
response, err := client.Do(request)
|
||||||
|
if err != nil {
|
||||||
|
return false, fmt.Errorf("HTTPS request to url %s failed with error %s", url, err)
|
||||||
|
}
|
||||||
|
// read response into data variable
|
||||||
|
data, err := ioutil.ReadAll(response.Body)
|
||||||
|
if err != nil {
|
||||||
|
return false, fmt.Errorf("can't read body %s, error is %s", response.Body, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(data) > 100 {
|
||||||
|
// API available
|
||||||
|
return true, nil
|
||||||
|
}
|
||||||
|
// API not available
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
|
|
||||||
func parseAmount(c config) (float64, error) {
|
func parseAmount(c config) (float64, error) {
|
||||||
// Define Variables
|
// Define Variables
|
||||||
var url string
|
|
||||||
var amount float64
|
var amount float64
|
||||||
|
|
||||||
// assemble url
|
// assemble url
|
||||||
url = "https://chainz.cryptoid.info/" + c.coin + "/api.dws?q=getbalance&a=" + c.address + "&key=" + c.apiKey
|
url := "https://chainz.cryptoid.info/" + c.coin + "/api.dws?q=getbalance&a=" + c.address + "&key=" + c.apiKey
|
||||||
|
|
||||||
// form url
|
// form url
|
||||||
request, err := http.NewRequest(http.MethodGet, url, nil)
|
request, err := http.NewRequest(http.MethodGet, url, nil)
|
||||||
|
Loading…
Reference in New Issue
Block a user