-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch.go
210 lines (174 loc) · 4.23 KB
/
search.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package gopddikti
import (
"fmt"
"net/http"
"net/url"
"sync"
)
const (
searchAllPath = "/hit"
searchAllPathStudent = "/hit_mhs"
)
type SearchAllResponse struct {
Students []Student
Lecturers []Lecturer
Colleges []College
Programmes []Programme
}
type Student struct {
Name string
ID string
College string
Programme string
DetailID string
}
type Lecturer struct {
Name string
ID string
College string
Programme string
DetailID string
}
type College struct {
Name string
Nickname string
ID string
Address string
DetailID string
}
type Programme struct {
Name string
Level string
College string
DetailID string
}
type RawStudentsResponse struct {
Students []SearchResponse `json:"mahasiswa"`
}
type RawAllResponse struct {
Lecturers []SearchResponse `json:"dosen"`
Colleges []SearchResponse `json:"pt"`
Programmes []SearchResponse `json:"prodi"`
}
type SearchResponse struct {
Text string `json:"text"`
WebsiteLink string `json:"website-link"`
}
// Search is used to search entities list based on given param.
// It will returns all four entities in the type of array.
func (c *Client) Search(param string) (res SearchAllResponse, err error) {
if err = checkParamString(param); err != nil {
return
}
var (
errStack []error
mu sync.Mutex
wg sync.WaitGroup
resRawStudent RawStudentsResponse
resRawAll RawAllResponse
)
wg.Add(2)
go func() {
defer wg.Done()
reqStudent, err := c.createRequest(http.MethodGet, searchAllPathStudent+"/"+url.QueryEscape(param))
if err != nil {
appendError(&errStack, err, &mu)
} else {
err = c.doRequest(reqStudent, &resRawStudent)
if err != nil {
appendError(&errStack, err, &mu)
}
}
}()
go func() {
defer wg.Done()
resRawAll, err = c.searchAll(param)
if err != nil {
appendError(&errStack, err, &mu)
}
}()
wg.Wait()
if len(errStack) > 0 {
combinedErr := errStack[0]
for _, v := range errStack[1:] {
combinedErr = fmt.Errorf("%v; %w", combinedErr, v)
}
err = combinedErr
return
}
wg.Add(4)
go func() {
defer wg.Done()
res.Students = parseStudentResponse(resRawStudent.Students)
}()
go func() {
defer wg.Done()
res.Lecturers = parseLecturerResponse(resRawAll.Lecturers)
}()
go func() {
defer wg.Done()
res.Colleges = parseCollegeResponse(resRawAll.Colleges)
}()
go func() {
defer wg.Done()
res.Programmes = parseProgrammeResponse(resRawAll.Programmes)
}()
wg.Wait()
return
}
// SearchStudents is used to search students based on given param.
// It will returns all the list of students.
func (c *Client) SearchStudents(param string) (res []Student, err error) {
raw, err := c.searchStudents(param)
res = parseStudentResponse(raw.Students)
return
}
// SearchLecturers is used to search lecturers based on given param.
// It will returns all the list of lecturers.
func (c *Client) SearchLecturers(param string) (res []Lecturer, err error) {
resp, err := c.searchAll(param)
res = parseLecturerResponse(resp.Lecturers)
return
}
// SearchColleges is used to search colleges based on given param.
// It will returns all the list of colleges.
func (c *Client) SearchColleges(param string) (res []College, err error) {
resp, err := c.searchAll(param)
res = parseCollegeResponse(resp.Colleges)
return
}
// SearchProgrammes is used to search programmes based on given param.
// It will returns all the list of programmes.
func (c *Client) SearchProgrammes(param string) (res []Programme, err error) {
resp, err := c.searchAll(param)
res = parseProgrammeResponse(resp.Programmes)
return
}
func (c *Client) searchStudents(param string) (res RawStudentsResponse, err error) {
if err = checkParamString(param); err != nil {
return
}
req, err := c.createRequest(http.MethodGet, searchAllPathStudent+"/"+url.QueryEscape(param))
if err != nil {
return
}
err = c.doRequest(req, &res)
if err != nil {
return
}
return
}
func (c *Client) searchAll(param string) (res RawAllResponse, err error) {
if err = checkParamString(param); err != nil {
return
}
req, err := c.createRequest(http.MethodGet, searchAllPath+"/"+url.QueryEscape(param))
if err != nil {
return
}
err = c.doRequest(req, &res)
if err != nil {
return
}
return
}