Skip to content

Commit

Permalink
bug: fixes the json format of update apidocs
Browse files Browse the repository at this point in the history
  • Loading branch information
srinandan committed Nov 27, 2023
1 parent 5ca61a2 commit cf9f72b
Showing 1 changed file with 23 additions and 21 deletions.
44 changes: 23 additions & 21 deletions internal/client/apidocs/apidocs.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package apidocs

import (
"encoding/json"
"net/url"
"path"
"strings"
Expand All @@ -40,15 +39,15 @@ func Create(siteid string, title string, description string, published string,
}

// Update
func Update(siteid string, name string, title string, description string, published string,
func Update(siteid string, id string, title string, description string, published string,
anonAllowed string, apiProductName string, requireCallbackUrl string, imageUrl string,
categoryIds []string,
) (respBody []byte, err error) {
return createOrUpdate(siteid, name, title, description, published, anonAllowed,
return createOrUpdate(siteid, id, title, description, published, anonAllowed,
apiProductName, requireCallbackUrl, imageUrl, categoryIds, UPDATE)
}

func createOrUpdate(siteid string, name string, title string, description string, published string,
func createOrUpdate(siteid string, id string, title string, description string, published string,
anonAllowed string, apiProductName string, requireCallbackUrl string, imageUrl string,
categoryIds []string, action Action,
) (respBody []byte, err error) {
Expand Down Expand Up @@ -81,12 +80,7 @@ func createOrUpdate(siteid string, name string, title string, description string
}

if len(categoryIds) > 0 {
c, err := json.Marshal(categoryIds)
if err != nil {
return nil, err
}
categories := string(c)
apidoc = append(apidoc, "\"categoryIds\":"+categories)
apidoc = append(apidoc, "\"categoryIds\":[\""+getArrayStr(categoryIds)+"\"]")
}

payload := "{" + strings.Join(apidoc, ",") + "}"
Expand All @@ -95,25 +89,25 @@ func createOrUpdate(siteid string, name string, title string, description string
u.Path = path.Join(u.Path, apiclient.GetApigeeOrg(), "sites", siteid, "apidocs")
respBody, err = apiclient.HttpClient(u.String(), payload)
} else {
u.Path = path.Join(u.Path, apiclient.GetApigeeOrg(), "sites", siteid, "apidocs", name)
u.Path = path.Join(u.Path, apiclient.GetApigeeOrg(), "sites", siteid, "apidocs", id)
respBody, err = apiclient.HttpClient(u.String(), payload, "PUT")
}

return respBody, err
}

// GetDocumentation
func GetDocumentation(siteid string, name string) (respBody []byte, err error) {
func GetDocumentation(siteid string, id string) (respBody []byte, err error) {
u, _ := url.Parse(apiclient.BaseURL)
u.Path = path.Join(u.Path, apiclient.GetApigeeOrg(), "sites", siteid, "apidocs", name, "documentation")
u.Path = path.Join(u.Path, apiclient.GetApigeeOrg(), "sites", siteid, "apidocs", id, "documentation")
respBody, err = apiclient.HttpClient(u.String())
return respBody, err
}

// Get
func Get(siteid string, name string) (respBody []byte, err error) {
func Get(siteid string, id string) (respBody []byte, err error) {
u, _ := url.Parse(apiclient.BaseURL)
u.Path = path.Join(u.Path, apiclient.GetApigeeOrg(), "sites", siteid, "apidocs", name)
u.Path = path.Join(u.Path, apiclient.GetApigeeOrg(), "sites", siteid, "apidocs", id)
respBody, err = apiclient.HttpClient(u.String())
return respBody, err
}
Expand All @@ -127,28 +121,36 @@ func List(siteid string) (respBody []byte, err error) {
}

// Delete
func Delete(siteid string, name string) (respBody []byte, err error) {
func Delete(siteid string, id string) (respBody []byte, err error) {
u, _ := url.Parse(apiclient.BaseURL)
u.Path = path.Join(u.Path, apiclient.GetApigeeOrg(), "sites", siteid, "apidocs", name)
u.Path = path.Join(u.Path, apiclient.GetApigeeOrg(), "sites", siteid, "apidocs", id)
respBody, err = apiclient.HttpClient(u.String(), "", "DELETE")
return respBody, err
}

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

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

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

u, _ := url.Parse(apiclient.BaseURL)
u.Path = path.Join(u.Path, apiclient.GetApigeeOrg(), "sites", siteid, "apidocs", name, "documentation")
u.Path = path.Join(u.Path, apiclient.GetApigeeOrg(), "sites", siteid, "apidocs", id, "documentation")
respBody, err = apiclient.HttpClient(u.String(), payload, "PATCH")

return nil, nil
}

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

0 comments on commit cf9f72b

Please sign in to comment.