-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi_request_codeSearch.go
156 lines (132 loc) · 4.66 KB
/
api_request_codeSearch.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
package request
import (
"bytes"
"encoding/json"
"errors"
"github.com/DanielFillol/DataJUD_API_CALLER/models"
"io/ioutil"
"log"
"strconv"
)
const SIZE = 100
// APIRequestCode makes an API request to the specified URL using the specified HTTP method and authentication header.
// It returns a models.ResponseBodyNextPage struct containing the API response body and an error (if any).
func APIRequestCode(url, method string, auth string, request models.ReadCsvCode) (models.ResponseBodyNextPage, error) {
// Create responseReturn new BodyRequestLawsuit struct with the document ID and pagination settings for the initial API call.
class, err := strconv.Atoi(request.ClassCode)
if err != nil {
return models.ResponseBodyNextPage{}, errors.New("failed to parse the class code to int: " + request.ClassCode)
}
req := models.BodyRequestCode{
Size: SIZE,
Query: models.Query{
Bool: models.Bool{
Must: []models.Must{
{Match: models.Match{ClasseCodigo: class}},
},
},
},
Sort: []models.Sort{{models.Timestamp{Order: "asc"}}},
}
// Serialize the BodyRequestLawsuit struct to JSON.
jsonReq, err := json.Marshal(req)
if err != nil {
return models.ResponseBodyNextPage{}, errors.New(err.Error())
}
// Create responseReturn new buffer with the JSON-encoded request body.
reqBody := bytes.NewBuffer(jsonReq)
// Make the API call and get the response.
res, err := call(url, method, auth, reqBody)
if err != nil {
return models.ResponseBodyNextPage{}, errors.New(err.Error())
}
// Read the response body.
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return models.ResponseBodyNextPage{}, errors.New(err.Error())
}
// Unmarshal the response body into responseReturn ResponseBodyLawsuit struct.
var response models.ResponseBodyNextPage
err = json.Unmarshal(body, &response)
if err != nil {
return models.ResponseBodyNextPage{}, errors.New(err.Error())
}
responseReturn := models.ResponseBodyNextPage{
Took: response.Took,
TimedOut: response.TimedOut,
Shards: response.Shards,
Hit: response.Hit,
}
// If the API response has more pages of data, make additional API calls and append the results to the response.
if len(response.Hit.Hits) == SIZE {
nextPage := strconv.Itoa(int(response.Hit.Hits[SIZE-1].Sort[0]))
// Make the API call and get the response.
lawsuits, err := callNextPage(url, method, auth, req, nextPage)
if err != nil {
return models.ResponseBodyNextPage{}, errors.New(err.Error())
}
responseReturn.Hit.Hits = append(responseReturn.Hit.Hits, lawsuits...)
return responseReturn, nil
}
return responseReturn, nil
}
// callNextPage returns a slice of models.Lawsuit structs containing the data from all pages of the API response.
func callNextPage(url string, method string, auth string, req models.BodyRequestCode, cursor string) ([]models.Hit2NextPage, error) {
var lawsuits []models.Hit2NextPage
c, err := strconv.Atoi(cursor)
if err != nil {
return lawsuits, errors.New("could not covert cursor into int: " + cursor)
}
for {
log.Println("success getting next page:" + strconv.Itoa(c))
// Create a new BodyRequest struct with the document ID and updated pagination settings for the next API call.
request := models.BodyRequestCodeNextPage{
Size: SIZE,
Query: models.Query{
Bool: models.Bool{
Must: []models.Must{
{Match: models.Match{ClasseCodigo: req.Query.Bool.Must[0].Match.ClasseCodigo}},
},
},
},
Sort: []models.Sort{{models.Timestamp{Order: "asc"}}},
SearchAfter: []int64{int64(c)},
}
// Serialize the BodyRequest struct to JSON.
jsonReq, err := json.Marshal(request)
if err != nil {
log.Println(err)
return lawsuits, err
}
// Create a new buffer with the JSON-encoded request body.
reqBody := bytes.NewBuffer(jsonReq)
// Call the API using the provided url, method, authorization, and request body.
res, err := call(url, method, auth, reqBody)
if err != nil {
log.Println(err)
return lawsuits, errors.New(err.Error())
}
// Read the response body.
body, err := ioutil.ReadAll(res.Body)
if err != nil {
log.Println(err)
return lawsuits, err
}
// Unmarshal the response body into a models.ResponseBody struct.
var response models.ResponseBodyNextPage
err = json.Unmarshal(body, &response)
if err != nil {
log.Println(err)
return lawsuits, err
}
// Append the current response to the lawsuits slice.
lawsuits = append(lawsuits, response.Hit.Hits...)
// If the API response indicates there are no more pages, break out of the loop.
if len(response.Hit.Hits) < SIZE {
break
}
// Update the cursor for the next API call.
c = int(response.Hit.Hits[SIZE-1].Sort[0])
}
return lawsuits, nil
}