@@ -246,7 +246,8 @@ func checkResponseStatus(r *http.Response) error {
246246 return fmt .Errorf ("postman Request failed with status code: %d" , r .StatusCode )
247247}
248248
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 ) {
250251 req , err := c .NewRequest (url , headers )
251252 if err != nil {
252253 return nil , err
@@ -256,13 +257,19 @@ func (c *Client) getPostmanReq(ctx context.Context, url string, headers map[stri
256257 if err != nil {
257258 return nil , err
258259 }
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+ }
259266
260267 ctx .Logger ().V (4 ).Info ("postman api response headers are available" , "response_header" , resp .Header )
261268
262269 if err := checkResponseStatus (resp ); err != nil {
263270 return nil , err
264271 }
265- return resp , nil
272+ return body , nil
266273}
267274
268275// 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) {
276283 if err := c .WorkspaceAndCollectionRateLimiter .Wait (ctx ); err != nil {
277284 return nil , fmt .Errorf ("could not wait for rate limiter during workspaces enumeration getting: %w" , err )
278285 }
279- r , err := c .getPostmanReq (ctx , "https://api.getpostman.com/workspaces" , nil )
286+ body , err := c .getPostmanResponseBodyBytes (ctx , "https://api.getpostman.com/workspaces" , nil )
280287 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 )
282289 }
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-
290290 if err := json .Unmarshal ([]byte (body ), & workspacesObj ); err != nil {
291291 return nil , fmt .Errorf ("could not unmarshal workspaces JSON during enumeration: %w" , err )
292292 }
@@ -318,17 +318,10 @@ func (c *Client) GetWorkspace(ctx context.Context, workspaceUUID string) (Worksp
318318 if err := c .WorkspaceAndCollectionRateLimiter .Wait (ctx ); err != nil {
319319 return Workspace {}, fmt .Errorf ("could not wait for rate limiter during workspace getting: %w" , err )
320320 }
321- r , err := c .getPostmanReq (ctx , url , nil )
321+ body , err := c .getPostmanResponseBodyBytes (ctx , url , nil )
322322 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 )
324324 }
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-
332325 if err := json .Unmarshal ([]byte (body ), & obj ); err != nil {
333326 return Workspace {}, fmt .Errorf ("could not unmarshal workspace JSON for workspace (%s): %w" , workspaceUUID , err )
334327 }
@@ -346,16 +339,10 @@ func (c *Client) GetEnvironmentVariables(ctx context.Context, environment_uuid s
346339 if err := c .GeneralRateLimiter .Wait (ctx ); err != nil {
347340 return VariableData {}, fmt .Errorf ("could not wait for rate limiter during environment variable getting: %w" , err )
348341 }
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 )
355343 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 )
357345 }
358- r .Body .Close ()
359346 if err := json .Unmarshal ([]byte (body ), & obj ); err != nil {
360347 return VariableData {}, fmt .Errorf ("could not unmarshal env variables JSON for environment (%s): %w" , environment_uuid , err )
361348 }
@@ -373,16 +360,10 @@ func (c *Client) GetCollection(ctx context.Context, collection_uuid string) (Col
373360 if err := c .WorkspaceAndCollectionRateLimiter .Wait (ctx ); err != nil {
374361 return Collection {}, fmt .Errorf ("could not wait for rate limiter during collection getting: %w" , err )
375362 }
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 )
382364 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 )
384366 }
385- r .Body .Close ()
386367 if err := json .Unmarshal ([]byte (body ), & obj ); err != nil {
387368 return Collection {}, fmt .Errorf ("could not unmarshal JSON for collection (%s): %w" , collection_uuid , err )
388369 }
0 commit comments