-
Notifications
You must be signed in to change notification settings - Fork 7
/
infermedica.go
186 lines (161 loc) · 3.99 KB
/
infermedica.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package infermedica
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"strings"
"github.com/pkg/errors"
)
type App struct {
baseURL string
appID string
appKey string
model string
interviewID string
}
func NewApp(id, key, model, interviewID string) App {
return App{
baseURL: "https://api.infermedica.com/v2/",
appID: id,
appKey: key,
model: model,
interviewID: interviewID,
}
}
func (a App) prepareRequest(method, url string, body interface{}) (*http.Request, error) {
switch method {
case "GET":
return a.prepareGETRequest(url)
case "POST":
return a.preparePOSTRequest(url, body)
}
return nil, errors.New("Method not allowed")
}
func (a App) addHeaders(req *http.Request) {
req.Header.Add("App-Id", a.appID)
req.Header.Add("App-Key", a.appKey)
req.Header.Add("Content-Type", "application/json")
if a.model != "" {
req.Header.Add("Model", a.model)
}
if a.interviewID != "" {
req.Header.Add("Interview-Id", a.interviewID)
}
}
func (a App) prepareGETRequest(url string) (*http.Request, error) {
baseURL := a.baseURL
if strings.Index(url, "covid19") != -1 {
baseURL = strings.ReplaceAll(baseURL, "v2/", "")
}
req, err := http.NewRequest("GET", baseURL+url, nil)
if err != nil {
return nil, err
}
a.addHeaders(req)
return req, nil
}
func (a App) preparePOSTRequest(url string, body interface{}) (*http.Request, error) {
b := new(bytes.Buffer)
err := json.NewEncoder(b).Encode(body)
if err != nil {
return nil, err
}
baseURL := a.baseURL
if strings.Index(url, "covid19") != -1 {
baseURL = strings.ReplaceAll(baseURL, "v2/", "")
}
req, err := http.NewRequest("POST", baseURL+url, b)
if err != nil {
return nil, err
}
a.addHeaders(req)
return req, nil
}
type Sex string
const (
SexMale Sex = "male"
SexFemale Sex = "female"
)
func (s Sex) Ptr() *Sex { return &s }
func (s Sex) String() string { return string(s) }
func (s *Sex) IsValid() bool {
_, err := SexFromString(s.String())
if err != nil {
return false
}
return true
}
func SexFromString(x string) (Sex, error) {
switch strings.ToLower(x) {
case "male":
return SexMale, nil
case "female":
return SexFemale, nil
default:
return "", fmt.Errorf("Unexpected value for Sex: %q", x)
}
}
type SexFilter string
const (
SexFilterBoth SexFilter = "both"
SexFilterMale SexFilter = "male"
SexFilterFemale SexFilter = "female"
)
func (s SexFilter) Ptr() *SexFilter { return &s }
func (s SexFilter) String() string { return string(s) }
func (s *SexFilter) IsValid() bool {
_, err := SexFilterFromString(s.String())
if err != nil {
return false
}
return true
}
func SexFilterFromString(x string) (SexFilter, error) {
switch strings.ToLower(x) {
case "both":
return SexFilterBoth, nil
case "male":
return SexFilterMale, nil
case "female":
return SexFilterFemale, nil
default:
return "", fmt.Errorf("Unexpected value for SexFilter: %q", x)
}
}
type EvidenceChoiceID string
const (
EvidenceChoiceIDPresent EvidenceChoiceID = "present"
EvidenceChoiceIDAbsent EvidenceChoiceID = "absent"
EvidenceChoiceIDUnknown EvidenceChoiceID = "unknown"
)
func (ecID EvidenceChoiceID) Ptr() *EvidenceChoiceID { return &ecID }
func (ecID EvidenceChoiceID) String() string { return string(ecID) }
func (ecID EvidenceChoiceID) IsValid() bool {
_, err := EvidenceChoiceIDFromString(ecID.String())
if err != nil {
return false
}
return true
}
func EvidenceChoiceIDFromString(x string) (EvidenceChoiceID, error) {
switch strings.ToLower(x) {
case "present":
return EvidenceChoiceIDPresent, nil
case "absent":
return EvidenceChoiceIDAbsent, nil
case "unknown":
return EvidenceChoiceIDUnknown, nil
default:
return "", fmt.Errorf("Unexpected value for evidence choice id: %q", x)
}
}
type Evidence struct {
ID string `json:"id"`
ChoiceID EvidenceChoiceID `json:"choice_id"`
Initial bool `json:"initial"`
}
type EvidenceCovid struct {
ID string `json:"id"`
ChoiceID EvidenceChoiceID `json:"choice_id"`
}