-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1443798
commit fb18c0b
Showing
1 changed file
with
72 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package client | ||
|
||
import ( | ||
"io" | ||
"net/http" | ||
|
||
"github.com/go-resty/resty/v2" | ||
) | ||
|
||
const ( | ||
pingPath = "/ping" | ||
uploadPath = "/upload" | ||
downloadPath = "/download" | ||
) | ||
|
||
// StorageClient is a client of storage service. | ||
type StorageClient struct { | ||
name string | ||
endpoint string | ||
username string | ||
password string | ||
} | ||
|
||
// NewStorageClient constructs a new storage client. | ||
func NewStorageClient(name, endpoint, username, password string) *StorageClient { | ||
return &StorageClient{name, endpoint, username, password} | ||
} | ||
|
||
// Ping pings storage server with basic auth. | ||
func (c *StorageClient) Ping() (*http.Response, error) { | ||
client := resty.New() | ||
|
||
resp, err := client.R().SetBasicAuth(c.username, c.password).Get(c.endpoint + pingPath) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return resp.RawResponse, nil | ||
} | ||
|
||
// Upload uploads a file to storage server using given io.Reader. | ||
func (c *StorageClient) Upload(file io.Reader, filename string) (*http.Response, error) { | ||
client := resty.New() | ||
|
||
resp, err := client.R(). | ||
SetFileReader("file", filename, file). | ||
SetFormData(map[string]string{ | ||
"filename": filename, | ||
}). | ||
SetBasicAuth(c.username, c.password). | ||
Post(c.endpoint + uploadPath) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return resp.RawResponse, nil | ||
} | ||
|
||
// Download downloads given filename from storage server. | ||
func (c *StorageClient) Download(filename string) (*http.Response, error) { | ||
client := resty.New() | ||
|
||
resp, err := client.R(). | ||
SetQueryParam(filename, filename). | ||
SetBasicAuth(c.username, c.password). | ||
Get(c.endpoint + pingPath) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return resp.RawResponse, nil | ||
} |