Skip to content

Commit

Permalink
* Add Method to delete catalogues (#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
edsonmichaque committed Nov 30, 2023
1 parent 7661f4f commit 94ccea8
Show file tree
Hide file tree
Showing 3 changed files with 175 additions and 164 deletions.
150 changes: 0 additions & 150 deletions catalogs.go

This file was deleted.

161 changes: 161 additions & 0 deletions catalogues.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
// Copyright 2023 Tyk Technologies
// SPDX-License-Identifier: MPL-2.0

package portal

import (
"bytes"
"context"
"encoding/json"
"fmt"
)

const (
pathCatalogues = "/portal-api/catalogues"
pathCatalogue = "/portal-api/catalogues/%d"
)

//go:generate mockery --name Catalogs --filename catalogs.go
type Catalogues interface {
CreateCatalogue(ctx context.Context, input *CreateCatalogueInput, opts ...Option) (*CreateCatalogueOutput, error)
GetCatalogue(ctx context.Context, id int64, opts ...Option) (*GetCatalogueOutput, error)
ListCatalogues(ctx context.Context, options *ListCataloguesInput, opts ...Option) (*ListCataloguesOutput, error)
UpdateCatalogue(ctx context.Context, id int64, input *UpdateCatalogueInput, opts ...Option) (*UpdateCatalogueOutput, error)
DeleteCatalogue(ctx context.Context, id int64, opts ...Option) (*CatalogueOutput, error)
}

type catalogues struct {
client *Client
}

func (p catalogues) CreateCatalogue(ctx context.Context, input *CreateCatalogueInput, opts ...Option) (*CreateCatalogueOutput, error) {
payload, err := json.Marshal(input)
if err != nil {
return nil, err
}

resp, err := p.client.doPost(ctx, pathCatalogues, bytes.NewReader(payload), nil)
if err != nil {
return nil, err
}

var catalog Catalogue

if err := resp.Unmarshal(&catalog); err != nil {
return nil, err
}

return &CreateCatalogueOutput{
Data: &catalog,
}, nil
}

func (p catalogues) GetCatalogue(ctx context.Context, id int64, opts ...Option) (*GetCatalogueOutput, error) {
resp, err := p.client.doGet(ctx, fmt.Sprintf(pathCatalogue, id), nil)
if err != nil {
return nil, err
}

var catalog Catalogue
if err := resp.Unmarshal(&catalog); err != nil {
return nil, err
}

return &GetCatalogueOutput{
Data: &catalog,
}, nil
}

func (p catalogues) ListCatalogues(ctx context.Context, options *ListCataloguesInput, opts ...Option) (*ListCataloguesOutput, error) {
resp, err := p.client.doGet(ctx, pathCatalogues, nil)
if err != nil {
return nil, err
}

var catalogs []Catalogue

if err := resp.Unmarshal(&catalogs); err != nil {
return nil, err
}

return &ListCataloguesOutput{
Data: catalogs,
}, nil
}

func (p catalogues) UpdateCatalogue(ctx context.Context, id int64, input *UpdateCatalogueInput, opts ...Option) (*UpdateCatalogueOutput, error) {
payload, err := json.Marshal(input)
if err != nil {
return nil, err
}

resp, err := p.client.doPut(ctx, fmt.Sprintf(pathCatalogue, id), bytes.NewReader(payload), nil)
if err != nil {
return nil, err
}

var catalog Catalogue

if err := resp.Unmarshal(&catalog); err != nil {
return nil, err
}

return &UpdateCatalogueOutput{
Data: &catalog,
}, nil
}

func (p catalogues) DeleteCatalogue(ctx context.Context, id int64, opts ...Option) (*CatalogueOutput, error) {
_, err := p.client.doDelete(ctx, fmt.Sprintf(pathCatalogue, id), nil, nil)
if err != nil {
return nil, err
}

return &CatalogueOutput{}, nil
}


type CatalogueInput struct {
ID *int64 `json:"ID,omitempty"`
Name string `json:"Name,omitempty"`
NameWithSlug string `json:"NameWithSlug,omitempty"`
Plans []int64 `json:"Plans,omitempty"`
Products []int64 `json:"Products,omitempty"`
VisibilityStatus string `json:"VisibilityStatus,omitempty"`
OrgCatalogs []struct {
OrganizationID int `json:"OrganizationID,omitempty"`
TeamID int `json:"TeamID,omitempty"`
} `json:"OrgCatalogs,omitempty"`
}

type UpdateCatalogueInput = CatalogueInput

type CreateCatalogueInput = CatalogueInput

type ListCataloguesInput struct{}

type ListCataloguesOutput struct {
Data []Catalogue
}

type Catalogue struct {
CreatedAt string `json:"CreatedAt,omitempty"`
ID int64 `json:"ID,omitempty"`
Name string `json:"Name,omitempty"`
OrgCatalogs []any `json:"OrgCatalogs,omitempty"`
Plans []any `json:"Plans,omitempty"`
Products []string `json:"Products,omitempty"`
UpdatedAt string `json:"UpdatedAt,omitempty"`
VisibilityStatus string `json:"VisibilityStatus,omitempty"`
NameWithSlug string `json:"NameWithSlug,omitempty"`
}

type CatalogueOutput struct {
Data *Catalogue
}

type UpdateCatalogueOutput = CatalogueOutput

type GetCatalogueOutput = CatalogueOutput

type CreateCatalogueOutput = CatalogueOutput
28 changes: 14 additions & 14 deletions portal.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,15 +101,15 @@ type Client struct {
minRetryBackoff time.Duration
skipValidation bool

pages Pages
providers Providers
plans Plans
users Users
orgs Orgs
products Products
catalogs Catalogs
ars ARs
apps Apps
pages Pages
providers Providers
plans Plans
users Users
orgs Orgs
products Products
catalogues Catalogues
ars ARs
apps Apps
}

func (c Client) Apps() Apps {
Expand All @@ -128,12 +128,12 @@ func (c *Client) SetARs(ar ARs) {
c.ars = ar
}

func (c Client) Catalogs() Catalogs {
return c.catalogs
func (c Client) Catalogues() Catalogues {
return c.catalogues
}

func (c *Client) SetCatalogs(catalogs Catalogs) {
c.catalogs = catalogs
func (c *Client) SetCatalogues(catalogues Catalogues) {
c.catalogues = catalogues
}

func (c Client) Products() Products {
Expand Down Expand Up @@ -231,7 +231,7 @@ func newClient(opts ...Option) (*Client, error) {
client.users = &users{client: client}
client.orgs = &orgs{client: client}
client.products = &products{client: client}
client.catalogs = &catalogs{client: client}
client.catalogues = &catalogues{client: client}
client.ars = &ars{client: client}
client.pages = &pages{client: client}
client.apps = &apps{client: client}
Expand Down

0 comments on commit 94ccea8

Please sign in to comment.