videolibrary_list.go raw

   1  package bunny
   2  
   3  import "context"
   4  
   5  // VideoLibraries represents the response of the List Video Library API endpoint.
   6  //
   7  // Bunny.net API docs: https://docs.bunny.net/reference/videolibrarypublic_index
   8  type VideoLibraries PaginationReply[VideoLibrary]
   9  
  10  // VideoLibraryListOpts represents both PaginationOptions and the other optional
  11  // query parameters of the List endpoint.
  12  type VideoLibraryListOpts struct {
  13  	VideoLibraryGetOpts
  14  	PaginationOptions
  15  }
  16  
  17  // List retrieves the Video Libraries.
  18  // If opts is nil, DefaultPaginationPerPage and DefaultPaginationPage will be used.
  19  // if opts.Page or opts.PerPage is < 1, the related DefaultPagination values are used.
  20  //
  21  // Bunny.net API docs: https://docs.bunny.net/reference/videolibrarypublic_index
  22  func (s *VideoLibraryService) List(ctx context.Context, opts *VideoLibraryListOpts) (*VideoLibraries, error) {
  23  	const path = "/videolibrary"
  24  
  25  	var res VideoLibraries
  26  
  27  	// NOTE: The resourceList function is not used for the purpose of
  28  	// providing the extra query param options in VideoLibraryGetOpts. In the future
  29  	// hopefully it can be removed for a better solution. See the following discussion:
  30  	// https://github.com/simplesurance/bunny-go/pull/27#discussion_r1021270152
  31  
  32  	// Ensure that opts.Page is >=1, if it isn't bunny.net will send a
  33  	// different response JSON object, that contains only a single Object,
  34  	// without items and paginations fields. Enforcing opts.page =>1 ensures
  35  	// that we always unmarshall into the same struct.
  36  	if opts == nil {
  37  		opts = &VideoLibraryListOpts{
  38  			PaginationOptions: PaginationOptions{
  39  				Page:    DefaultPaginationPage,
  40  				PerPage: DefaultPaginationPerPage,
  41  			},
  42  		}
  43  	} else {
  44  		opts.ensureConstraints()
  45  	}
  46  
  47  	req, err := s.client.newGetRequest(path, opts)
  48  	if err != nil {
  49  		return nil, err
  50  	}
  51  
  52  	if err := s.client.sendRequest(ctx, req, &res); err != nil {
  53  		return nil, err
  54  	}
  55  
  56  	return &res, nil
  57  }
  58