-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi_async_request.go
86 lines (75 loc) · 2.52 KB
/
api_async_request.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
package request
import (
"github.com/DanielFillol/DataJUD_API_CALLER/models"
"log"
"sync"
)
const API = "/_search"
// AsyncAPIRequestLawsuit makes API requests asynchronously
func AsyncAPIRequestLawsuit(users []models.ReadCsvLaawsuit, numberOfWorkers int, url string, method string, auth string) ([]models.ResponseBodyLawsuit, error) {
// Create a channel to signal when the goroutines are done processing inputs
done := make(chan struct{})
defer close(done)
// Create a channel to receive inputs from
inputCh := StreamInputsLawsuits(done, users)
// Create a wait group to wait for the worker goroutines to finish
var wg sync.WaitGroup
wg.Add(numberOfWorkers)
// Create a channel to receive results from
resultCh := make(chan models.ResponseBodyLawsuit)
// Spawn worker goroutines to process inputs
var errorOnApiRequests error
for i := 0; i < numberOfWorkers; i++ {
go func() {
// Each worker goroutine consumes inputs from the shared input channel
for input := range inputCh {
// Make the API request and send the response to the result channel
tj, err := defineTJLawsuit(input.CNJNumber)
bodyStr, err := APIRequestLawsuit(url+tj+API, method, auth, input)
resultCh <- bodyStr
if err != nil {
// If there is an error making the API request, print the error
log.Println("error send request: " + err.Error())
errorOnApiRequests = err
//break
} else {
log.Println("success send request: " + "200 " + input.CNJNumber)
}
}
// When the worker goroutine is done processing inputs, signal the wait group
wg.Done()
}()
}
// Wait for all worker goroutines to finish processing inputs
go func() {
wg.Wait()
close(resultCh)
}()
// Return early on error in any given call on API requests
if errorOnApiRequests != nil {
return nil, errorOnApiRequests
}
// Collect results from the result channel and return them as a slice
var results []models.ResponseBodyLawsuit
for result := range resultCh {
results = append(results, result)
}
return results, nil
}
// StreamInputsLawsuits sends inputs from a slice to a channel
func StreamInputsLawsuits(done <-chan struct{}, inputs []models.ReadCsvLaawsuit) <-chan models.ReadCsvLaawsuit {
// Create a channel to send inputs to
inputCh := make(chan models.ReadCsvLaawsuit)
go func() {
defer close(inputCh)
for _, input := range inputs {
select {
case inputCh <- input:
case <-done:
// If the done channel is closed prematurely, finish the loop (closing the input channel)
break
}
}
}()
return inputCh
}