7fde8b908a
* Light client implementation with get-info and list-buckets commands * Fix client package name * Fix go.mod to work with vgo * Use single `fmt.Printf` for `get-info` output * Use unnamed import to client package * Simplify usage of sha256 and sha512 sums * Remove obsolete test code * Use helper structs for unmarshalling bridge info * Remove LGPL license files and adjust copyright headers * Use github.com/zeebo/errs * Use httptest for test http server * Use viper for env var management * Nested struct for swagger * Add github.com/zeebo/errs to go.mod * More bucket tests * word wrap long line * Use zeebo/errs for crypto errors
58 lines
1.2 KiB
Go
58 lines
1.2 KiB
Go
// Copyright (C) 2018 Storj Labs, Inc.
|
|
// See LICENSE for copying information.
|
|
|
|
package client
|
|
|
|
import (
|
|
"encoding/json"
|
|
"io/ioutil"
|
|
"log"
|
|
"net/http"
|
|
"strconv"
|
|
)
|
|
|
|
// Bucket metdata
|
|
type Bucket struct {
|
|
ID string
|
|
Name string
|
|
Created string
|
|
Decrypted bool
|
|
}
|
|
|
|
// GetBuckets returns the list of buckets
|
|
func GetBuckets(env Env) ([]Bucket, error) {
|
|
client := &http.Client{}
|
|
req, err := http.NewRequest("GET", env.URL+"/buckets", nil)
|
|
if err != nil {
|
|
return []Bucket{}, err
|
|
}
|
|
req.SetBasicAuth(env.User, env.Password)
|
|
resp, err := client.Do(req)
|
|
if err != nil {
|
|
return []Bucket{}, err
|
|
}
|
|
defer resp.Body.Close()
|
|
if resp.StatusCode != http.StatusOK {
|
|
return []Bucket{}, UnexpectedStatusCode.New(strconv.Itoa(resp.StatusCode))
|
|
}
|
|
b, err := ioutil.ReadAll(resp.Body)
|
|
if err != nil {
|
|
return []Bucket{}, err
|
|
}
|
|
var buckets []Bucket
|
|
err = json.Unmarshal(b, &buckets)
|
|
if err != nil {
|
|
return buckets, err
|
|
}
|
|
for i, b := range buckets {
|
|
decryptedName, err := decryptBucketName(b.Name, env.Mnemonic)
|
|
if err != nil {
|
|
log.Printf("Could not decrypt bucket name %s: %s\n", b.Name, err)
|
|
continue
|
|
}
|
|
buckets[i].Name = decryptedName
|
|
buckets[i].Decrypted = true
|
|
}
|
|
return buckets, err
|
|
}
|