@@ -4,12 +4,13 @@ import (
4
4
"bytes"
5
5
"encoding/json"
6
6
"errors"
7
+ "io"
7
8
"net/http"
8
9
"razor/cache"
9
10
"razor/core"
11
+ "strings"
10
12
"time"
11
13
12
- "io/ioutil"
13
14
"razor/core/types"
14
15
15
16
"github.com/PaesslerAG/jsonpath"
@@ -23,62 +24,51 @@ func (*UtilsStruct) GetDataFromAPI(dataSourceURLStruct types.DataSourceURL, loca
23
24
}
24
25
cachedData , cachedErr := localCache .Read (dataSourceURLStruct .URL )
25
26
if cachedErr != nil {
26
- var body []byte
27
- if dataSourceURLStruct .Type == "GET" {
27
+ var response []byte
28
+ switch dataSourceURLStruct .Type {
29
+ case "GET" :
28
30
err := retry .Do (
29
31
func () error {
30
- response , err := client .Get (dataSourceURLStruct .URL )
31
- if err != nil {
32
- return err
33
- }
34
- defer response .Body .Close ()
35
- if response .StatusCode != 200 {
36
- log .Errorf ("API: %s responded with status code %d" , dataSourceURLStruct .URL , response .StatusCode )
37
- return errors .New ("unable to reach API" )
38
- }
39
- body , err = IOInterface .ReadAll (response .Body )
32
+ responseBody , err := ProcessRequest (client , dataSourceURLStruct , nil )
40
33
if err != nil {
34
+ log .Error ("Error in processing GET request: " , err )
41
35
return err
42
36
}
37
+ response = responseBody
43
38
return nil
44
- }, retry .Attempts (2 ), retry .Delay (time .Second * 2 ))
39
+ }, retry .Attempts (core . ProcessRequestRetryAttempts ), retry .Delay (time .Second * time . Duration ( core . ProcessRequestRetryDelay ) ))
45
40
if err != nil {
46
41
return nil , err
47
42
}
48
- }
49
- if dataSourceURLStruct .Type == "POST" {
43
+ case "POST" :
50
44
postBody , err := json .Marshal (dataSourceURLStruct .Body )
51
45
if err != nil {
52
46
log .Errorf ("Error in marshalling body of a POST request URL %s: %v" , dataSourceURLStruct .URL , err )
47
+ return nil , err
53
48
}
54
- responseBody := bytes .NewBuffer (postBody )
49
+ requestBody := bytes .NewBuffer (postBody )
55
50
err = retry .Do (
56
51
func () error {
57
- response , err := client .Post (dataSourceURLStruct .URL , dataSourceURLStruct .ContentType , responseBody )
58
- if err != nil {
59
- log .Errorf ("Error sending POST request URL %s: %v" , dataSourceURLStruct .URL , err )
60
- return err
61
- }
62
- defer response .Body .Close ()
63
- if response .StatusCode != 200 {
64
- log .Errorf ("URL: %s responded with status code %d" , dataSourceURLStruct .URL , response .StatusCode )
65
- return errors .New ("unable to reach API" )
66
- }
67
- body , err = ioutil .ReadAll (response .Body )
52
+ responseBody , err := ProcessRequest (client , dataSourceURLStruct , requestBody )
68
53
if err != nil {
54
+ log .Error ("Error in processing POST request: " , err )
69
55
return err
70
56
}
57
+ response = responseBody
71
58
return nil
72
- }, retry .Attempts (2 ), retry .Delay (time .Second * 2 ))
59
+ }, retry .Attempts (core . ProcessRequestRetryAttempts ), retry .Delay (time .Second * time . Duration ( core . ProcessRequestRetryDelay ) ))
73
60
if err != nil {
74
61
return nil , err
75
62
}
63
+ default :
64
+ return nil , errors .New ("invalid request type" )
76
65
}
66
+
77
67
dataToCache := cache.Data {
78
- Result : body ,
68
+ Result : response ,
79
69
}
80
70
localCache .Update (dataToCache , dataSourceURLStruct .URL , time .Now ().Add (time .Second * time .Duration (core .StateLength )).Unix ())
81
- return body , nil
71
+ return response , nil
82
72
}
83
73
log .Debugf ("Getting Data for URL %s from local cache..." , dataSourceURLStruct .URL )
84
74
return cachedData .Result , nil
@@ -105,3 +95,47 @@ func (*UtilsStruct) GetDataFromXHTML(dataSourceURLStruct types.DataSourceURL, se
105
95
}
106
96
return priceData , nil
107
97
}
98
+
99
+ func AddHeaderToRequest (request * http.Request , headerMap map [string ]string ) (* http.Request , error ) {
100
+ for key , value := range headerMap {
101
+ // If core.APIKeyRegex = `$` and if value starts with '$' then we need to fetch the respective value from env file
102
+ if strings .HasPrefix (value , core .APIKeyRegex ) {
103
+ _ , APIKey , err := GetKeyWordAndAPIKeyFromENVFile (value )
104
+ if err != nil {
105
+ log .Error ("Error in getting value from env file: " , err )
106
+ return nil , err
107
+ }
108
+ value = APIKey
109
+ }
110
+ log .Debugf ("Adding key: %s, value: %s pair to header" , key , value )
111
+ request .Header .Add (key , value )
112
+ }
113
+ return request , nil
114
+ }
115
+
116
+ func ProcessRequest (client http.Client , dataSourceURLStruct types.DataSourceURL , requestBody io.Reader ) ([]byte , error ) {
117
+ request , err := http .NewRequest (dataSourceURLStruct .Type , dataSourceURLStruct .URL , requestBody )
118
+ if err != nil {
119
+ return nil , err
120
+ }
121
+ requestWithHeader , err := AddHeaderToRequest (request , dataSourceURLStruct .Header )
122
+ if err != nil {
123
+ log .Errorf ("Error in adding header to %s request: %v" , dataSourceURLStruct .Type , err )
124
+ return nil , err
125
+ }
126
+ response , err := client .Do (requestWithHeader )
127
+ if err != nil {
128
+ log .Errorf ("Error sending %s request URL %s: %v" , dataSourceURLStruct .Type , dataSourceURLStruct .URL , err )
129
+ return nil , err
130
+ }
131
+ defer response .Body .Close ()
132
+ if response .StatusCode != 200 {
133
+ log .Errorf ("API: %s responded with status code %d" , dataSourceURLStruct .URL , response .StatusCode )
134
+ return nil , errors .New ("unable to reach API" )
135
+ }
136
+ responseBody , err := io .ReadAll (response .Body )
137
+ if err != nil {
138
+ return nil , err
139
+ }
140
+ return responseBody , nil
141
+ }
0 commit comments