Example #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
}
Example #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("part") {
			url += "&part=" + opts.Value("part")
		} else {
			return nil, errors.New("missing index parameter: part")
		}
		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
}
Example #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
}