-
Notifications
You must be signed in to change notification settings - Fork 0
/
chessdotcom.go
48 lines (44 loc) · 1.2 KB
/
chessdotcom.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
package chessdotcomgo
import (
"encoding/json"
"io/ioutil"
"log"
"net/http"
"github.com/ATTron/chessdotcom-go/util"
)
func processRespJSON(resp *http.Response) (string, error) {
switch resp.StatusCode {
case 200:
b, _ := ioutil.ReadAll(resp.Body)
var jsonResp map[string]interface{}
err := json.Unmarshal(b, &jsonResp)
util.Check(err)
returnResp, err := json.MarshalIndent(&jsonResp, "", " ")
util.Check(err)
return string(returnResp), nil
case 404:
log.Println("Unable to find the request you are looking for")
return "", util.ErrNotFound
default:
log.Fatal("Could not return valid response from server . . .")
return "", util.ErrBadType
}
}
func processRespArray(resp *http.Response) (string, error) {
switch resp.StatusCode {
case 200:
b, _ := ioutil.ReadAll(resp.Body)
var jsonResp []interface{}
err := json.Unmarshal(b, &jsonResp)
util.Check(err)
returnResp, err := json.MarshalIndent(&jsonResp, "", " ")
util.Check(err)
return string(returnResp), nil
case 404:
log.Println("Unable to find the request you are looking for")
return "", util.ErrNotFound
default:
log.Fatal("Could not return valid response from server . . .")
return "", util.ErrBadType
}
}