Skip to content

Commit

Permalink
added test data, and uploading functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
jmtx1020 committed Apr 20, 2024
1 parent 788184a commit 01a72fb
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 0 deletions.
68 changes: 68 additions & 0 deletions api/ipfs/pinning/pinning.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"io"
"mime/multipart"
"net/http"
"strings"

Expand Down Expand Up @@ -64,6 +65,19 @@ func (o *Origins) UnmarshalJSON(data []byte) error {
return nil
}

type UploadObjectResponse struct {
RequestID string `json:"requestId"`
Status string `json:"status"`
Created string `json:"created"`
Pin PinnedObjectPayload `json:"pin"`
Info UploadObjectResponseInfo `json:"info"`
Delegates []string `json:"delegates"`
}

type UploadObjectResponseInfo struct {
Size string `json:"size"`
}

type PinningAPI struct {
API *client.APIWrapper
}
Expand Down Expand Up @@ -282,3 +296,57 @@ func (p *PinningAPI) DeletePinnedObject(requestID string) (bool, error) {

return status, nil
}

func (p *PinningAPI) UploadObject(body []byte, key, contentType string) (UploadObjectResponse, error) {
p.API.SetBaseURL("https://api.quicknode.com/ipfs/rest/v1/s3/put-object")
endpoint := p.API.BaseURL

requestBody := bytes.Buffer{}
writer := multipart.NewWriter(&requestBody)

part, err := writer.CreateFormFile("Body", "file")
if err != nil {
return UploadObjectResponse{}, err
}
_, err = part.Write(body)
if err != nil {
return UploadObjectResponse{}, err
}

// Add other form fields
_ = writer.WriteField("Key", key)
_ = writer.WriteField("ContentType", contentType)

err = writer.Close()
if err != nil {
return UploadObjectResponse{}, err
}

req, err := http.NewRequest("POST", endpoint, &requestBody)
if err != nil {
return UploadObjectResponse{}, err
}
req.Header.Set("Content-Type", writer.FormDataContentType())

resp, err := p.API.Client.Do(req)
if err != nil {
return UploadObjectResponse{}, err
}
defer resp.Body.Close()

responseBody, err := io.ReadAll(resp.Body)
if err != nil {
return UploadObjectResponse{}, err
}

if resp.StatusCode < 200 || resp.StatusCode >= 300 {
return UploadObjectResponse{}, fmt.Errorf("failed to upload object: %s", responseBody)
}

var uploadObjectResponse UploadObjectResponse
err = json.Unmarshal(responseBody, &uploadObjectResponse)
if err != nil {
return UploadObjectResponse{}, err
}
return uploadObjectResponse, nil
}
17 changes: 17 additions & 0 deletions api/ipfs/pinning/pinning_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,23 @@ func TestDeletePinnedObject(t *testing.T) {
}
}

func TestUploadObject(t *testing.T) {
apiToken := os.Getenv("QUICKNODE_API_TOKEN")
apiWrapper := client.NewAPIWrapper(apiToken, "https://api.quicknode.com/ipfs/rest/v1/s3/put-object")

pinningAPI := &PinningAPI{API: apiWrapper}

fileContent, err := os.ReadFile("test_data/grumpy.jpg")
if err != nil {
t.Errorf("Unexpected error: %v", err)
}

_, err = pinningAPI.UploadObject(fileContent, "grumpy.jpg", "image/jpeg")
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
}

func randomString(length int) string {
b := make([]byte, length)
for i := range b {
Expand Down
Binary file added api/ipfs/pinning/test_data/grumpy.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 01a72fb

Please sign in to comment.