@@ -95,20 +95,11 @@ func (s *ScanossApiServiceHttpImpl) buildURL(endpoint string, params QueryParams
9595 return u .String (), nil
9696}
9797
98- func (s * ScanossApiServiceHttpImpl ) GetWithParams (endpoint string , params QueryParams ) (* http.Response , error ) {
98+ func (s * ScanossApiServiceHttpImpl ) GetWithParams (ctx context. Context , endpoint string , params QueryParams ) (* http.Response , error ) {
9999 fullURL , err := s .buildURL (endpoint , params )
100100 if err != nil {
101101 return nil , fmt .Errorf ("failed to build URL: %w" , err )
102102 }
103-
104- ctx := s .ctx
105- if ctx == nil {
106- ctx = context .Background ()
107- }
108-
109- ctx , cancel := context .WithCancel (ctx )
110- defer cancel ()
111-
112103 req , err := http .NewRequestWithContext (ctx , http .MethodGet , fullURL , nil )
113104 if err != nil {
114105 return nil , fmt .Errorf ("failed to create request: %w" , err )
@@ -120,9 +111,9 @@ func (s *ScanossApiServiceHttpImpl) GetWithParams(endpoint string, params QueryP
120111
121112 resp , err := s .client .Do (req )
122113 if err != nil {
114+ log .Debug ().Msgf ("Get Request to %s failed: %v" , fullURL , err )
123115 return nil , fmt .Errorf ("request failed: %w" , err )
124116 }
125-
126117 return resp , nil
127118}
128119
@@ -132,61 +123,64 @@ func (s *ScanossApiServiceHttpImpl) SearchComponents(request entities.ComponentS
132123 log .Error ().Err (err ).Msg ("Invalid component search request" )
133124 return entities.ComponentSearchResponse {}, fmt .Errorf ("invalid search request: %w" , err )
134125 }
135-
136126 log .Debug ().
137127 Str ("search" , request .Search ).
138128 Str ("vendor" , request .Vendor ).
139129 Str ("component" , request .Component ).
130+ Str ("package" , request .Package ).
140131 Int32 ("limit" , request .Limit ).
141132 Msg ("Searching components via SCANOSS API" )
142133
143134 if s .apiKey == "" {
144135 log .Error ().Msg ("SCANOSS API key not configured" )
145136 return entities.ComponentSearchResponse {}, fmt .Errorf ("SCANOSS API key not configured" )
146137 }
147-
148138 params := QueryParams {
149139 "search" : request .Search ,
150140 }
151-
152141 if request .Vendor != "" {
153142 params ["vendor" ] = request .Vendor
154143 }
155-
156144 if request .Component != "" {
157145 params ["component" ] = request .Component
158146 }
159-
160147 if request .Package != "" {
161148 params ["package" ] = request .Package
162149 }
163-
164150 if request .Limit > 0 {
165151 params ["limit" ] = fmt .Sprintf ("%d" , request .Limit )
166152 }
167-
168153 if request .Offset > 0 {
169154 params ["offset" ] = fmt .Sprintf ("%d" , request .Offset )
170155 }
171-
172- resp , err := s .GetWithParams ("/api/v2/components/search" , params )
156+ if s .ctx == nil {
157+ log .Warn ().Msg ("No context provided, using default context" )
158+ s .ctx = context .Background ()
159+ }
160+ ctx , cancel := context .WithCancel (s .ctx )
161+ defer cancel ()
162+ // Send the component search request
163+ resp , err := s .GetWithParams (ctx , "/v2/components/search" , params )
173164 if err != nil {
174165 log .Error ().Err (err ).Msg ("Error calling SCANOSS component search API" )
175166 return entities.ComponentSearchResponse {}, fmt .Errorf ("API call failed: %w" , err )
176167 }
177168 defer resp .Body .Close ()
178-
169+ body , bodyErr := io .ReadAll (resp .Body )
170+ if bodyErr != nil {
171+ log .Warn ().Err (bodyErr ).Msg ("Failed to read response body" )
172+ } else {
173+ log .Debug ().Msgf ("Response body: '%v'" , string (body ))
174+ }
179175 if resp .StatusCode != http .StatusOK {
180- body , _ := io .ReadAll (resp .Body )
181176 log .Error ().Int ("statusCode" , resp .StatusCode ).Str ("body" , string (body )).Msg ("API returned non-200 status" )
182177 return entities.ComponentSearchResponse {}, fmt .Errorf ("API returned status %d: %s" , resp .StatusCode , string (body ))
183178 }
184-
185179 var apiResponse entities.ComponentSearchResponse
186- if err := json .NewDecoder (resp .Body ).Decode (& apiResponse ); err != nil {
187- return entities.ComponentSearchResponse {}, fmt .Errorf ("failed to decode response: %w" , err )
180+ if jsonErr := json .Unmarshal (body , & apiResponse ); jsonErr != nil {
181+ log .Error ().Err (jsonErr ).Msgf ("Failed to decode response body: %v" , jsonErr )
182+ return entities.ComponentSearchResponse {}, fmt .Errorf ("failed to decode response: %w" , jsonErr )
188183 }
189-
190184 log .Debug ().
191185 Int ("componentCount" , len (apiResponse .Components )).
192186 Msg ("Successfully retrieved component search results" )
@@ -212,24 +206,32 @@ func (s *ScanossApiServiceHttpImpl) GetLicensesByPurl(request entities.Component
212206 "purl" : request .Purl ,
213207 "requirement" : request .Requirement ,
214208 }
215-
216- resp , err := s .GetWithParams ("/api/v2/licenses/component" , params )
209+ if s .ctx == nil {
210+ log .Warn ().Msg ("No context provided, using default context" )
211+ s .ctx = context .Background ()
212+ }
213+ ctx , cancel := context .WithCancel (s .ctx )
214+ defer cancel ()
215+ resp , err := s .GetWithParams (ctx , "/v2/licenses/component" , params )
217216 if err != nil {
218217 log .Error ().Err (err ).Msg ("Error calling SCANOSS component search API" )
219218 return entities.GetLicensesByPurlResponse {}, fmt .Errorf ("API call failed: %w" , err )
220219 }
221220 defer resp .Body .Close ()
222-
221+ body , bodyErr := io .ReadAll (resp .Body )
222+ if bodyErr != nil {
223+ log .Warn ().Err (bodyErr ).Msg ("Failed to read response body" )
224+ } else {
225+ log .Debug ().Msgf ("Response body: '%v'" , string (body ))
226+ }
223227 if resp .StatusCode != http .StatusOK {
224- body , _ := io .ReadAll (resp .Body )
225228 log .Error ().Int ("statusCode" , resp .StatusCode ).Str ("body" , string (body )).Msg ("API returned non-200 status" )
226229 return entities.GetLicensesByPurlResponse {}, fmt .Errorf ("API returned status %d: %s" , resp .StatusCode , string (body ))
227230 }
228-
229231 var apiResponse entities.GetLicensesByPurlResponse
230- if err := json .NewDecoder (resp .Body ).Decode (& apiResponse ); err != nil {
231- return entities.GetLicensesByPurlResponse {}, fmt .Errorf ("failed to decode response: %w" , err )
232+ if jsonErr := json .Unmarshal (body , & apiResponse ); jsonErr != nil {
233+ log .Error ().Err (jsonErr ).Msgf ("Failed to decode response body: %v" , jsonErr )
234+ return entities.GetLicensesByPurlResponse {}, fmt .Errorf ("failed to decode response: %w" , jsonErr )
232235 }
233-
234236 return apiResponse , nil
235237}
0 commit comments