Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: adds support to get category by name #354 #355

Merged
merged 5 commits into from
Dec 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions cmd/apicategories/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,27 @@ import (
// GetCmd to get a catalog items
var GetCmd = &cobra.Command{
Use: "get",
Short: "Gets an API Category by ID",
Long: "Gets an API Category by ID",
Short: "Gets an API Category by ID or name",
Long: "Gets an API Category by ID or name",
Args: func(cmd *cobra.Command, args []string) (err error) {
if siteid == "" {
return fmt.Errorf("siteid is a mandatory parameter")
}
if name == "" && id == "" {
return fmt.Errorf("name or id must be set as a parameter")
}
if name != "" && id != "" {
return fmt.Errorf("name and id cannot be set as a parameter")
}
return apiclient.SetApigeeOrg(org)
},
RunE: func(cmd *cobra.Command, args []string) (err error) {
if siteid == "" {
return fmt.Errorf("siteid is a mandatory parameter")
if name != "" {
var payload []byte
if payload, err = apicategories.GetByName(siteid, name); err != nil {
return err
}
return apiclient.PrettyPrint("application/json", payload)
}
_, err = apicategories.Get(siteid, id)
return
Expand All @@ -44,5 +57,6 @@ var GetCmd = &cobra.Command{
func init() {
GetCmd.Flags().StringVarP(&id, "id", "i",
"", "API Category ID")
_ = GetCmd.MarkFlagRequired("id")
GetCmd.Flags().StringVarP(&name, "name", "n",
"", "API Catalog Name")
}
16 changes: 15 additions & 1 deletion cmd/apidocs/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,22 @@ var GetCmd = &cobra.Command{
if siteid == "" {
return fmt.Errorf("siteid is a mandatory parameter")
}
if name == "" && id == "" {
return fmt.Errorf("title or id must be set as a parameter")
}
if name != "" && id != "" {
return fmt.Errorf("title and id cannot be set as a parameter")
}
return apiclient.SetApigeeOrg(org)
},
RunE: func(cmd *cobra.Command, args []string) (err error) {
if name != "" {
var payload []byte
if payload, err = apidocs.GetByTitle(siteid, name); err != nil {
return err
}
return apiclient.PrettyPrint("application/json", payload)
}
_, err = apidocs.Get(siteid, id)
return
},
Expand All @@ -43,5 +56,6 @@ var GetCmd = &cobra.Command{
func init() {
GetCmd.Flags().StringVarP(&id, "id", "i",
"", "Catalog ID")
_ = GetCmd.MarkFlagRequired("id")
GetCmd.Flags().StringVarP(&name, "title", "",
"", "Catalog Title")
}
35 changes: 35 additions & 0 deletions internal/client/apicategories/apicategories.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,15 @@ import (
"net/url"
"os"
"path"
"reflect"
"strings"

"internal/apiclient"

"internal/client/sites"
"internal/clilog"

"github.com/thedevsaddam/gojsonq"
)

type listapicategories struct {
Expand Down Expand Up @@ -93,6 +97,37 @@ func Update(siteid string, name string) (respBody []byte, err error) {
return respBody, err
}

// GetByName
func GetByName(siteid string, name string) (respBody []byte, err error) {
apiclient.ClientPrintHttpResponse.Set(false)
defer apiclient.ClientPrintHttpResponse.Set(apiclient.GetCmdPrintHttpResponseSetting())
listRespBytes, err := List(siteid)
if err != nil {
return nil, fmt.Errorf("failed to fetch apidocs: %w", err)
}
out := gojsonq.New().JSONString(string(listRespBytes)).From("data").Where("name", "eq", name).First()
if isNil(out) {
return nil, fmt.Errorf("unable to find category with name %s", name)
}
outBytes, err := json.Marshal(out)
if err != nil {
return outBytes, err
}
return outBytes, nil
}

// from: https://mangatmodi.medium.com/go-check-nil-interface-the-right-way-d142776edef1
func isNil(i interface{}) bool {
if i == nil {
return true
}
switch reflect.TypeOf(i).Kind() {
case reflect.Ptr, reflect.Map, reflect.Array, reflect.Chan, reflect.Slice:
return reflect.ValueOf(i).IsNil()
}
return false
}

// Export
func Export(folder string) (err error) {
apiclient.ClientPrintHttpResponse.Set(false)
Expand Down
33 changes: 33 additions & 0 deletions internal/client/apidocs/apidocs.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,39 @@ func Get(siteid string, id string) (respBody []byte, err error) {
return respBody, err
}

// GetByTitle
func GetByTitle(siteid string, title string) (respBody []byte, err error) {
apiclient.ClientPrintHttpResponse.Set(false)
defer apiclient.ClientPrintHttpResponse.Set(apiclient.GetCmdPrintHttpResponseSetting())
fullList := listapidocs{}
pageToken := ""
for {
l := listapidocs{}
listRespBytes, err := List(siteid, maxPageSize, pageToken)
if err != nil {
return nil, fmt.Errorf("failed to fetch apidocs: %w", err)
}
err = json.Unmarshal(listRespBytes, &l)
if err != nil {
return nil, fmt.Errorf("failed to unmarshall: %w", err)
}
fullList.Data = append(fullList.Data, l.Data...)
pageToken = l.NextPageToken
if l.NextPageToken == "" {
break
}
}
for _, data := range fullList.Data {
if data.Title == title {
if respBody, err = json.Marshal(data); err != nil {
return nil, err
}
return respBody, nil
}
}
return nil, fmt.Errorf("unable to find apidocs with title %s", title)
}

// List
func List(siteid string, pageSize int, pageToken string) (respBody []byte, err error) {
u, _ := url.Parse(apiclient.BaseURL)
Expand Down