Example #1
0
func parseTwitterResponse(resp *httpclient.Response, out interface{}) error {
	if !resp.IsOK() {
		var message string
		var code int
		var errs twitterErrors
		if resp.DecodeJSON(&errs) == nil && len(errs.Errors) > 0 {
			message = errs.Errors[0].Message
			code = errs.Errors[0].Code
		}
		return &TwitterError{
			Message:    message,
			Code:       code,
			StatusCode: resp.StatusCode,
		}
	}
	if err := resp.DecodeJSON(out); err != nil {
		return err
	}
	return nil
}
Example #2
0
func parseToken(resp *httpclient.Response) (*Token, error) {
	defer resp.Close()
	b, err := resp.ReadAll()
	if err != nil {
		return nil, err
	}
	s := string(b)
	if !resp.IsOK() {
		return nil, fmt.Errorf("oAuth service returned non-200 status code %d: %s", resp.StatusCode, s)
	}
	values, err := url.ParseQuery(s)
	if err != nil {
		return nil, err
	}
	key := values.Get("oauth_token")
	secret := values.Get("oauth_token_secret")
	if key == "" || secret == "" {
		return nil, fmt.Errorf("can't parse token from %q", s)
	}
	return &Token{
		Key:    key,
		Secret: secret,
	}, nil
}
Example #3
0
func (c *Client) responseHasError(r *httpclient.Response) bool {
	if c.ResponseHasError != nil {
		return c.ResponseHasError(r)
	}
	return !r.IsOK()
}