gpaste/client/client.go

240 lines
5.4 KiB
Go
Raw Normal View History

2022-01-20 16:50:56 +00:00
package client
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"mime/multipart"
"net/http"
2022-01-21 01:40:33 +00:00
"os"
"path/filepath"
2022-01-20 16:50:56 +00:00
"time"
"git.t-juice.club/torjus/gpaste/api"
"git.t-juice.club/torjus/gpaste/files"
"github.com/google/uuid"
2022-01-21 01:40:33 +00:00
"github.com/kirsle/configdir"
2022-01-20 16:50:56 +00:00
)
2022-01-24 19:25:52 +00:00
const defaultTimeout = 10 * time.Second
2022-01-20 16:50:56 +00:00
type Client struct {
2022-01-24 19:25:52 +00:00
BaseURL string `json:"baseUrl"`
AuthToken string `json:"authToken"`
2022-01-20 16:50:56 +00:00
httpClient http.Client
}
2022-01-21 01:40:33 +00:00
func (c *Client) WriteConfigToWriter(w io.Writer) error {
encoder := json.NewEncoder(w)
return encoder.Encode(c)
}
func (c *Client) WriteConfig() error {
dir := configdir.LocalConfig("gpaste")
// Ensure dir exists
err := os.MkdirAll(dir, os.ModePerm)
if err != nil {
return err
}
2022-01-24 19:25:52 +00:00
2022-01-21 01:40:33 +00:00
path := filepath.Join(dir, "client.json")
2022-01-24 19:25:52 +00:00
2022-01-21 01:40:33 +00:00
f, err := os.Create(path)
if err != nil {
return err
}
defer f.Close()
return c.WriteConfigToWriter(f)
}
func (c *Client) LoadConfig() error {
dir := configdir.LocalCache("gpaste")
path := filepath.Join(dir, "client.json")
2022-01-24 19:25:52 +00:00
2022-01-21 01:40:33 +00:00
f, err := os.Open(path)
if err != nil {
return err
}
defer f.Close()
return c.LoadConfigFromReader(f)
}
func (c *Client) LoadConfigFromReader(r io.Reader) error {
decoder := json.NewDecoder(r)
return decoder.Decode(c)
}
2022-01-20 16:50:56 +00:00
func (c *Client) Login(ctx context.Context, username, password string) error {
url := fmt.Sprintf("%s/api/login", c.BaseURL)
// TODO: Change timeout
2022-01-24 19:25:52 +00:00
ctx, cancel := context.WithTimeout(ctx, defaultTimeout)
2022-01-20 16:50:56 +00:00
defer cancel()
body := new(bytes.Buffer)
requestData := api.RequestAPILogin{
Username: username,
Password: password,
}
2022-01-24 19:25:52 +00:00
2022-01-20 16:50:56 +00:00
encoder := json.NewEncoder(body)
if err := encoder.Encode(&requestData); err != nil {
return fmt.Errorf("error encoding response: %w", err)
}
2022-01-24 19:25:52 +00:00
2022-01-20 16:50:56 +00:00
req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, body)
if err != nil {
return fmt.Errorf("error creating request: %w", err)
}
resp, err := c.httpClient.Do(req)
if err != nil {
return fmt.Errorf("unable to perform request: %s", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("got non-ok response from server: %s", resp.Status)
}
var responseData api.ResponseAPILogin
decoder := json.NewDecoder(resp.Body)
if err := decoder.Decode(&responseData); err != nil {
return fmt.Errorf("unable to parse response: %s", err)
}
2022-01-24 19:25:52 +00:00
2022-01-20 16:50:56 +00:00
c.AuthToken = responseData.Token
return nil
}
func (c *Client) UserCreate(ctx context.Context, username, password string) error {
url := fmt.Sprintf("%s/api/user", c.BaseURL)
body := new(bytes.Buffer)
requestData := &api.RequestAPIUserCreate{
Username: username,
Password: password,
}
2022-01-24 19:25:52 +00:00
2022-01-20 16:50:56 +00:00
encoder := json.NewEncoder(body)
if err := encoder.Encode(requestData); err != nil {
return fmt.Errorf("error encoding response: %w", err)
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, body)
if err != nil {
return fmt.Errorf("error creating request: %w", err)
}
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", c.AuthToken))
resp, err := c.httpClient.Do(req)
if err != nil {
return fmt.Errorf("unable to perform request: %s", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusAccepted {
return fmt.Errorf("got non-ok response from server: %s", resp.Status)
}
return nil
}
func (c *Client) Download(ctx context.Context, id string) (io.ReadCloser, error) {
url := fmt.Sprintf("%s/api/file/%s", c.BaseURL, id)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
return nil, fmt.Errorf("error creating request: %w", err)
}
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", c.AuthToken))
resp, err := c.httpClient.Do(req)
if err != nil {
return nil, fmt.Errorf("unable to perform request: %s", err)
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("got non-ok response from server: %s", resp.Status)
}
return resp.Body, nil
}
2022-01-24 18:15:43 +00:00
func (c *Client) Upload(ctx context.Context, files ...*files.File) (*api.ResponseAPIFilePost, error) {
2022-01-20 16:50:56 +00:00
url := fmt.Sprintf("%s/api/file", c.BaseURL)
client := &http.Client{}
// TODO: Change timeout
// TODO: Improve buffering
buf := &bytes.Buffer{}
mw := multipart.NewWriter(buf)
for _, file := range files {
fw, err := mw.CreateFormFile(uuid.Must(uuid.NewRandom()).String(), file.OriginalFilename)
if err != nil {
return nil, err
}
2022-01-24 19:25:52 +00:00
2022-01-20 16:50:56 +00:00
if _, err := io.Copy(fw, file.Body); err != nil {
return nil, err
}
2022-01-24 19:25:52 +00:00
2022-01-20 16:50:56 +00:00
file.Body.Close()
}
2022-01-24 19:25:52 +00:00
2022-01-20 16:50:56 +00:00
mw.Close()
2022-01-24 19:25:52 +00:00
2022-01-20 16:50:56 +00:00
req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, buf)
if err != nil {
return nil, err
}
2022-01-24 19:25:52 +00:00
2022-01-20 16:50:56 +00:00
req.Header.Add("Content-Type", mw.FormDataContentType())
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
2022-01-24 18:15:43 +00:00
var expectedResp *api.ResponseAPIFilePost
2022-01-20 16:50:56 +00:00
decoder := json.NewDecoder(resp.Body)
if err := decoder.Decode(&expectedResp); err != nil {
return nil, fmt.Errorf("error decoding response: %w", err)
}
return expectedResp, nil
}
2022-01-21 06:17:52 +00:00
func (c *Client) Delete(ctx context.Context, id string) error {
url := fmt.Sprintf("%s/api/file/%s", c.BaseURL, id)
req, err := http.NewRequestWithContext(ctx, http.MethodDelete, url, nil)
if err != nil {
return fmt.Errorf("error creating request: %w", err)
}
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", c.AuthToken))
2022-01-24 19:25:52 +00:00
2022-01-21 06:17:52 +00:00
resp, err := c.httpClient.Do(req)
if err != nil {
return fmt.Errorf("unable to perform request: %s", err)
}
2022-01-24 19:25:52 +00:00
defer resp.Body.Close()
2022-01-21 06:17:52 +00:00
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("got non-ok response from server: %s", resp.Status)
}
return nil
}