storj/pkg/client/info.go
Kaloyan Raev 7fde8b908a
Light client implementation with get-info and list-buckets commands (#2)
* 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
2018-04-16 16:42:06 +03:00

59 lines
1.2 KiB
Go

// Copyright (C) 2018 Storj Labs, Inc.
// See LICENSE for copying information.
package client
import (
"encoding/json"
"io/ioutil"
"net/http"
"strconv"
)
// Info struct of the GetInfo() response
type Info struct {
Title string
Description string
Version string
Host string
}
type swagger struct {
Info struct {
Title string
Description string
Version string
}
Host string
}
// UnmarshalJSON overrides the unmarshalling for Info to correctly extract the data from the Swagger JSON response
func (info *Info) UnmarshalJSON(b []byte) error {
var s swagger
json.Unmarshal(b, &s)
info.Title = s.Info.Title
info.Description = s.Info.Description
info.Version = s.Info.Version
info.Host = s.Host
return nil
}
// GetInfo returns info about the Storj Bridge server
func GetInfo(env Env) (Info, error) {
info := Info{}
resp, err := http.Get(env.URL)
if err != nil {
return info, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return info, UnexpectedStatusCode.New(strconv.Itoa(resp.StatusCode))
}
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return info, err
}
err = json.Unmarshal(b, &info)
return info, err
}