Example #1
0
File: rest.go Project: Ulexus/ari
func (c *Client) checkNappingError(res *napping.Response, err error, errMsg errorResponse) (*napping.Response, error) {
	if err == nil {
		status := res.Status()
		if status > 299 {
			err := fmt.Errorf("Non-2XX returned by server (%s)", res.HttpResponse().Status)
			if errMsg.Message != "" {
				err = fmt.Errorf("%s: %s", err.Error(), errMsg.Message)
			}
			c.Log(fmt.Sprintf(" - %s", err.Error()))
			return res, err
		}
	}
	c.Log(" - Success")
	return res, err
}
Example #2
0
func checkResponseError(resp *napping.Response, err error) error {
	if err != nil {
		return errors.New("Could not send request to Farmer server. (" + err.Error() + ")")
	}

	if resp.Status() != 201 && resp.Status() != 200 && resp.Status() != 204 {
		return errors.New("Unexpected response from Farmer server. (" + resp.RawText() + ")")
	}

	return nil
}
Example #3
0
func PostMsg(url string, payload interface{}, result interface{}) (error, *napping.Response) {

	e := httperr{}
	var (
		err  error
		resp *napping.Response
	)

	err, auth := GetAuthHeader("POST", "/v1/sms/")
	if err != nil {
		return err, nil
	}
	if debug {
		fmt.Println("auth: ", auth)
	}
	headers.Add("Authorization", auth)
	headers.Add("Accept", "application/json")

	sessn = napping.Session{
		Client: &clnt,
		Log:    debug,
		Header: &headers,
	}

	//
	// Send request to server
	//

	resp, err = sessn.Post(url, &payload, &result, &e)

	if err != nil {
		return err, resp
	}
	if resp.Status() >= 300 {
		return errors.New(e.Message), resp
	} else {
		// all is good in the world
		return nil, resp
	}
}
Example #4
0
//Request represent a request to LB
func Request(method int, u string, body interface{}) (*Response, *errors.Error) {
	var (
		err error
		r   *napping.Response
	)
	glog.Infof("Contacting host: %s", u)
	if method != common.GET {
		data, err := json.Marshal(body)
		if err != nil {
			return nil, &errors.ErrorCodeBadRequestMarshal
		}
		glog.Infof("Contacting host: %s, JSON data %s", u, string(data))
	}
	switch method {
	case common.GET:
		{
			r, err = sess.Get(u, nil, body, nil)
		}
	case common.POST:
		{
			r, err = sess.Post(u, body, nil, nil)
		}
	case common.PUT:
		{
			r, err = sess.Put(u, body, nil, nil)
		}
	case common.PATCH:
		{
			r, err = sess.Patch(u, body, nil, nil)
		}
	case common.DELETE:
		{
			r, err = sess.Delete(u, nil, nil, nil)
		}
	}
	if err != nil {
		return nil, &errors.Error{Status: http.StatusBadRequest, Message: err.Error()}
	}
	return &Response{r.Status(), r.RawText()}, nil
}
Example #5
0
func SendRequest(u string, method int, sess *napping.Session, pload interface{}, res interface{}) (error, *napping.Response) {

	//
	// Send request to server
	//
	e := httperr{}
	var (
		err  error
		resp *napping.Response
	)
	sess.Log = debug

	switch method {
	case GET:
		resp, err = sess.Get(u, nil, &res, &e)
	case POST:
		resp, err = sess.Post(u, &pload, &res, &e)
	case PUT:
		resp, err = sess.Put(u, &pload, &res, &e)
	case PATCH:
		resp, err = sess.Patch(u, &pload, &res, &e)
	case DELETE:
		resp, err = sess.Delete(u, &res, &e)
	}

	if err != nil {
		return err, resp
	}
	if resp.Status() == 401 {
		return errors.New("unauthorised - check your username and passwd"), resp
	}
	if resp.Status() >= 300 {
		return errors.New(e.Message), resp
	} else {
		// all is good in the world
		return nil, resp
	}
}
Example #6
0
func (f *Device) SendRequest(u string, method int, pload interface{}, res interface{}) (error, *Response) {

	//
	// Send request to server
	//
	e := httperr{}
	var (
		err   error
		nresp *napping.Response
	)
	f.Session.Log = debug

	switch method {
	case GET:
		nresp, err = f.Session.Get(u, nil, &res, &e)
	case POST:
		nresp, err = f.Session.Post(u, &pload, &res, &e)
	case PUT:
		nresp, err = f.Session.Put(u, &pload, &res, &e)
	case PATCH:
		nresp, err = f.Session.Patch(u, &pload, &res, &e)
	case DELETE:
		nresp, err = f.Session.Delete(u, nil, &res, &e)
	}

	var resp = Response{Status: nresp.Status(), Message: e.Message}
	if err != nil {
		return err, &resp
	}
	if nresp.Status() == 401 {
		return errors.New("unauthorised - check your username and passwd"), &resp
	}
	if nresp.Status() >= 300 {
		return errors.New(e.Message), &resp
	} else {
		// all is good in the world
		return nil, &resp
	}
}