Skip to content

Commit

Permalink
Fixed import, needed new to use doc ID and already encoded specs
Browse files Browse the repository at this point in the history
  • Loading branch information
kurtkanaskie committed Dec 12, 2023
1 parent 4ae1307 commit 5c673b5
Showing 1 changed file with 30 additions and 18 deletions.
48 changes: 30 additions & 18 deletions internal/client/apidocs/apidocs.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
package apidocs

import (
"encoding/base64"
// "encoding/base64"
"encoding/json"
"errors"
"fmt"
Expand Down Expand Up @@ -95,6 +95,14 @@ type data struct {
GraphqlSchemaDisplayName string `json:"graphqlSchemaDisplayName,omitempty"`
}

type apidocResponse 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"`
}

// Create
func Create(siteid string, title string, description string, published string,
anonAllowed string, apiProductName string, requireCallbackUrl string, imageUrl string,
Expand Down Expand Up @@ -211,17 +219,18 @@ func UpdateDocumentation(siteid string, id string, displayName string,

if openAPIDoc != "" {
payload = "{\"oasDocumentation\":{\"spec\":{\"displayName\":\"" +
displayName + "\",\"contents\":" + openAPIDoc + "}}}"
displayName + "\",\"contents\":\"" + openAPIDoc + "\"}}}"

Check failure

Code scanning / CodeQL

Potentially unsafe quoting Critical

If this
JSON value
contains a double quote, it could break out of the enclosing quotes.
}

if graphQLDoc != "" {
payload = "{\"graphqlDocumentation\":{\"endpointUri\":\"" + endpointUri +
"\",\"schema\":{\"displayName\":\"" + displayName +
"\",\"contents\":" + graphQLDoc + "}}}"
"\",\"contents\":\"" + graphQLDoc + "\"}}}"

Check failure

Code scanning / CodeQL

Potentially unsafe quoting Critical

If this
JSON value
contains a double quote, it could break out of the enclosing quotes.
}

u, _ := url.Parse(apiclient.BaseURL)
u.Path = path.Join(u.Path, apiclient.GetApigeeOrg(), "sites", siteid, "apidocs", id, "documentation")
fmt.Printf("Kurt: update %s\n%s\n", u.String(), payload)
respBody, err = apiclient.HttpClient(u.String(), payload, "PATCH")

return nil, nil
Expand Down Expand Up @@ -280,42 +289,45 @@ func Export(folder string) (err error) {

func Import(siteid string, folder string) (err error) {
var errs []string
var respBody []byte
docsList, err := readAPIDocsDataFile(path.Join(folder, "site_"+siteid+".json"))
if err != nil {
return err
}
for _, doc := range docsList {
// 1. create the apidoc object
_, err = Create(siteid, doc.Title, doc.Description, strconv.FormatBool(doc.Published),
respBody, err = Create(siteid, doc.Title, doc.Description, strconv.FormatBool(doc.Published),
strconv.FormatBool(doc.AnonAllowed), doc.ApiProductName,
strconv.FormatBool(doc.RequireCallbackUrl), doc.ImageUrl, doc.CategoryIDs)
if err != nil {
errs = append(errs, err.Error())
continue

}

// get the new doc.ID from the created apidoc
response := apidocResponse{}
err = json.Unmarshal(respBody, &response)
if err != nil {
return err
}

// 2. find the documentation associated with this site
documentationFileName := fmt.Sprintf("apidocs_%s_%s", siteid, doc.ID)
documentationFileName := path.Join(folder, "apidocs_"+siteid+"_"+doc.ID+".json")
apidocument, err := readAPIDocumentationFile(documentationFileName)
if err != nil {
errs = append(errs, err.Error())
continue
}
if apidocument.Data.GraphqlDocumentation != nil {
schema, err := base64.StdEncoding.DecodeString(apidocument.Data.GraphqlDocumentation.Schema.Contents)
if err != nil {
errs = append(errs, err.Error())
continue
}
_, err = UpdateDocumentation(siteid, doc.ID, apidocument.Data.OasDocumentation.Spec.DisplayName, "",
string(schema), apidocument.Data.GraphqlDocumentation.EndpointUri)
_, err = UpdateDocumentation(siteid, response.Data.ID,
apidocument.Data.GraphqlDocumentation.Schema.DisplayName, "",
apidocument.Data.GraphqlDocumentation.Schema.Contents,
apidocument.Data.GraphqlDocumentation.EndpointUri)
} else {
oasdoc, err := base64.StdEncoding.DecodeString(apidocument.Data.OasDocumentation.Spec.Contents)
if err != nil {
errs = append(errs, err.Error())
continue
}
_, err = UpdateDocumentation(siteid, doc.ID, apidocument.Data.OasDocumentation.Spec.DisplayName, string(oasdoc),
_, err = UpdateDocumentation(siteid, response.Data.ID,
apidocument.Data.OasDocumentation.Spec.DisplayName,
apidocument.Data.OasDocumentation.Spec.Contents,
"", "")
}
if err != nil {
Expand Down

0 comments on commit 5c673b5

Please sign in to comment.