Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
pkieltyka committed Mar 13, 2024
1 parent 55b0b43 commit 27ea56e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 15 deletions.
35 changes: 21 additions & 14 deletions metadata/collections.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@ import (
"path/filepath"
)

type CollectionRest struct {
httpClient *httpclient
metadataURL string
type CollectionsService struct {
Collections
options Options
projectAccessKey string
httpclient HTTPClient
}

// NewCollectionREST creates a new Sequence Metadata client instance for REST Collections endpoints.
func NewCollectionREST(projectAccessKey string, options ...Options) CollectionRest {
// NewCollections creates a new Sequence Metadata Collections client instance. Please see
// https://sequence.build to get a `projectAccessKey` and service-level account JWTAuthToken.
func NewCollections(projectAccessKey string, options ...Options) CollectionsService {
opts := Options{}
if len(options) > 0 {
opts = options[0]
Expand All @@ -25,31 +28,35 @@ func NewCollectionREST(projectAccessKey string, options ...Options) CollectionRe
client: opts.HTTPClient,
projectAccessKey: projectAccessKey,
}

if opts.HTTPClient == nil {
client.client = http.DefaultClient
}

if opts.JWTAuthToken != "" {
client.jwtAuthHeader = fmt.Sprintf("BEARER %s", opts.JWTAuthToken)
}

metadataServiceURL := "https://metadata.sequence.app"
if opts.MetadataServiceURL != "" {
metadataServiceURL = opts.MetadataServiceURL
} else {
opts.MetadataServiceURL = metadataServiceURL
}

return CollectionRest{
httpClient: client,
metadataURL: metadataServiceURL,
serviceClient := NewCollectionsClient(metadataServiceURL, client)

return CollectionsService{
Collections: serviceClient,
options: opts,
projectAccessKey: projectAccessKey,
httpclient: client,
}
}

func (c *CollectionRest) AssetUpload(projectID, collectionID, tokenID, assetID string, fileContent io.Reader) (*http.Response, error) {
func (c *CollectionsService) UploadAsset(projectID, collectionID, tokenID, assetFilename string, fileContent io.Reader) (*http.Response, error) {
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)

fileWriter, err := writer.CreateFormFile("file", filepath.Base(assetID))
fileWriter, err := writer.CreateFormFile("file", filepath.Base(assetFilename))
if err != nil {
return nil, fmt.Errorf("create form file: %w", err)
}
Expand All @@ -61,15 +68,15 @@ func (c *CollectionRest) AssetUpload(projectID, collectionID, tokenID, assetID s

writer.Close()

endpointURL := fmt.Sprintf("%s/collections/%s/%s/%s/upload/%s", c.metadataURL, projectID, collectionID, tokenID, assetID)
endpointURL := fmt.Sprintf("%s/collections/%s/%s/%s/upload/%s", c.options.MetadataServiceURL, projectID, collectionID, tokenID, assetFilename)
req, err := http.NewRequest(http.MethodPut, endpointURL, body)
if err != nil {
return nil, fmt.Errorf("create request: %w", err)
}

req.Header.Set("Content-Type", writer.FormDataContentType())

resp, err := c.httpClient.client.Do(req)
resp, err := c.httpclient.Do(req)
if err != nil {
return nil, fmt.Errorf("do: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion metadata/metadata.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 27ea56e

Please sign in to comment.