utils.go_dis 612 B

12345678910111213141516171819202122232425262728293031323334353637
  1. package homekit
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "io/ioutil"
  6. "net/http"
  7. "strconv"
  8. "time"
  9. )
  10. func isJSON(s string) bool {
  11. var js map[string]interface{}
  12. return json.Unmarshal([]byte(s), &js) == nil
  13. }
  14. func tryGet(url string) (string, error) {
  15. client := http.Client{
  16. Timeout: 10 * time.Second,
  17. }
  18. resp, err := client.Get(url)
  19. if err != nil {
  20. return "", err
  21. }
  22. if resp.StatusCode != 200 {
  23. return "", errors.New("Server side return status code " + strconv.Itoa(resp.StatusCode))
  24. }
  25. content, err := ioutil.ReadAll(resp.Body)
  26. if err != nil {
  27. return "", err
  28. }
  29. return string(content), nil
  30. }