Skip to content

Commit

Permalink
INT - handle retries for spec and image content
Browse files Browse the repository at this point in the history
  • Loading branch information
jcollins-axway committed May 16, 2022
1 parent 8dcd5f3 commit 3707490
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 24 deletions.
56 changes: 32 additions & 24 deletions client/pkg/apigee/apicalls.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,40 +237,48 @@ func (a *ApigeeClient) GetProduct(productName string) (*models.ApiProduct, error

// GetImageWithURL - get the list of portals for the org
func (a *ApigeeClient) GetImageWithURL(imageURL, portalURL string) (string, string) {
// Get the portal
response, err := a.newRequest(http.MethodGet, fmt.Sprintf("%s%s", portalURL, imageURL)).Execute()
if err != nil {
return "", ""
}
retries := 2
for i := 0; i <= retries; i++ {
// Get the portal
response, err := a.newRequest(http.MethodGet, fmt.Sprintf("%s%s", portalURL, imageURL)).Execute()
if err != nil {
return "", ""
}

contentType := ""
if contentTypeArray, ok := response.Headers["Content-Type"]; ok {
contentType = contentTypeArray[0]
// assuming an octet stream type is actually an image/png
if contentType == "application/octet-stream" {
contentType = "image/png"
contentType := ""
if contentTypeArray, ok := response.Headers["Content-Type"]; ok {
contentType = contentTypeArray[0]
// assuming an octet stream type is actually an image/png
if contentType == "application/octet-stream" {
contentType = "image/png"
}
}
}

if response.Code != 200 || string(response.Body) == "" || contentType == "" {
return "", ""
}
if response.Code != 200 || string(response.Body) == "" || contentType == "" {
continue
}

return base64.StdEncoding.EncodeToString(response.Body), contentType
return base64.StdEncoding.EncodeToString(response.Body), contentType
}
return "", ""
}

// GetSpecContent - get the spec content for an api product
func (a *ApigeeClient) GetSpecContent(contentID string) []byte {
// Get the spec content file
response, err := a.newRequest(http.MethodGet, fmt.Sprintf(orgDataAPIURL+"/specs/doc/%s/content", a.cfg.Organization, contentID),
WithDefaultHeaders(),
).Execute()
retries := 2
for i := 0; i <= retries; i++ {
// Get the spec content file
response, err := a.newRequest(http.MethodGet, fmt.Sprintf(orgDataAPIURL+"/specs/doc/%s/content", a.cfg.Organization, contentID),
WithDefaultHeaders(),
).Execute()

if err != nil || response.Code != 200 {
continue
}

if err != nil || response.Code != 200 {
return []byte{}
return response.Body
}

return response.Body
return []byte{}
}

// GetRevisionSpec - gets the resource file of type openapi for the org, api, revision, and spec file specified
Expand Down
3 changes: 3 additions & 0 deletions discovery/pkg/apigee/portalapihandlerjob.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,9 @@ func (j *newPortalAPIHandler) buildServiceBody(newAPI *apigee.APIDocData, produc
imageContentType := ""
if newAPI.ImageURL != nil {
image, imageContentType = j.apigeeClient.GetImageWithURL(*newAPI.ImageURL, portal.CurrentURL)
if image == "" {
return nil, fmt.Errorf("could not retrive image, when it was set, for Product %s in Portal %s, skipping", newAPI.ProductName, newAPI.GetPortalTitle())
}
}

// create the service body to use for update or create
Expand Down

0 comments on commit 3707490

Please sign in to comment.