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

feat(go): add create asset upload api #14

Merged
merged 1 commit into from
Mar 4, 2024
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
60 changes: 55 additions & 5 deletions go/cms.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ type Interface interface {
Asset(ctx context.Context, id string) (*Asset, error)
UploadAsset(ctx context.Context, projectID, url string) (string, error)
UploadAssetDirectly(ctx context.Context, projectID, name string, data io.Reader) (string, error)
CreateAssetByToken(ctx context.Context, projectID, token string) (*Asset, error)
CreateAssetUpload(ctx context.Context, projectID, name string) (*AssetUpload, error)
CommentToItem(ctx context.Context, assetID, content string) error
CommentToAsset(ctx context.Context, assetID, content string) error
}
Expand Down Expand Up @@ -450,11 +452,7 @@ func (c *CMS) UploadAsset(ctx context.Context, projectID, url string) (string, e
return "", fmt.Errorf("failed to read body: %w", err2)
}

type res struct {
ID string `json:"id"`
}

r := &res{}
r := &Asset{}
if err2 := json.Unmarshal(body, &r); err2 != nil {
return "", fmt.Errorf("failed to parse body: %w", err2)
}
Expand Down Expand Up @@ -505,6 +503,58 @@ func (c *CMS) UploadAssetDirectly(ctx context.Context, projectID, name string, d
return r.ID, nil
}

func (c *CMS) CreateAssetByToken(ctx context.Context, projectID, token string) (*Asset, error) {
rb := map[string]string{
"token": token,
}

b, err2 := c.send(ctx, http.MethodPost, []string{"api", "projects", projectID, "assets"}, "", rb)
if err2 != nil {
return nil, fmt.Errorf("failed to upload an asset: %w", err2)
}

defer func() { _ = b.Close() }()

body, err2 := io.ReadAll(b)
if err2 != nil {
return nil, fmt.Errorf("failed to read body: %w", err2)
}

r := &Asset{}
if err2 := json.Unmarshal(body, &r); err2 != nil {
return nil, fmt.Errorf("failed to parse body: %w", err2)
}

return r, nil
}

func (c *CMS) CreateAssetUpload(ctx context.Context, projectID, name string) (*AssetUpload, error) {
b, err2 := c.send(
ctx,
http.MethodPost,
[]string{"api", "projects", projectID, "assets", "uploads"},
"",
map[string]string{"name": name},
)
if err2 != nil {
return nil, fmt.Errorf("failed to upload an asset: %w", err2)
}

defer func() { _ = b.Close() }()

body, err2 := io.ReadAll(b)
if err2 != nil {
return nil, fmt.Errorf("failed to read body: %w", err2)
}

r := &AssetUpload{}
if err2 := json.Unmarshal(body, &r); err2 != nil {
return nil, fmt.Errorf("failed to parse body: %w", err2)
}

return r, nil
}

func (c *CMS) Asset(ctx context.Context, assetID string) (*Asset, error) {
b, err := c.send(ctx, http.MethodGet, []string{"api", "assets", assetID}, "", nil)
if err != nil {
Expand Down
6 changes: 6 additions & 0 deletions go/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ type Asset struct {
File *File `json:"file,omitempty"`
}

type AssetUpload struct {
URL string `json:"url"`
Token string `json:"token"`
ContentType string `json:"contentType"`
}

type File struct {
Name string `json:"name"`
Size int `json:"size"`
Expand Down
Loading