Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(worker): use internal helper client to GET pypi package data #44

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions internal/helper/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,31 @@ import (
)

type Client struct {
HTTP *http.Client
HTTP *http.Client
BaseURL string
}

// NewClient
// todo: complete proper client settings
func NewClient() *Client {
func NewClient(baseURL string) *Client {
return &Client{
HTTP: &http.Client{
Timeout: time.Second * 5,
},
BaseURL: baseURL,
}
}

// Get makes a GET request to the specified url and returns the response.
func (c *Client) Get(url string) (*http.Response, error) {
r, err := c.HTTP.Get(url)
if err != nil {
return nil, err
}

return r, nil
}

// IsValidURL ...
func (c *Client) ParseURL(uri string) *url.URL {
u, err := url.Parse(uri)
Expand Down
35 changes: 10 additions & 25 deletions pip/worker/pypi.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@ package worker

import (
"encoding/json"
"errors"
"io"
"net/http"
"reflect"
"strings"

"github.com/opensbom-generator/parsers/internal/helper"
"github.com/opensbom-generator/parsers/meta"
"github.com/pkg/errors"
)

var (
errorPypiCouldNotFetchPkgData = errors.New("could not fetch package data from PyPI")
const (
pypiBaseURL = "https://pypi.org"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sometimes, organizations mirror pypi.org internally (for example: https://www.jfrog.com/confluence/display/JFROG/PyPI+Repositories). Perhaps there is a way to configure this in a file and read that information in?

errPypiCouldNotFetchPkgData = "could not fetch package data from PyPI"
)

type PypiPackageData struct {
Expand Down Expand Up @@ -82,31 +83,15 @@ var HashAlgoPickOrder []meta.HashAlgorithm = []meta.HashAlgorithm{
meta.HashAlgoMD2,
}

func makeGetRequest(packageJSONURL string) (*http.Response, error) {
url := "https://" + packageJSONURL

request, _ := http.NewRequest("GET", url, nil)
request.Header.Set("Accept", "application/json")

client := &http.Client{}
response, err := client.Do(request)
if err != nil {
return nil, err
}

if response.StatusCode != http.StatusOK {
return nil, errorPypiCouldNotFetchPkgData
}

return response, err
}

func GetPackageDataFromPyPi(packageJSONURL string) (PypiPackageData, error) {
packageJSONURL = strings.ReplaceAll(packageJSONURL, "pypi.org", "")
packageInfo := PypiPackageData{}

response, err := makeGetRequest(packageJSONURL)
client := helper.NewClient(pypiBaseURL)

response, err := client.Get(packageJSONURL)
if err != nil {
return packageInfo, err
return packageInfo, errors.Wrap(err, errPypiCouldNotFetchPkgData)
}
defer response.Body.Close()

Expand Down