2021-01-10 19:52:54 +00:00
|
|
|
package remote
|
2021-01-08 22:43:03 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net/http"
|
2021-01-10 00:24:07 +00:00
|
|
|
"strings"
|
2021-01-08 22:43:03 +00:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Client interface {
|
2021-02-02 05:28:46 +00:00
|
|
|
GetBackupRemoteUploadURLs(ctx context.Context, backup string, size int64) (BackupRemoteUploadResponse, error)
|
2021-02-02 04:50:23 +00:00
|
|
|
GetInstallationScript(ctx context.Context, uuid string) (InstallationScript, error)
|
|
|
|
GetServerConfiguration(ctx context.Context, uuid string) (ServerConfigurationResponse, error)
|
|
|
|
GetServers(context context.Context, perPage int) ([]RawServerData, error)
|
2021-01-08 22:43:03 +00:00
|
|
|
SetArchiveStatus(ctx context.Context, uuid string, successful bool) error
|
2021-02-02 05:28:46 +00:00
|
|
|
SetBackupStatus(ctx context.Context, backup string, data BackupRequest) error
|
|
|
|
SendRestorationStatus(ctx context.Context, backup string, successful bool) error
|
2021-01-08 22:43:03 +00:00
|
|
|
SetInstallationStatus(ctx context.Context, uuid string, successful bool) error
|
|
|
|
SetTransferStatus(ctx context.Context, uuid string, successful bool) error
|
2021-02-02 04:59:17 +00:00
|
|
|
ValidateSftpCredentials(ctx context.Context, request SftpAuthRequest) (SftpAuthResponse, error)
|
2021-01-08 22:43:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type client struct {
|
|
|
|
httpClient *http.Client
|
|
|
|
baseUrl string
|
|
|
|
tokenId string
|
|
|
|
token string
|
|
|
|
retries int
|
|
|
|
}
|
|
|
|
|
|
|
|
type ClientOption func(c *client)
|
|
|
|
|
|
|
|
func CreateClient(base, tokenId, token string, opts ...ClientOption) Client {
|
|
|
|
httpClient := &http.Client{
|
|
|
|
Timeout: time.Second * 15,
|
|
|
|
}
|
2021-01-10 00:24:07 +00:00
|
|
|
base = strings.TrimSuffix(base, "/")
|
2021-01-08 22:43:03 +00:00
|
|
|
c := &client{
|
2021-01-10 00:24:07 +00:00
|
|
|
baseUrl: base + "/api/remote",
|
2021-01-08 22:43:03 +00:00
|
|
|
tokenId: tokenId,
|
|
|
|
token: token,
|
|
|
|
httpClient: httpClient,
|
|
|
|
retries: 3,
|
|
|
|
}
|
|
|
|
for _, o := range opts {
|
|
|
|
o(c)
|
|
|
|
}
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithTimeout(timeout time.Duration) ClientOption {
|
|
|
|
return func(c *client) {
|
|
|
|
c.httpClient.Timeout = timeout
|
|
|
|
}
|
|
|
|
}
|