package main

import (
	"encoding/json"
	"net/http"
	"strconv"
)

//WechatError when API call goes wrong, it shows error message
type WechatError struct {
	ErrCode int64  `json:"errcode"`
	ErrMsg  string `json:"errmsg"`
}

//getWechatAPIError  given an http body,  try to decode json error
//             caller needs to makesure content type is json
//return
//             the error strucutre, on success
//             err: if json decode encounters problem
func getWechatAPIError(respBody string) (result WechatError, err error) {
	err = json.Unmarshal([]byte(respBody), &result)
	return
}

//see if a http response is json response
func isJSON(r *http.Response) bool {
	contentType := r.Header.Get("Content-Type")
	contentLen := r.Header.Get("Content-Length")
	len, _ := strconv.Atoi(contentLen)

	if contentType == "text/plain" || contentType == "text/json" {
		if len < 10240 { //less than 10K
			return true
		}
	}
	return false
}

//ToDo: test error message @todo
