@@ -68,6 +68,7 @@ func NewClient(log *zerolog.Logger) *AppHttp {
68
68
log : log ,
69
69
}
70
70
}
71
+
71
72
func (c * AppHttp ) DoHttpRequest (ctx context.Context , req Request , res any ) error {
72
73
ctx , span := instrumentation .NewTraceSpan (ctx , "DoHttpRequest" )
73
74
defer span .End ()
@@ -83,49 +84,19 @@ func (c *AppHttp) DoHttpRequest(ctx context.Context, req Request, res any) error
83
84
fasthttp .ReleaseResponse (response )
84
85
}()
85
86
87
+ // Set method, URI, and headers
86
88
request .Header .SetMethod (req .Method )
87
89
request .SetRequestURI (req .Endpoint )
88
-
89
- // Set request headers
90
90
for key , value := range req .Headers {
91
91
request .Header .Set (key , value )
92
92
}
93
- // If there are files to upload, create multipart form data
94
- if req .Files != nil {
95
- var buffer bytes.Buffer
96
- writer := multipart .NewWriter (& buffer )
97
-
98
- // Add files to the form
99
- for key , file := range req .Files {
100
- part , err := writer .CreateFormFile (key , file .FileName ) // Adjust filename as needed
101
- if err != nil {
102
- return errors .Wrap (err , "failed to create form file" )
103
- }
104
-
105
- if _ , err := io .Copy (part , file .File ); err != nil {
106
- return errors .Wrap (err , "failed to copy file to form" )
107
- }
108
- }
109
-
110
- // Close the writer to finalize the form data
111
- if err := writer .Close (); err != nil {
112
- return errors .Wrap (err , "failed to close writer" )
113
- }
114
-
115
- request .SetBody (buffer .Bytes ())
116
- request .Header .Set ("Content-Type" , writer .FormDataContentType ())
117
- } else if req .Body != nil {
118
- // If there is a body, marshal it to JSON
119
- jsonBody , err := json .Marshal (req .Body )
120
- if err != nil {
121
- c .log .Err (err ).Ctx (ctx ).Msg ("[DoHttpRequest]Marshal" )
122
- return errors .Wrap (err , "failed to marshal request body" )
123
- }
124
93
125
- request .SetBody (jsonBody )
126
- request .Header .Set ("Content-Type" , constant .MIMEApplicationJSON )
94
+ // Handle body or multipart files
95
+ if err := c .prepareRequestBody (request , req ); err != nil {
96
+ return err
127
97
}
128
98
99
+ // Log request
129
100
start := time .Now ()
130
101
c .log .Info ().Ctx (ctx ).
131
102
Str ("method" , req .Method ).
@@ -134,34 +105,81 @@ func (c *AppHttp) DoHttpRequest(ctx context.Context, req Request, res any) error
134
105
Interface ("body" , string (request .Body ())).
135
106
Msg ("[DoHttpRequest]Sending request" )
136
107
137
- // Perform the request
108
+ // Execute request
138
109
if err := c .client .Do (request , response ); err != nil {
139
110
c .log .Err (err ).Ctx (ctx ).Msg ("[DoHttpRequest]client.Do" )
140
111
return errors .Wrap (err , "failed to execute HTTP request" )
141
112
}
142
113
114
+ // Log response
143
115
c .log .Info ().Ctx (ctx ).
144
116
Int ("status_code" , response .StatusCode ()).
145
117
Dur ("duration" , time .Since (start )).
146
118
RawJSON ("response" , response .Body ()).
147
119
Msg ("[DoHttpRequest]Received response" )
148
120
149
- // Check response status code
150
- if response .StatusCode () != fasthttp .StatusOK {
151
- c .log .Error ().Ctx (ctx ).
152
- Int ("status_code" , response .StatusCode ()).
153
- Dur ("duration" , time .Since (start )).
154
- Msg ("[DoHttpRequest] Unexpected status code" )
155
-
156
- return fmt .Errorf ("unexpected status code: %v" , response .StatusCode ())
121
+ // Check status code and decode response
122
+ if err := c .checkStatusCode (response ); err != nil {
123
+ return err
157
124
}
158
125
159
- // Decode response if a response struct is provided
160
- if response != nil {
126
+ if res != nil {
161
127
if err := json .Unmarshal (response .Body (), res ); err != nil {
162
128
c .log .Err (err ).Ctx (ctx ).Msg ("[DoHttpRequest]json.Unmarshal" )
163
129
return errors .Wrap (err , "failed to decode response" )
164
130
}
165
131
}
132
+
133
+ return nil
134
+ }
135
+
136
+ func (c * AppHttp ) checkStatusCode (response * fasthttp.Response ) error {
137
+ if response .StatusCode () != fasthttp .StatusOK {
138
+ return fmt .Errorf ("unexpected status code: %v" , response .StatusCode ())
139
+ }
140
+
141
+ return nil
142
+ }
143
+
144
+ func (c * AppHttp ) prepareRequestBody (request * fasthttp.Request , req Request ) error {
145
+ if req .Files != nil {
146
+ return c .prepareMultipartBody (request , req )
147
+ }
148
+
149
+ if req .Body != nil {
150
+ jsonBody , err := json .Marshal (req .Body )
151
+ if err != nil {
152
+ return errors .Wrap (err , "failed to marshal request body" )
153
+ }
154
+
155
+ request .SetBody (jsonBody )
156
+ request .Header .Set ("Content-Type" , constant .MIMEApplicationJSON )
157
+ }
158
+
159
+ return nil
160
+ }
161
+
162
+ func (c * AppHttp ) prepareMultipartBody (request * fasthttp.Request , req Request ) error {
163
+ var buffer bytes.Buffer
164
+ writer := multipart .NewWriter (& buffer )
165
+
166
+ for key , file := range req .Files {
167
+ part , err := writer .CreateFormFile (key , file .FileName )
168
+ if err != nil {
169
+ return errors .Wrap (err , "failed to create form file" )
170
+ }
171
+
172
+ if _ , err := io .Copy (part , file .File ); err != nil {
173
+ return errors .Wrap (err , "failed to copy file to form" )
174
+ }
175
+ }
176
+
177
+ if err := writer .Close (); err != nil {
178
+ return errors .Wrap (err , "failed to close writer" )
179
+ }
180
+
181
+ request .SetBody (buffer .Bytes ())
182
+ request .Header .Set ("Content-Type" , writer .FormDataContentType ())
183
+
166
184
return nil
167
185
}
0 commit comments