|
| 1 | +package request |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "encoding/json" |
| 6 | + "errors" |
| 7 | + "github.com/DanielFillol/DataJUD_API_CALLER/models" |
| 8 | + "io/ioutil" |
| 9 | + "log" |
| 10 | + "strconv" |
| 11 | +) |
| 12 | + |
| 13 | +// APIRequestCode makes an API request to the specified URL using the specified HTTP method and authentication header. |
| 14 | +// It returns a models.ResponseBodyNextPage struct containing the API response body and an error (if any). |
| 15 | +func APIRequestCode(url, method string, auth string, request models.ReadCsvCode) (models.ResponseBodyNextPage, error) { |
| 16 | + // Create responseReturn new BodyRequestLawsuit struct with the document ID and pagination settings for the initial API call. |
| 17 | + class, err := strconv.Atoi(request.ClassCode) |
| 18 | + if err != nil { |
| 19 | + return models.ResponseBodyNextPage{}, errors.New("failed to parse the class code to int: " + request.ClassCode) |
| 20 | + } |
| 21 | + court, err := strconv.Atoi(request.CourtCode) |
| 22 | + if err != nil { |
| 23 | + return models.ResponseBodyNextPage{}, errors.New("failed to parse the court code to int: " + request.CourtCode) |
| 24 | + } |
| 25 | + |
| 26 | + req := models.BodyRequestCode{ |
| 27 | + Size: 100, |
| 28 | + Query: models.Query{ |
| 29 | + Bool: models.Bool{ |
| 30 | + Must: []models.Must{ |
| 31 | + {Match: models.Match{ClasseCodigo: class}}, |
| 32 | + {Match: models.Match{OrgaoJulgadorCodigo: court}}, |
| 33 | + }, |
| 34 | + }, |
| 35 | + }, |
| 36 | + Sort: []models.Sort{{models.Timestamp{Order: "asc"}}}, |
| 37 | + } |
| 38 | + |
| 39 | + // Serialize the BodyRequestLawsuit struct to JSON. |
| 40 | + jsonReq, err := json.Marshal(req) |
| 41 | + if err != nil { |
| 42 | + return models.ResponseBodyNextPage{}, errors.New(err.Error()) |
| 43 | + } |
| 44 | + |
| 45 | + // Create responseReturn new buffer with the JSON-encoded request body. |
| 46 | + reqBody := bytes.NewBuffer(jsonReq) |
| 47 | + |
| 48 | + // Make the API call and get the response. |
| 49 | + res, err := call(url, method, auth, reqBody) |
| 50 | + if err != nil { |
| 51 | + return models.ResponseBodyNextPage{}, errors.New(err.Error()) |
| 52 | + } |
| 53 | + |
| 54 | + // Read the response body. |
| 55 | + body, err := ioutil.ReadAll(res.Body) |
| 56 | + if err != nil { |
| 57 | + return models.ResponseBodyNextPage{}, errors.New(err.Error()) |
| 58 | + } |
| 59 | + |
| 60 | + // Unmarshal the response body into responseReturn ResponseBodyLawsuit struct. |
| 61 | + var response models.ResponseBodyNextPage |
| 62 | + err = json.Unmarshal(body, &response) |
| 63 | + if err != nil { |
| 64 | + return models.ResponseBodyNextPage{}, errors.New(err.Error()) |
| 65 | + } |
| 66 | + |
| 67 | + responseReturn := models.ResponseBodyNextPage{ |
| 68 | + Took: response.Took, |
| 69 | + TimedOut: response.TimedOut, |
| 70 | + Shards: response.Shards, |
| 71 | + Hit: response.Hit, |
| 72 | + } |
| 73 | + |
| 74 | + // If the API response has more pages of data, make additional API calls and append the results to the response. |
| 75 | + if len(response.Hit.Hits) == 100 { |
| 76 | + nextPage := strconv.Itoa(int(response.Hit.Hits[99].Sort[0])) |
| 77 | + // Make the API call and get the response. |
| 78 | + lawsuits, err := callNextPage(url, method, auth, req, nextPage) |
| 79 | + if err != nil { |
| 80 | + return models.ResponseBodyNextPage{}, errors.New(err.Error()) |
| 81 | + } |
| 82 | + |
| 83 | + responseReturn.Hit.Hits = append(responseReturn.Hit.Hits, lawsuits...) |
| 84 | + return responseReturn, nil |
| 85 | + } |
| 86 | + |
| 87 | + return responseReturn, nil |
| 88 | +} |
| 89 | + |
| 90 | +// callNextPage returns a slice of models.Lawsuit structs containing the data from all pages of the API response. |
| 91 | +func callNextPage(url string, method string, auth string, req models.BodyRequestCode, cursor string) ([]models.Hit2NextPage, error) { |
| 92 | + var lawsuits []models.Hit2NextPage |
| 93 | + c, err := strconv.Atoi(cursor) |
| 94 | + if err != nil { |
| 95 | + return lawsuits, errors.New("could not covert cursor into int: " + cursor) |
| 96 | + } |
| 97 | + |
| 98 | + for { |
| 99 | + log.Println("success getting next page:" + strconv.Itoa(c)) |
| 100 | + // Create a new BodyRequest struct with the document ID and updated pagination settings for the next API call. |
| 101 | + request := models.BodyRequestCodeNextPage{ |
| 102 | + Size: 100, |
| 103 | + Query: models.Query{ |
| 104 | + Bool: models.Bool{ |
| 105 | + Must: []models.Must{ |
| 106 | + {Match: models.Match{ClasseCodigo: req.Query.Bool.Must[0].Match.ClasseCodigo}}, |
| 107 | + {Match: models.Match{OrgaoJulgadorCodigo: req.Query.Bool.Must[1].Match.OrgaoJulgadorCodigo}}, |
| 108 | + }, |
| 109 | + }, |
| 110 | + }, |
| 111 | + Sort: []models.Sort{{models.Timestamp{Order: "asc"}}}, |
| 112 | + SearchAfter: []int64{int64(c)}, |
| 113 | + } |
| 114 | + |
| 115 | + // Serialize the BodyRequest struct to JSON. |
| 116 | + jsonReq, err := json.Marshal(request) |
| 117 | + if err != nil { |
| 118 | + log.Println(err) |
| 119 | + return lawsuits, err |
| 120 | + } |
| 121 | + |
| 122 | + // Create a new buffer with the JSON-encoded request body. |
| 123 | + reqBody := bytes.NewBuffer(jsonReq) |
| 124 | + |
| 125 | + // Call the API using the provided url, method, authorization, and request body. |
| 126 | + res, err := call(url, method, auth, reqBody) |
| 127 | + if err != nil { |
| 128 | + log.Println(err) |
| 129 | + return lawsuits, errors.New(err.Error()) |
| 130 | + } |
| 131 | + |
| 132 | + // Read the response body. |
| 133 | + body, err := ioutil.ReadAll(res.Body) |
| 134 | + if err != nil { |
| 135 | + log.Println(err) |
| 136 | + return lawsuits, err |
| 137 | + } |
| 138 | + |
| 139 | + // Unmarshal the response body into a models.ResponseBody struct. |
| 140 | + var response models.ResponseBodyNextPage |
| 141 | + err = json.Unmarshal(body, &response) |
| 142 | + if err != nil { |
| 143 | + log.Println(err) |
| 144 | + return lawsuits, err |
| 145 | + } |
| 146 | + |
| 147 | + // Append the current response to the lawsuits slice. |
| 148 | + lawsuits = append(lawsuits, response.Hit.Hits...) |
| 149 | + |
| 150 | + // If the API response indicates there are no more pages, break out of the loop. |
| 151 | + if len(response.Hit.Hits) < 100 { |
| 152 | + break |
| 153 | + } |
| 154 | + |
| 155 | + // Update the cursor for the next API call. |
| 156 | + c = int(response.Hit.Hits[99].Sort[0]) |
| 157 | + } |
| 158 | + |
| 159 | + return lawsuits, nil |
| 160 | +} |
0 commit comments