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

Introduce caching for GitHub access method and add tests for the downloader #57

Merged
merged 3 commits into from
Aug 19, 2022
Merged
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
9 changes: 9 additions & 0 deletions pkg/common/accessio/downloader/downloader.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package downloader

import "io"

// Downloader defines a downloader for various objects using a WriterAt to
// transfer data to.
type Downloader interface {
Download(w io.WriterAt) error
}
36 changes: 36 additions & 0 deletions pkg/common/accessio/downloader/http/downloader.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package http

import (
"bytes"
"fmt"
"io"
"net/http"
)

// Downloader simply uses the default HTTP client to download the contents of a URL.
type Downloader struct {
link string
}

func NewDownloader(link string) *Downloader {
return &Downloader{
link: link,
}
}

func (h *Downloader) Download(w io.WriterAt) error {
resp, err := http.Get(h.link)
if err != nil {
return fmt.Errorf("failed to get link: %w", err)
}
defer resp.Body.Close()
var blob []byte
buf := bytes.NewBuffer(blob)
if _, err := io.Copy(buf, resp.Body); err != nil {
return fmt.Errorf("failed to copy response body: %w", err)
}
if _, err := w.WriteAt(buf.Bytes(), 0); err != nil {
return fmt.Errorf("failed to WriteAt to the writer: %w", err)
}
return nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,14 @@ import (

const defaultRegion = "us-west-1"

// Downloader defines a downloader for AWS S3 objects.
type Downloader interface {
Download(w io.WriterAt) error
}

// S3Downloader is a downloader capable of downloading S3 Objects.
type S3Downloader struct {
// Downloader is a downloader capable of downloading S3 Objects.
type Downloader struct {
region, bucket, key, version string
creds *AWSCreds
}

func NewS3Downloader(region, bucket, key, version string, creds *AWSCreds) *S3Downloader {
return &S3Downloader{
func NewDownloader(region, bucket, key, version string, creds *AWSCreds) *Downloader {
return &Downloader{
region: region,
bucket: bucket,
key: key,
Expand All @@ -42,7 +37,7 @@ type AWSCreds struct {
SessionToken string
}

func (s *S3Downloader) Download(w io.WriterAt) error {
func (s *Downloader) Download(w io.WriterAt) error {
ctx := context.Background()
opts := []func(*config.LoadOptions) error{
config.WithRegion(s.region),
Expand Down Expand Up @@ -79,6 +74,7 @@ func (s *S3Downloader) Download(w io.WriterAt) error {
}

client := s3.NewFromConfig(cfg, func(o *s3.Options) {
// Pass in creds because of https://github.com/aws/aws-sdk-go-v2/issues/1797
o.Credentials = awsCred
o.Region = s.region
})
Expand Down
35 changes: 0 additions & 35 deletions pkg/contexts/ocm/accessmethods/github/downloader.go

This file was deleted.

Loading