@@ -246,7 +246,8 @@ func checkResponseStatus(r *http.Response) error {
246
246
return fmt .Errorf ("postman Request failed with status code: %d" , r .StatusCode )
247
247
}
248
248
249
- func (c * Client ) getPostmanReq (ctx context.Context , url string , headers map [string ]string ) (* http.Response , error ) {
249
+ // getPostmanResponseBodyBytes makes a request to the Postman API and returns the response body as bytes.
250
+ func (c * Client ) getPostmanResponseBodyBytes (ctx context.Context , url string , headers map [string ]string ) ([]byte , error ) {
250
251
req , err := c .NewRequest (url , headers )
251
252
if err != nil {
252
253
return nil , err
@@ -256,13 +257,19 @@ func (c *Client) getPostmanReq(ctx context.Context, url string, headers map[stri
256
257
if err != nil {
257
258
return nil , err
258
259
}
260
+ defer resp .Body .Close ()
261
+
262
+ body , err := io .ReadAll (resp .Body )
263
+ if err != nil {
264
+ return nil , fmt .Errorf ("could not read postman response body: %w" , err )
265
+ }
259
266
260
267
ctx .Logger ().V (4 ).Info ("postman api response headers are available" , "response_header" , resp .Header )
261
268
262
269
if err := checkResponseStatus (resp ); err != nil {
263
270
return nil , err
264
271
}
265
- return resp , nil
272
+ return body , nil
266
273
}
267
274
268
275
// EnumerateWorkspaces returns the workspaces for a given user (both private, public, team and personal).
@@ -276,17 +283,10 @@ func (c *Client) EnumerateWorkspaces(ctx context.Context) ([]Workspace, error) {
276
283
if err := c .WorkspaceAndCollectionRateLimiter .Wait (ctx ); err != nil {
277
284
return nil , fmt .Errorf ("could not wait for rate limiter during workspaces enumeration getting: %w" , err )
278
285
}
279
- r , err := c .getPostmanReq (ctx , "https://api.getpostman.com/workspaces" , nil )
286
+ body , err := c .getPostmanResponseBodyBytes (ctx , "https://api.getpostman.com/workspaces" , nil )
280
287
if err != nil {
281
- return nil , fmt .Errorf ("could not get workspaces during enumeration: %w" , err )
288
+ return nil , fmt .Errorf ("could not get postman workspace response bytes during enumeration: %w" , err )
282
289
}
283
-
284
- body , err := io .ReadAll (r .Body )
285
- if err != nil {
286
- return nil , fmt .Errorf ("could not read response body for workspaces during enumeration: %w" , err )
287
- }
288
- r .Body .Close ()
289
-
290
290
if err := json .Unmarshal ([]byte (body ), & workspacesObj ); err != nil {
291
291
return nil , fmt .Errorf ("could not unmarshal workspaces JSON during enumeration: %w" , err )
292
292
}
@@ -318,17 +318,10 @@ func (c *Client) GetWorkspace(ctx context.Context, workspaceUUID string) (Worksp
318
318
if err := c .WorkspaceAndCollectionRateLimiter .Wait (ctx ); err != nil {
319
319
return Workspace {}, fmt .Errorf ("could not wait for rate limiter during workspace getting: %w" , err )
320
320
}
321
- r , err := c .getPostmanReq (ctx , url , nil )
321
+ body , err := c .getPostmanResponseBodyBytes (ctx , url , nil )
322
322
if err != nil {
323
- return Workspace {}, fmt .Errorf ("could not get workspace (%s): %w" , workspaceUUID , err )
323
+ return Workspace {}, fmt .Errorf ("could not get postman workspace (%s) response bytes : %w" , workspaceUUID , err )
324
324
}
325
-
326
- body , err := io .ReadAll (r .Body )
327
- if err != nil {
328
- return Workspace {}, fmt .Errorf ("could not read response body for workspace (%s): %w" , workspaceUUID , err )
329
- }
330
- r .Body .Close ()
331
-
332
325
if err := json .Unmarshal ([]byte (body ), & obj ); err != nil {
333
326
return Workspace {}, fmt .Errorf ("could not unmarshal workspace JSON for workspace (%s): %w" , workspaceUUID , err )
334
327
}
@@ -346,16 +339,10 @@ func (c *Client) GetEnvironmentVariables(ctx context.Context, environment_uuid s
346
339
if err := c .GeneralRateLimiter .Wait (ctx ); err != nil {
347
340
return VariableData {}, fmt .Errorf ("could not wait for rate limiter during environment variable getting: %w" , err )
348
341
}
349
- r , err := c .getPostmanReq (ctx , url , nil )
350
- if err != nil {
351
- return VariableData {}, fmt .Errorf ("could not get env variables for environment (%s): %w" , environment_uuid , err )
352
- }
353
-
354
- body , err := io .ReadAll (r .Body )
342
+ body , err := c .getPostmanResponseBodyBytes (ctx , url , nil )
355
343
if err != nil {
356
- return VariableData {}, fmt .Errorf ("could not read env var response body for environment (%s): %w" , environment_uuid , err )
344
+ return VariableData {}, fmt .Errorf ("could not get postman environment (%s) response bytes : %w" , environment_uuid , err )
357
345
}
358
- r .Body .Close ()
359
346
if err := json .Unmarshal ([]byte (body ), & obj ); err != nil {
360
347
return VariableData {}, fmt .Errorf ("could not unmarshal env variables JSON for environment (%s): %w" , environment_uuid , err )
361
348
}
@@ -373,16 +360,10 @@ func (c *Client) GetCollection(ctx context.Context, collection_uuid string) (Col
373
360
if err := c .WorkspaceAndCollectionRateLimiter .Wait (ctx ); err != nil {
374
361
return Collection {}, fmt .Errorf ("could not wait for rate limiter during collection getting: %w" , err )
375
362
}
376
- r , err := c .getPostmanReq (ctx , url , nil )
377
- if err != nil {
378
- return Collection {}, fmt .Errorf ("could not get collection (%s): %w" , collection_uuid , err )
379
- }
380
-
381
- body , err := io .ReadAll (r .Body )
363
+ body , err := c .getPostmanResponseBodyBytes (ctx , url , nil )
382
364
if err != nil {
383
- return Collection {}, fmt .Errorf ("could not read response body for collection (%s): %w" , collection_uuid , err )
365
+ return Collection {}, fmt .Errorf ("could not get postman collection (%s) response bytes : %w" , collection_uuid , err )
384
366
}
385
- r .Body .Close ()
386
367
if err := json .Unmarshal ([]byte (body ), & obj ); err != nil {
387
368
return Collection {}, fmt .Errorf ("could not unmarshal JSON for collection (%s): %w" , collection_uuid , err )
388
369
}
0 commit comments