Пример #1
0
func (n *Node) Get() (err error) {
	if n.Id == "" {
		return errors.New("missing node Id")
	}
	url := conf.Server.Url + "/node/" + n.Id
	res, err := client.Get(url, client.Header{}, nil)
	if err == nil {
		if res.StatusCode == 200 {
			r := WNode{Data: n}
			body, _ := ioutil.ReadAll(res.Body)
			if err = json.Unmarshal(body, &r); err == nil {
				return err
			}
		} else {
			r := Wrapper{}
			body, _ := ioutil.ReadAll(res.Body)
			if err = json.Unmarshal(body, &r); err == nil {
				return errors.New(res.Status + ": " + (*r.Error)[0])
			} else {
				return errors.New("request error: " + res.Status)
			}
		}
	}
	return
}
Пример #2
0
func (n *Node) Download(opts Opts) (download io.Reader, err error) {
	if n.Id == "" {
		return nil, errors.New("missing node Id")
	}
	url := conf.Server.Url + "/node/" + n.Id + "?download"
	if opts.HasKey("index") {
		url += "&index=" + opts.Value("index")
		if opts.HasKey("parts") {
			url += "&part=" + opts.Value("parts")
		} else {
			return nil, errors.New("missing index parameter: parts")
		}
		if opts.HasKey("index_options") {
			// index options should be in form key=value
			url += "&" + opts.Value("index_options")
		}
	}

	res, err := client.Get(url, client.Header{}, nil)
	if err == nil {
		if res.StatusCode == 200 {
			return res.Body, nil
		} else {
			r := Wrapper{}
			body, _ := ioutil.ReadAll(res.Body)
			if err = json.Unmarshal(body, &r); err == nil {
				return nil, errors.New(res.Status + ": " + (*r.Error)[0])
			} else {
				return nil, errors.New("request error: " + res.Status)
			}
		}
	}
	return nil, err
}
Пример #3
0
func OAuthToken(username, password string) (t *Token, err error) {
	client.SetBasicAuth(username, password)
	res, err := client.Get(conf.Auth.TokenUrl, client.Header{}, nil)
	if err == nil {
		if res.StatusCode == http.StatusCreated {
			if body, err := ioutil.ReadAll(res.Body); err == nil {
				if err = json.Unmarshal(body, &t); err != nil {
					return nil, err
				}
			}
		} else {
			return nil, errors.New("Authentication failed: Unexpected response status: " + res.Status)
		}
	} else {
		return nil, err
	}
	return
}