template.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package client
  2. import (
  3. "context"
  4. "fmt"
  5. "net/http"
  6. "github.com/porter-dev/porter/internal/models"
  7. )
  8. func (c *Client) ListTemplates(
  9. ctx context.Context,
  10. ) ([]*models.PorterChartList, error) {
  11. req, err := http.NewRequest(
  12. "GET",
  13. fmt.Sprintf("%s/templates", c.BaseURL),
  14. nil,
  15. )
  16. if err != nil {
  17. return nil, err
  18. }
  19. req = req.WithContext(ctx)
  20. bodyResp := make([]*models.PorterChartList, 0)
  21. if httpErr, err := c.sendRequest(req, &bodyResp, true); httpErr != nil || err != nil {
  22. if httpErr != nil {
  23. return nil, fmt.Errorf("code %d, errors %v", httpErr.Code, httpErr.Errors)
  24. }
  25. return nil, err
  26. }
  27. return bodyResp, nil
  28. }
  29. func (c *Client) GetTemplate(
  30. ctx context.Context,
  31. name, version string,
  32. ) (*models.PorterChartRead, error) {
  33. req, err := http.NewRequest(
  34. "GET",
  35. fmt.Sprintf("%s/templates/%s/%s", c.BaseURL, name, version),
  36. nil,
  37. )
  38. if err != nil {
  39. return nil, err
  40. }
  41. req = req.WithContext(ctx)
  42. bodyResp := &models.PorterChartRead{}
  43. if httpErr, err := c.sendRequest(req, &bodyResp, true); httpErr != nil || err != nil {
  44. if httpErr != nil {
  45. return nil, fmt.Errorf("code %d, errors %v", httpErr.Code, httpErr.Errors)
  46. }
  47. return nil, err
  48. }
  49. return bodyResp, nil
  50. }