-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
53 lines (41 loc) · 1.1 KB
/
api.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
package qrand
import (
"encoding/json"
"fmt"
"net/http"
)
// URL points to the Quantum Random Number Generator API
const URL = "https://qrng.anu.edu.au/API/jsonI.php"
// HTTPClient interface
type HTTPClient interface {
Do(req *http.Request) (*http.Response, error)
}
var (
Client HTTPClient
)
func init() {
Client = &http.Client{}
}
// Response describes the response from the qrng API
type Response struct {
DataType string `json:"type"`
Length int `json:"length"`
Size int `json:"size"`
Data []interface{} `json:"data"`
Success bool `json:"success"`
}
// Get makes a formatted GET request with the parameters supplied
func Get(length int, dataType string, size int) (jsonResponse Response, err error) {
URLWithParams := fmt.Sprintf("%s?length=%v&type=%v&size=%v", URL, length, dataType, size)
request, err := http.NewRequest(http.MethodGet, URLWithParams, nil)
if err != nil {
return
}
response, err := Client.Do(request)
if err != nil {
return
}
defer response.Body.Close()
err = json.NewDecoder(response.Body).Decode(&jsonResponse)
return
}