-
Notifications
You must be signed in to change notification settings - Fork 0
/
function.go
44 lines (35 loc) · 867 Bytes
/
function.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
package function
import (
"encoding/json"
"io/ioutil"
"log"
"net/http"
"time"
)
// GetUserDetails - Get one user's details from randomuser.me API
func GetUserDetails(w http.ResponseWriter, r *http.Request) {
randomUserClient := http.Client{
Timeout: time.Second * 3,
}
req, err := http.NewRequest(http.MethodGet, "https://randomuser.me/api/", nil)
if err != nil {
log.Fatal(err)
return
}
res, err2 := randomUserClient.Do(req)
if err2 != nil {
log.Fatal(err2)
return
}
body, err3 := ioutil.ReadAll(res.Body)
if err3 != nil {
log.Fatal(err3)
}
var o map[string]interface{}
json.Unmarshal([]byte(body), &o)
results := o["results"].([]interface{})
result := results[0].(map[string]interface{})
result["generator"] = "google-cloud-function"
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(result)
}