-
Notifications
You must be signed in to change notification settings - Fork 7
/
suggest.go
48 lines (43 loc) · 1006 Bytes
/
suggest.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 infermedica
import (
"encoding/json"
"net/http"
"time"
"github.com/pkg/errors"
)
// SuggestReq is a struct to request suggestions
type SuggestReq struct {
Sex Sex `json:"sex"`
Age int `json:"age"`
Evidences []Evidence `json:"evidence"`
}
// SuggestRes is a response struct for suggest
type SuggestRes struct {
ID string `json:"id"`
Name string `json:"name"`
CommonName string `json:"common_name"`
}
// Suggest is a func to request suggestions
func (a *App) Suggest(sr SuggestReq) (*[]SuggestRes, error) {
if !sr.Sex.IsValid() {
return nil, errors.New("Unexpected value for Sex")
}
req, err := a.prepareRequest("POST", "suggest", sr)
if err != nil {
return nil, err
}
client := &http.Client{
Timeout: time.Second * 5,
}
res, err := client.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
r := []SuggestRes{}
err = json.NewDecoder(res.Body).Decode(&r)
if err != nil {
return nil, err
}
return &r, nil
}