Skip to content

Commit

Permalink
feat: add support to import apidocs #345
Browse files Browse the repository at this point in the history
  • Loading branch information
srinandan committed Dec 4, 2023
1 parent 1daa094 commit 3e04f1f
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 6 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,6 @@ apiproxy/

#keys for envoy
remote-service.*

#json files
*.json
1 change: 1 addition & 0 deletions cmd/apidocs/apidocs.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func init() {
Cmd.AddCommand(CreateCmd)
Cmd.AddCommand(UpdateCmd)
Cmd.AddCommand(ExpCmd)
Cmd.AddCommand(ImpCmd)

_ = Cmd.MarkFlagRequired("org")
_ = Cmd.MarkFlagRequired("siteid")
Expand Down
6 changes: 2 additions & 4 deletions cmd/apidocs/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,14 @@ var ExpCmd = &cobra.Command{
if err = apiclient.FolderExists(folder); err != nil {
return err
}
apiclient.DisableCmdPrintHttpResponse()
return apidocs.Export(folder)
},
}

var (
folder string
)
var folder string

func init() {

ExpCmd.Flags().StringVarP(&folder, "folder", "f",
"", "folder to export API Docs")
}
50 changes: 50 additions & 0 deletions cmd/apidocs/import.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright 2023 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 (
"internal/apiclient"

"internal/client/apidocs"

"github.com/spf13/cobra"
)

// ImpCmd to import products
var ImpCmd = &cobra.Command{
Use: "import",
Short: "Import from a folder containing apidocs",
Long: "Import from a folder containing apidocs",
Args: func(cmd *cobra.Command, args []string) (err error) {
return apiclient.SetApigeeOrg(org)
},
RunE: func(cmd *cobra.Command, args []string) error {
return apidocs.Import(conn, folder)
},
}

var (
conn int
filePath string
)

func init() {
ImpCmd.Flags().StringVarP(&filePath, "folder", "f",
"", "Folder containing apidocs.json and apidocs_<siteid>_<id>.json files")
ImpCmd.Flags().IntVarP(&conn, "conn", "c",
4, "Number of connections")

_ = ImpCmd.MarkFlagRequired("file")
}
29 changes: 28 additions & 1 deletion internal/client/apidocs/apidocs.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (

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

type Action uint8
Expand Down Expand Up @@ -173,7 +174,9 @@ func Delete(siteid string, id string) (respBody []byte, err error) {
}

// UpdateDocumentation
func UpdateDocumentation(siteid string, id string, displayName string, openAPIDoc string, graphQLDoc string, endpointUri string) (respBody []byte, err error) {
func UpdateDocumentation(siteid string, id string, displayName string,
openAPIDoc string, graphQLDoc string, endpointUri string,
) (respBody []byte, err error) {
var payload string

if openAPIDoc != "" {
Expand Down Expand Up @@ -220,6 +223,17 @@ func Export(folder string) (err error) {
}
listdocs.Data = append(listdocs.Data, l.Data...)
pageToken = l.NextPageToken
// write apidocs Documentation
for _, data := range l.Data {
respDocsBody, err := GetDocumentation(siteid, data.ID)
if err != nil {
return err
}
docFileName := fmt.Sprintf("apidocs_%s_%s.json", siteid, data.ID)
if err = apiclient.WriteByteArrayToFile(path.Join(folder, docFileName), false, respDocsBody); err != nil {
return err
}
}
if l.NextPageToken == "" {
break
}
Expand All @@ -234,8 +248,21 @@ func Export(folder string) (err error) {
return apiclient.WriteByteArrayToFile(path.Join(folder, "apidocs.json"), false, respBody)
}

func Import(conn int, folder string) (err error) {
entities, err := readAPIDocsFile(path.Join(folder, "apidocs.json"))

Check failure on line 252 in internal/client/apidocs/apidocs.go

View workflow job for this annotation

GitHub Actions / lint

entities declared and not used) (typecheck)

Check failure on line 252 in internal/client/apidocs/apidocs.go

View workflow job for this annotation

GitHub Actions / lint

entities declared and not used) (typecheck)
if err != nil {
clilog.Error.Println("Error reading file: ", err)
return err
}
return fmt.Errorf("not implemented")
}

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

func readAPIDocsFile(filePath string) (d []data, err error) {
return nil, nil
}
3 changes: 2 additions & 1 deletion internal/client/sites/sites.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@
package sites

import (
"internal/apiclient"
"net/url"
"path"

"internal/apiclient"

"github.com/thedevsaddam/gojsonq"
)

Expand Down

0 comments on commit 3e04f1f

Please sign in to comment.