-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4c177ae
commit 338e6bf
Showing
1 changed file
with
77 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package metadata | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"io" | ||
"mime/multipart" | ||
"net/http" | ||
"path/filepath" | ||
) | ||
|
||
type CollectionRest struct { | ||
httpClient *httpclient | ||
metadataURL string | ||
} | ||
|
||
// NewCollectionREST creates a new Sequence Metadata client instance for REST Collections endpoints. | ||
func NewCollectionREST(projectAccessKey string, options ...Options) CollectionRest { | ||
opts := Options{} | ||
if len(options) > 0 { | ||
opts = options[0] | ||
} | ||
|
||
client := &httpclient{ | ||
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 | ||
} | ||
|
||
return CollectionRest{ | ||
httpClient: client, | ||
metadataURL: metadataServiceURL, | ||
} | ||
} | ||
|
||
func (c *CollectionRest) AssetUpload(projectID, collectionID, tokenID, assetID string, fileContent io.Reader) (*http.Response, error) { | ||
body := &bytes.Buffer{} | ||
writer := multipart.NewWriter(body) | ||
|
||
fileWriter, err := writer.CreateFormFile("file", filepath.Base(assetID)) | ||
if err != nil { | ||
return nil, fmt.Errorf("create form file: %w", err) | ||
} | ||
|
||
_, err = io.Copy(fileWriter, fileContent) | ||
if err != nil { | ||
return nil, fmt.Errorf("copy bytes: %w", err) | ||
} | ||
|
||
writer.Close() | ||
|
||
req, err := http.NewRequest(http.MethodPut, c.metadataURL, 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) | ||
if err != nil { | ||
return nil, fmt.Errorf("do: %w", err) | ||
} | ||
|
||
return resp, nil | ||
} |