Skip to content

Commit

Permalink
Merge pull request #505 from Jougan-0/handleQueryUrl
Browse files Browse the repository at this point in the history
handle query url more gracefully for multi word package name w/signoff
  • Loading branch information
Mohd Uzair authored Jun 12, 2024
2 parents 6995cc9 + 5594a61 commit 93397db
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
4 changes: 2 additions & 2 deletions generators/artifacthub/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ import (
"gopkg.in/yaml.v2"
)

const ArtifactHubAPIEndpint = "https://artifacthub.io/api/v1"
const ArtifactHubAPIEndpoint = "https://artifacthub.io/api/v1"
const ArtifactHubChartUrlFieldName = "content_url"
const AhHelmExporterEndpoint = ArtifactHubAPIEndpint + "/helm-exporter"
const AhHelmExporterEndpoint = ArtifactHubAPIEndpoint + "/helm-exporter"

// internal representation of artifacthub package
// it contains information we need to identify a package using ArtifactHub API
Expand Down
25 changes: 19 additions & 6 deletions generators/artifacthub/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"net/http"
"net/url"

"github.com/layer5io/meshkit/utils/manifests"
)
Expand All @@ -20,18 +21,30 @@ const AhTextSearchQueryFieldName = "ts_query_web"

func GetAhPackagesWithName(name string) ([]AhPackage, error) {
pkgs := make([]AhPackage, 0)
url := fmt.Sprintf("%s/packages/search?%s=%s&", ArtifactHubAPIEndpint, AhTextSearchQueryFieldName, name)
// add params

// Construct URL with encoded query parameters
baseURL, err := url.Parse(fmt.Sprintf("%s/packages/search", ArtifactHubAPIEndpoint))
if err != nil {
return nil, ErrGetAhPackage(err)
}

query := url.Values{}
query.Add(AhTextSearchQueryFieldName, name)

for key, val := range AhApiSearchParams {
url = fmt.Sprintf("%s%s=%s&", url, key, val)
query.Add(key, val)
}
// get packages
resp, err := http.Get(url)

baseURL.RawQuery = query.Encode()
finalURL := baseURL.String()

// Get packages
resp, err := http.Get(finalURL)
if err != nil {
return nil, ErrGetAhPackage(err)
}
if resp.StatusCode != 200 {
err = fmt.Errorf("status code %d for %s", resp.StatusCode, url)
err = fmt.Errorf("status code %d for %s", resp.StatusCode, finalURL)
return nil, ErrGetAhPackage(err)
}
defer resp.Body.Close()
Expand Down

0 comments on commit 93397db

Please sign in to comment.