2023-04-03 18:27:15 +00:00
|
|
|
|
package penadisk
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bytes"
|
2023-08-15 21:33:53 +00:00
|
|
|
|
"context"
|
2023-04-03 18:27:15 +00:00
|
|
|
|
"encoding/json"
|
2023-08-15 21:33:53 +00:00
|
|
|
|
"fmt"
|
2023-04-03 18:27:15 +00:00
|
|
|
|
"io"
|
|
|
|
|
"mime/multipart"
|
|
|
|
|
"net/http"
|
|
|
|
|
"net/url"
|
|
|
|
|
"os"
|
|
|
|
|
"strconv"
|
|
|
|
|
"strings"
|
2023-08-15 21:33:53 +00:00
|
|
|
|
|
|
|
|
|
"github.com/minio/minio-go/v7"
|
2023-04-03 18:27:15 +00:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const (
|
2023-08-15 21:33:53 +00:00
|
|
|
|
DefaultFolder = "templategen"
|
|
|
|
|
DefaultTemplateFolder = DefaultFolder + "/templates"
|
|
|
|
|
DefaultSaveFolder = DefaultFolder + "/saved"
|
2023-04-03 18:27:15 +00:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var (
|
2023-08-15 21:33:53 +00:00
|
|
|
|
URL = ""
|
|
|
|
|
V1DiskAPI = URL + "/api/v1/"
|
2023-04-03 18:27:15 +00:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
|
URL = os.Getenv("PENADISK_URL")
|
2023-08-15 21:33:53 +00:00
|
|
|
|
V1DiskAPI = URL + "api/v1/"
|
2023-04-03 18:27:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type Client struct {
|
2023-08-15 21:33:53 +00:00
|
|
|
|
/* ID пользователя пены */
|
2023-04-03 18:27:15 +00:00
|
|
|
|
userID string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewClient(userID string) *Client {
|
|
|
|
|
return &Client{userID: userID}
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-15 21:33:53 +00:00
|
|
|
|
func (c *Client) GetDisk(ctx context.Context) (*BucketWithStats, error) {
|
|
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, V1DiskAPI+"bucket", http.NoBody)
|
2023-04-03 18:27:15 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
c.setHeaders(req)
|
|
|
|
|
|
|
|
|
|
resp, err := http.DefaultClient.Do(req)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err = checkError(resp); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var r BucketWithStats
|
|
|
|
|
err = json.NewDecoder(resp.Body).Decode(&r)
|
|
|
|
|
|
|
|
|
|
return &r, err
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-15 21:33:53 +00:00
|
|
|
|
func (c *Client) SetBucketQuota(ctx context.Context, size uint64) error {
|
2023-04-03 18:27:15 +00:00
|
|
|
|
data := url.Values{}
|
|
|
|
|
|
|
|
|
|
data.Set("size", strconv.FormatUint(size, 10))
|
2023-08-15 21:33:53 +00:00
|
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodPut, V1DiskAPI+"bucket/quota/set?"+data.Encode(), http.NoBody)
|
2023-04-03 18:27:15 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
c.setHeaders(req)
|
|
|
|
|
|
|
|
|
|
resp, err := http.DefaultClient.Do(req)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return checkError(resp)
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-15 21:33:53 +00:00
|
|
|
|
func (c *Client) UnsetBucketQuota(ctx context.Context) error {
|
|
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodPut, V1DiskAPI+"bucket/quota/unset", http.NoBody)
|
2023-04-03 18:27:15 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
c.setHeaders(req)
|
|
|
|
|
|
|
|
|
|
resp, err := http.DefaultClient.Do(req)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return checkError(resp)
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-15 21:33:53 +00:00
|
|
|
|
func (c *Client) EnableBucketVersioning(ctx context.Context) error {
|
|
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodPut, V1DiskAPI+"bucket/versioning/enable", http.NoBody)
|
2023-04-03 18:27:15 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
c.setHeaders(req)
|
|
|
|
|
|
|
|
|
|
resp, err := http.DefaultClient.Do(req)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return checkError(resp)
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-15 21:33:53 +00:00
|
|
|
|
func (c *Client) SuspendBucketVersioning(ctx context.Context) error {
|
|
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodPut, V1DiskAPI+"bucket/versioning/suspend", http.NoBody)
|
2023-04-03 18:27:15 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
c.setHeaders(req)
|
|
|
|
|
|
|
|
|
|
resp, err := http.DefaultClient.Do(req)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return checkError(resp)
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-15 21:33:53 +00:00
|
|
|
|
// GetResources - получить информацию о ресурсах.
|
|
|
|
|
func (c *Client) GetResources(ctx context.Context, path string, recursive, withVersion bool) ([]minio.ObjectInfo, error) {
|
2023-04-03 18:27:15 +00:00
|
|
|
|
data := url.Values{}
|
|
|
|
|
data.Set("path", path)
|
|
|
|
|
|
|
|
|
|
if recursive {
|
|
|
|
|
data.Set("recursive", "true")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if withVersion {
|
|
|
|
|
data.Set("with_versions", "true")
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-15 21:33:53 +00:00
|
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, V1DiskAPI+"resources?"+data.Encode(), http.NoBody)
|
2023-04-03 18:27:15 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
c.setHeaders(req)
|
|
|
|
|
|
|
|
|
|
resp, err := http.DefaultClient.Do(req)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err = checkError(resp); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var r []minio.ObjectInfo
|
|
|
|
|
err = json.NewDecoder(resp.Body).Decode(&r)
|
|
|
|
|
|
|
|
|
|
return r, err
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-15 21:33:53 +00:00
|
|
|
|
// PutResources - создать папку.
|
|
|
|
|
func (c *Client) PutResources(ctx context.Context, path string) error {
|
2023-04-03 18:27:15 +00:00
|
|
|
|
data := url.Values{}
|
|
|
|
|
data.Set("path", path)
|
|
|
|
|
|
2023-08-15 21:33:53 +00:00
|
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodPut, V1DiskAPI+"resources?"+data.Encode(), http.NoBody)
|
2023-04-03 18:27:15 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
c.setHeaders(req)
|
|
|
|
|
|
|
|
|
|
resp, err := http.DefaultClient.Do(req)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return checkError(resp)
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-15 21:33:53 +00:00
|
|
|
|
func (c *Client) UploadResources(ctx context.Context, path, filename string, overwrite bool,
|
|
|
|
|
file io.Reader) (fullPath string, exportURL string, err error) {
|
2023-04-03 18:27:15 +00:00
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
|
bw := multipart.NewWriter(buf)
|
|
|
|
|
|
|
|
|
|
if !strings.HasSuffix(path, "/") {
|
|
|
|
|
path += "/"
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-15 21:33:53 +00:00
|
|
|
|
err = bw.WriteField("path", path)
|
|
|
|
|
if err != nil {
|
2023-04-03 18:27:15 +00:00
|
|
|
|
return "", "", err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if overwrite {
|
2023-08-15 21:33:53 +00:00
|
|
|
|
err = bw.WriteField("overwrite", "true")
|
|
|
|
|
if err != nil {
|
2023-04-03 18:27:15 +00:00
|
|
|
|
return "", "", err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fw, err := bw.CreateFormFile("file", filename)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", "", err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_, err = io.Copy(fw, file)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", "", err
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-15 21:33:53 +00:00
|
|
|
|
err = bw.Close()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", "", err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodPost, V1DiskAPI+"resources/upload", buf)
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", "", err
|
|
|
|
|
}
|
2023-04-03 18:27:15 +00:00
|
|
|
|
|
|
|
|
|
c.setHeaders(req)
|
|
|
|
|
req.Header.Set("Content-Type", bw.FormDataContentType())
|
|
|
|
|
|
|
|
|
|
resp, err := http.DefaultClient.Do(req)
|
2023-08-15 21:33:53 +00:00
|
|
|
|
|
2023-04-03 18:27:15 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
return "", "", err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err = checkError(resp); err != nil {
|
|
|
|
|
return "", "", err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fullPath = path + filename
|
|
|
|
|
|
2023-08-15 21:33:53 +00:00
|
|
|
|
exportURL, err = c.GetPublicDownloadURL(ctx, fullPath)
|
2023-04-03 18:27:15 +00:00
|
|
|
|
|
2023-08-15 21:33:53 +00:00
|
|
|
|
return fullPath, exportURL, err
|
2023-04-03 18:27:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-08-15 21:33:53 +00:00
|
|
|
|
// GetPublicDownloadURL - получить публичную ссылку для скачивания файла. Ссылка живет 30 минут.
|
|
|
|
|
func (c *Client) GetPublicDownloadURL(ctx context.Context, path string) (href string, err error) {
|
2023-04-03 18:27:15 +00:00
|
|
|
|
data := url.Values{}
|
|
|
|
|
data.Set("path", path)
|
|
|
|
|
|
|
|
|
|
// Получаем публичную ссылку на файл живущую 30 минут
|
2023-08-15 21:33:53 +00:00
|
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, V1DiskAPI+"resources/download?"+data.Encode(), http.NoBody)
|
2023-04-03 18:27:15 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
c.setHeaders(req)
|
|
|
|
|
|
|
|
|
|
resp, err := http.DefaultClient.Do(req)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = checkError(resp)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var r RespDownloadResources
|
|
|
|
|
err = json.NewDecoder(resp.Body).Decode(&r)
|
|
|
|
|
|
|
|
|
|
return r.Href, err
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-15 21:33:53 +00:00
|
|
|
|
func (c *Client) DownloadResource(ctx context.Context, path, downloadPath string) error {
|
|
|
|
|
href, err := c.GetPublicDownloadURL(ctx, path)
|
2023-04-03 18:27:15 +00:00
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-15 21:33:53 +00:00
|
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, href, http.NoBody)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
resp, err := http.DefaultClient.Do(req)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-03 18:27:15 +00:00
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = checkError(resp)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err = checkError(resp); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Create the file
|
|
|
|
|
out, err := os.Create(downloadPath)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2023-08-15 21:33:53 +00:00
|
|
|
|
defer out.Close() //nolint
|
2023-04-03 18:27:15 +00:00
|
|
|
|
|
|
|
|
|
// Write the body to file
|
|
|
|
|
_, err = io.Copy(out, resp.Body)
|
|
|
|
|
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-15 21:33:53 +00:00
|
|
|
|
func (c *Client) DownloadResourceBytes(ctx context.Context, path string) ([]byte, error) {
|
|
|
|
|
href, err := c.GetPublicDownloadURL(ctx, path)
|
2023-04-03 18:27:15 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-15 21:33:53 +00:00
|
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, href, http.NoBody)
|
2023-04-03 18:27:15 +00:00
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-15 21:33:53 +00:00
|
|
|
|
resp, err := http.DefaultClient.Do(req)
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
2023-04-03 18:27:15 +00:00
|
|
|
|
if err = checkError(resp); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return io.ReadAll(resp.Body)
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-15 21:33:53 +00:00
|
|
|
|
func (c *Client) CopyResources(ctx context.Context, from, path string, overwrite bool) error {
|
2023-04-03 18:27:15 +00:00
|
|
|
|
data := url.Values{}
|
|
|
|
|
data.Set("from", from)
|
|
|
|
|
data.Set("path", path)
|
|
|
|
|
if overwrite {
|
|
|
|
|
data.Set("overwrite", "true")
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-15 21:33:53 +00:00
|
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodPut, V1DiskAPI+"resources/copy?"+data.Encode(), http.NoBody)
|
2023-04-03 18:27:15 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
c.setHeaders(req)
|
|
|
|
|
|
|
|
|
|
resp, err := http.DefaultClient.Do(req)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return checkError(resp)
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-15 21:33:53 +00:00
|
|
|
|
func (c *Client) MoveResources(ctx context.Context, from, path string, overwrite bool) error {
|
2023-04-03 18:27:15 +00:00
|
|
|
|
data := url.Values{}
|
|
|
|
|
data.Set("from", from)
|
|
|
|
|
data.Set("path", path)
|
|
|
|
|
if overwrite {
|
|
|
|
|
data.Set("overwrite", "true")
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-15 21:33:53 +00:00
|
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodPut, V1DiskAPI+"resources/move?"+data.Encode(), http.NoBody)
|
2023-04-03 18:27:15 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
c.setHeaders(req)
|
|
|
|
|
|
|
|
|
|
resp, err := http.DefaultClient.Do(req)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return checkError(resp)
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-15 21:33:53 +00:00
|
|
|
|
func (c *Client) DeleteResources(ctx context.Context, path string, permanently bool) error {
|
2023-04-03 18:27:15 +00:00
|
|
|
|
data := url.Values{}
|
|
|
|
|
data.Set("path", path)
|
|
|
|
|
if permanently {
|
|
|
|
|
data.Set("permanently", "true")
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-15 21:33:53 +00:00
|
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodDelete, V1DiskAPI+"resources?"+data.Encode(), http.NoBody)
|
2023-04-03 18:27:15 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
c.setHeaders(req)
|
|
|
|
|
|
|
|
|
|
resp, err := http.DefaultClient.Do(req)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return checkError(resp)
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-15 21:33:53 +00:00
|
|
|
|
func (c *Client) PublishResources(ctx context.Context, path string) (href string, err error) {
|
2023-04-03 18:27:15 +00:00
|
|
|
|
data := url.Values{}
|
|
|
|
|
data.Set("path", path)
|
|
|
|
|
|
2023-08-15 21:33:53 +00:00
|
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodPut, V1DiskAPI+"resources/publish?"+data.Encode(), http.NoBody)
|
2023-04-03 18:27:15 +00:00
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
c.setHeaders(req)
|
|
|
|
|
|
|
|
|
|
resp, err := http.DefaultClient.Do(req)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err = checkError(resp); err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var r RespPublishResources
|
|
|
|
|
err = json.NewDecoder(resp.Body).Decode(&r)
|
|
|
|
|
|
|
|
|
|
return r.Href, err
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-15 21:33:53 +00:00
|
|
|
|
func (c *Client) UnpublishResources(ctx context.Context, path string) error {
|
2023-04-03 18:27:15 +00:00
|
|
|
|
data := url.Values{}
|
|
|
|
|
data.Set("path", path)
|
|
|
|
|
|
2023-08-15 21:33:53 +00:00
|
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodPut, V1DiskAPI+"resources/unpublish?"+data.Encode(), http.NoBody)
|
2023-04-03 18:27:15 +00:00
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
c.setHeaders(req)
|
|
|
|
|
|
|
|
|
|
resp, err := http.DefaultClient.Do(req)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return checkError(resp)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *Client) setHeaders(req *http.Request) {
|
|
|
|
|
req.Header.Set("LocalUserID", c.userID)
|
|
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func checkError(resp *http.Response) error {
|
|
|
|
|
switch resp.StatusCode {
|
2023-08-15 21:33:53 +00:00
|
|
|
|
case http.StatusOK, http.StatusCreated, http.StatusAccepted, http.StatusNonAuthoritativeInfo,
|
|
|
|
|
http.StatusNoContent, http.StatusResetContent, http.StatusPartialContent,
|
|
|
|
|
http.StatusMultiStatus, http.StatusAlreadyReported, http.StatusIMUsed:
|
2023-04-03 18:27:15 +00:00
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
body, err := io.ReadAll(resp.Body)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-15 21:33:53 +00:00
|
|
|
|
return fmt.Errorf("api/penaDisk: %v | %v", resp.StatusCode, string(body))
|
2023-04-03 18:27:15 +00:00
|
|
|
|
}
|