-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanu_api.go
More file actions
40 lines (34 loc) · 908 Bytes
/
Copy pathanu_api.go
File metadata and controls
40 lines (34 loc) · 908 Bytes
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
package pkg
import (
"encoding/json"
"fmt"
"net/http"
)
const apibase = "https://qrng.anu.edu.au/API/jsonI.php"
type ApiResponse struct {
Length int `json:"length"`
Data []uint `json:"data"`
}
// queryDefaultSize will request ten numbers from the API.
func queryDefaultSize(dt datatype) ([]uint, error) {
return queryApi(dt, 10)
}
// querySingleValue will just request one number from the API.
func querySingleValue(dt datatype) (uint, error) {
resp, err := queryApi(dt, 1)
if err != nil {
return 0, err
}
return resp[0], nil
}
// queryApi performs the actual request against the anu.edu servers.
func queryApi(dt datatype, size int) ([]uint, error) {
query := fmt.Sprintf("%v?length=%d&type=%v", apibase, size, dt)
response, err := http.Get(query)
if err != nil {
return nil, err
}
resp := new(ApiResponse)
json.NewDecoder(response.Body).Decode(resp)
return resp.Data, nil
}