github.com/pivotal-cf/go-pivnet/v6@v6.0.2/products.go (about)

     1  package pivnet
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  
     7  	"encoding/json"
     8  	"github.com/pivotal-cf/go-pivnet/v6/logger"
     9  )
    10  
    11  type ProductsService struct {
    12  	client Client
    13  	l      logger.Logger
    14  }
    15  
    16  type S3Directory struct {
    17  	Path string `json:"path,omitempty" yaml:"path,omitempty"`
    18  }
    19  
    20  type Product struct {
    21  	ID            int          `json:"id,omitempty" yaml:"id,omitempty"`
    22  	Slug          string       `json:"slug,omitempty" yaml:"slug,omitempty"`
    23  	Name          string       `json:"name,omitempty" yaml:"name,omitempty"`
    24  	S3Directory   *S3Directory `json:"s3_directory,omitempty" yaml:"s3_directory,omitempty"`
    25  	InstallsOnPks bool         `json:"installs_on_pks,omitempty" yaml:"installs_on_pks,omitempty"`
    26  }
    27  
    28  type SlugAliasResponse struct {
    29  	Slugs       []string `json:"slugs" yaml:"slugs"`
    30  	CurrentSlug string   `json:"current_slug" yaml:"current_slug"`
    31  }
    32  
    33  type ProductsResponse struct {
    34  	Products []Product `json:"products,omitempty"`
    35  }
    36  
    37  func (p ProductsService) List() ([]Product, error) {
    38  	url := "/products"
    39  
    40  	var response ProductsResponse
    41  	resp, err := p.client.MakeRequest(
    42  		"GET",
    43  		url,
    44  		http.StatusOK,
    45  		nil,
    46  	)
    47  	if err != nil {
    48  		return []Product{}, err
    49  	}
    50  	defer resp.Body.Close()
    51  
    52  	err = json.NewDecoder(resp.Body).Decode(&response)
    53  	if err != nil {
    54  		return []Product{}, err
    55  	}
    56  
    57  	return response.Products, nil
    58  }
    59  
    60  func (p ProductsService) Get(slug string) (Product, error) {
    61  	url := fmt.Sprintf("/products/%s", slug)
    62  
    63  	var response Product
    64  	resp, err := p.client.MakeRequest(
    65  		"GET",
    66  		url,
    67  		http.StatusOK,
    68  		nil,
    69  	)
    70  	if err != nil {
    71  		return Product{}, err
    72  	}
    73  	defer resp.Body.Close()
    74  
    75  	err = json.NewDecoder(resp.Body).Decode(&response)
    76  	if err != nil {
    77  		return Product{}, err
    78  	}
    79  
    80  	return response, nil
    81  }
    82  
    83  func (p ProductsService) SlugAlias(slug string) (SlugAliasResponse, error) {
    84  	url := fmt.Sprintf("/products/%s/slug_alias", slug)
    85  
    86  	var response SlugAliasResponse
    87  	resp, err := p.client.MakeRequest(
    88  		"GET",
    89  		url,
    90  		http.StatusOK,
    91  		nil,
    92  	)
    93  	if err != nil {
    94  		return SlugAliasResponse{}, err
    95  	}
    96  	defer resp.Body.Close()
    97  
    98  	err = json.NewDecoder(resp.Body).Decode(&response)
    99  	if err != nil {
   100  		return SlugAliasResponse{}, err
   101  	}
   102  
   103  	return response, nil
   104  }