Exemple #1
0
func decodeListing(resp *httpclient.Response) (*Listing, error) {
	var value map[string]interface{}
	err := resp.DecodeJSON(&value)
	if err != nil {
		return nil, err
	}
	data, ok := value["data"].(map[string]interface{})
	if !ok {
		return nil, ErrInvalidDataFormat
	}
	children, ok := data["children"].([]interface{})
	if !ok {
		return nil, ErrInvalidDataFormat
	}
	var stories []*Story
	for _, v := range children {
		story, err := decodeStory(v)
		if err != nil {
			return nil, err
		}
		stories = append(stories, story)
	}
	return &Listing{
		Stories: stories,
	}, nil
}
Exemple #2
0
func decodeResponseError(resp *httpclient.Response) error {
	c := &ErrorContainer{}
	if err := resp.DecodeJSON(&c); err != nil {
		return err
	}
	return fmt.Errorf("error from Facebook (type %v): %v", c.Error.Type, c.Error.Message)
}
Exemple #3
0
func googleError(r *httpclient.Response) error {
	data, _ := r.ReadAll()
	var e *Error
	if err := json.Unmarshal(data, &e); err == nil && e != nil && e.Message != "" {
		return e
	}
	return fmt.Errorf("invalid status code %d: %s", r.StatusCode, string(data))
}
Exemple #4
0
func (c *Client) decodeError(r *httpclient.Response) error {
	if c.DecodeError != nil {
		return c.DecodeError(r)
	}
	data, err := r.ReadAll()
	if err != nil {
		return fmt.Errorf("error reading response: %s", err)
	}
	return errors.New(string(data))
}
Exemple #5
0
func parseJson(resp *httpclient.Response) (map[string]interface{}, error) {
	var m map[string]interface{}
	if err := resp.DecodeJSON(&m); err != nil {
		return nil, err
	}
	if rresp, ok := m["resource_response"].(map[string]interface{}); ok {
		if err, ok := rresp["error"].(map[string]interface{}); ok {
			msg, _ := err["message"].(string)
			if msg == "" {
				msg, _ = err["code"].(string)
			}
			return nil, errors.New(msg)
		}
	}
	return m, nil
}
Exemple #6
0
func decodeArgs(resp *httpclient.Response) (map[string]string, error) {
	var m map[string]interface{}
	if err := resp.DecodeJSON(&m); err != nil {
		return nil, err
	}
	var args map[string]interface{}
	if strings.HasSuffix(resp.Request.URL.Path, "post") {
		args = m["form"].(map[string]interface{})
	} else {
		args = m["args"].(map[string]interface{})
	}
	values := make(map[string]string, len(args))
	for k, v := range args {
		values[k] = v.(string)
	}
	return values, nil
}
Exemple #7
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
}
Exemple #8
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
}
Exemple #9
0
func (c *Client) responseHasError(r *httpclient.Response) bool {
	if c.ResponseHasError != nil {
		return c.ResponseHasError(r)
	}
	return !r.IsOK()
}
Exemple #10
0
func decodeError(r *httpclient.Response) error {
	var m map[string]interface{}
	r.UnmarshalJSON(&m)
	message, _ := m["message"].(string)
	return errors.New(message)
}