Skip to content

Commit

Permalink
feat: export apidocs to a file #344
Browse files Browse the repository at this point in the history
  • Loading branch information
srinandan committed Dec 2, 2023
1 parent 69bbead commit b270478
Show file tree
Hide file tree
Showing 6 changed files with 165 additions and 4 deletions.
1 change: 1 addition & 0 deletions cmd/apidocs/apidocs.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ func init() {
Cmd.AddCommand(DocCmd)
Cmd.AddCommand(CreateCmd)
Cmd.AddCommand(UpdateCmd)
Cmd.AddCommand(ExpCmd)

_ = Cmd.MarkFlagRequired("org")
_ = Cmd.MarkFlagRequired("siteid")
Expand Down
54 changes: 54 additions & 0 deletions cmd/apidocs/export.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package apidocs

import (
"os"

"internal/apiclient"

"internal/client/apidocs"

"github.com/spf13/cobra"
)

// ExpCmd to export apidocs
var ExpCmd = &cobra.Command{
Use: "export",
Short: "Export API Docs to a file",
Long: "Export API Docs to a file",
Args: func(cmd *cobra.Command, args []string) (err error) {
return apiclient.SetApigeeOrg(org)
},
RunE: func(cmd *cobra.Command, args []string) (err error) {
if folder == "" {
folder, _ = os.Getwd()
}
if err = apiclient.FolderExists(folder); err != nil {
return err
}
return apidocs.Export(folder)
},
}

var (
folder string
)

func init() {

ExpCmd.Flags().StringVarP(&folder, "folder", "f",
"", "folder to export API Docs")
}
14 changes: 13 additions & 1 deletion cmd/apidocs/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,19 @@ var ListCmd = &cobra.Command{
return apiclient.SetApigeeOrg(org)
},
RunE: func(cmd *cobra.Command, args []string) (err error) {
_, err = apidocs.List(siteid)
_, err = apidocs.List(siteid, pageSize, pageToken)
return
},
}

var (
pageSize int
pageToken string
)

func init() {
ListCmd.Flags().IntVarP(&pageSize, "page-size", "",
-1, "The maximum number of versions to return")
ListCmd.Flags().StringVarP(&pageToken, "page-token", "",
"", "A page token, received from a previous call")
}
13 changes: 11 additions & 2 deletions cmd/org/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (

"internal/clilog"

"internal/client/apidocs"
"internal/client/apis"
"internal/client/appgroups"
"internal/client/apps"
Expand Down Expand Up @@ -180,6 +181,9 @@ var ExportCmd = &cobra.Command{
}
}

clilog.Info.Println("Exporting API Portal apidocs Configuration...")
apidocs.Export(portalsFolderName)

Check failure on line 185 in cmd/org/export.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `apidocs.Export` is not checked (errcheck)

Check failure on line 185 in cmd/org/export.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `apidocs.Export` is not checked (errcheck)

if runtimeType == "HYBRID" {
clilog.Info.Println("Exporting Sync Authorization Identities...")
if respBody, err = sync.Get(); err != nil {
Expand Down Expand Up @@ -307,8 +311,13 @@ func createFolders() (err error) {
if err = os.Mkdir(proxiesFolderName, 0o755); err != nil {
return err
}
err = os.Mkdir(sharedFlowsFolderName, 0o755)
return err
if os.Mkdir(sharedFlowsFolderName, 0o755); err != nil {

Check failure on line 314 in cmd/org/export.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `os.Mkdir` is not checked (errcheck)

Check failure on line 314 in cmd/org/export.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `os.Mkdir` is not checked (errcheck)
return err
}
if os.Mkdir(portalsFolderName, 0o755); err != nil {

Check failure on line 317 in cmd/org/export.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `os.Mkdir` is not checked (errcheck)

Check failure on line 317 in cmd/org/export.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `os.Mkdir` is not checked (errcheck)
return err
}
return nil
}

func exportKVMEntries(scope string, env string, listKVMBytes []byte) (err error) {
Expand Down
1 change: 1 addition & 0 deletions cmd/org/variables.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const (

proxiesFolderName = "proxies"
sharedFlowsFolderName = "sharedflows"
portalsFolderName = "portals"
)

var conn int
86 changes: 85 additions & 1 deletion internal/client/apidocs/apidocs.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,15 @@
package apidocs

import (
"encoding/json"
"fmt"
"net/url"
"path"
"strconv"
"strings"

"internal/apiclient"
"internal/client/sites"
)

type Action uint8
Expand All @@ -29,6 +33,37 @@ const (
UPDATE
)

const maxPageSize = 100

type listapidocs struct {
Status string `json:"status,omitempty"`
Message string `json:"message,omitempty"`
RequestID string `json:"requestId,omitempty"`
ErrorCode string `json:"errorCode,omitempty"`
Data []data `json:"data,omitempty"`
NextPageToken string `json:"nextPageToken,omitempty"`
}

type data struct {
SiteID string `json:"siteId,omitempty"`
ID string `json:"id,omitempty"`
Title string `json:"title,omitempty"`
Description string `json:"description,omitempty"`
Published bool `json:"published,omitempty"`
AnonAllowed bool `json:"anonAllowed,omitempty"`
ApiProductName string `json:"apiProductName,omitempty"`
RequireCallbackUrl bool `json:"requireCallbackUrl,omitempty"`
ImageUrl string `json:"imageUrl,omitempty"`
CategoryIDs []string `json:"categoryIds,omitempty"`
Modified string `json:"modified,omitempty"`
Visibility bool `json:"visibility,omitempty"`
EdgeAPIProductName string `json:"edgeAPIProductName,omitempty"`
SpecID string `json:"specId,omitempty"`
GraphqlSchema string `json:"graphqlSchema,omitempty"`
GraphqlEndpointUrl string `json:"graphqlEndpointUrl,omitempty"`
GraphqlSchemaDisplayName string `json:"graphqlSchemaDisplayName,omitempty"`
}

// Create
func Create(siteid string, title string, description string, published string,
anonAllowed string, apiProductName string, requireCallbackUrl string, imageUrl string,
Expand Down Expand Up @@ -113,9 +148,18 @@ func Get(siteid string, id string) (respBody []byte, err error) {
}

// List
func List(siteid string) (respBody []byte, err error) {
func List(siteid string, pageSize int, pageToken string) (respBody []byte, err error) {
u, _ := url.Parse(apiclient.BaseURL)
u.Path = path.Join(u.Path, apiclient.GetApigeeOrg(), "sites", siteid, "apidocs")
q := u.Query()
if pageSize != -1 {
q.Set("pageSize", strconv.Itoa(pageSize))
}
if pageToken != "" {
q.Set("pageToken", pageToken)
}

u.RawQuery = q.Encode()
respBody, err = apiclient.HttpClient(u.String())
return respBody, err
}
Expand Down Expand Up @@ -150,6 +194,46 @@ func UpdateDocumentation(siteid string, id string, displayName string, openAPIDo
return nil, nil
}

// Export
func Export(folder string) (err error) {
apiclient.ClientPrintHttpResponse.Set(false)
defer apiclient.ClientPrintHttpResponse.Set(apiclient.GetCmdPrintHttpResponseSetting())

siteids, err := sites.GetSiteIDs()
if err != nil {
return err
}

pageToken := ""
listdocs := listapidocs{}

for _, siteid := range siteids {
for {
l := listapidocs{}
listRespBytes, err := List(siteid, maxPageSize, pageToken)
if err != nil {
return fmt.Errorf("failed to fetch apidocs: %w", err)
}
err = json.Unmarshal(listRespBytes, &l)
if err != nil {
return fmt.Errorf("failed to unmarshall: %w", err)
}
listdocs.Data = append(listdocs.Data, l.Data...)
pageToken = l.NextPageToken
if l.NextPageToken == "" {
break
}
}
}

respBody, err := json.Marshal(listdocs.Data)
if err != nil {
return err
}
respBody, _ = apiclient.PrettifyJSON(respBody)
return apiclient.WriteByteArrayToFile(path.Join(folder, "apidocs.json"), false, respBody)
}

func getArrayStr(str []string) string {
tmp := strings.Join(str, ",")
tmp = strings.ReplaceAll(tmp, ",", "\",\"")
Expand Down

0 comments on commit b270478

Please sign in to comment.