Skip to content

Commit aec07be

Browse files
committed
feat: API request functions
1 parent a123b91 commit aec07be

File tree

1 file changed

+101
-0
lines changed

1 file changed

+101
-0
lines changed

request/api_request.go

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package request
2+
3+
import (
4+
"bytes"
5+
"encoding/json"
6+
"errors"
7+
"github.com/DanielFillol/DataJUD_API_CALLER/models"
8+
"io"
9+
"io/ioutil"
10+
"log"
11+
"net/http"
12+
"strconv"
13+
"time"
14+
)
15+
16+
// APIRequest makes an API request to the specified URL using the specified HTTP method and authentication header.
17+
// It returns a models.ResponseBody struct containing the API response body and an error (if any).
18+
func APIRequest(url, method string, auth string, request models.ReadCsv) (models.ResponseBody, error) {
19+
cnj, err := modifyCNJ(request.CNJNumber)
20+
if err != nil {
21+
return models.ResponseBody{}, err
22+
}
23+
24+
// Create a new BodyRequest struct with the document ID and pagination settings for the initial API call.
25+
req := models.BodyRequest{
26+
Query: models.Query{Match: models.Match{CNJNumber: cnj}},
27+
}
28+
29+
// Serialize the BodyRequest struct to JSON.
30+
jsonReq, err := json.Marshal(req)
31+
if err != nil {
32+
log.Println(err)
33+
return models.ResponseBody{}, err
34+
}
35+
36+
// Create a new buffer with the JSON-encoded request body.
37+
reqBody := bytes.NewBuffer(jsonReq)
38+
39+
// Make the API call and get the response.
40+
res, err := call(url, method, auth, reqBody)
41+
if err != nil {
42+
log.Println(err)
43+
return models.ResponseBody{}, errors.New(err.Error() + " " + req.Query.Match.CNJNumber)
44+
}
45+
46+
// Read the response body.
47+
body, err := ioutil.ReadAll(res.Body)
48+
if err != nil {
49+
log.Println(err)
50+
return models.ResponseBody{}, err
51+
}
52+
53+
// Unmarshal the response body into a ResponseBody struct.
54+
var response models.ResponseBody
55+
err = json.Unmarshal(body, &response)
56+
if err != nil {
57+
log.Println(err)
58+
return models.ResponseBody{}, err
59+
}
60+
61+
return models.ResponseBody{
62+
Took: response.Took,
63+
TimedOut: response.TimedOut,
64+
Shards: response.Shards,
65+
Hit: response.Hit,
66+
}, nil
67+
}
68+
69+
// call sends an HTTP request to the specified URL using the specified method and request body, with the specified authorization header.
70+
// It returns the HTTP response or an error if the request fails.
71+
func call(url, method string, AUTH string, body io.Reader) (*http.Response, error) {
72+
// Create an HTTP client with a 10-second timeout.
73+
client := &http.Client{Timeout: time.Second * 10}
74+
75+
// Create a new HTTP request with the specified method, URL, and request body.
76+
req, err := http.NewRequest(method, url, body)
77+
if err != nil {
78+
log.Println(err)
79+
return nil, err
80+
}
81+
82+
// Set the Content-Type and Authorization headers for the request.
83+
req.Header.Set("Content-Type", "application/json")
84+
req.Header.Set("Authorization", AUTH)
85+
86+
// Send the request and get the response.
87+
response, err := client.Do(req)
88+
if err != nil {
89+
log.Println(err)
90+
return nil, err
91+
}
92+
93+
// If the response status code is not OK, return an error with the status code.
94+
if response.StatusCode != http.StatusOK {
95+
log.Println(req.URL)
96+
log.Println(strconv.Itoa(response.StatusCode))
97+
return nil, errors.New(strconv.Itoa(response.StatusCode))
98+
}
99+
100+
return response, nil
101+
}

0 commit comments

Comments
 (0)