Exemplo n.º 1
0
func encodeParams(params map[string]interface{}) (result string, err error) {
	if len(params) > 0 {
		values := url.Values{}
		for key, value := range params {
			values.Add(key, to.String(value))
		}
		result = values.Encode()
	}
	return
}
Exemplo n.º 2
0
func read_body(response *http.Response) (body string, err error) {
	var reader io.ReadCloser
	var contents []byte
	using_gzip := response.Header.Get("Content-Encoding")
	switch using_gzip {
	case "gzip":
		reader, err = gzip.NewReader(response.Body)
		if err != nil {
			return
		}
		defer reader.Close()
	default:
		reader = response.Body
	}

	contents, err = ioutil.ReadAll(reader)
	if err != nil {
		return
	}
	body = to.String(contents)
	return body, nil
}
Exemplo n.º 3
0
func encodeMultipart(params map[string]interface{}) (multipartContentType string, multipartData *bytes.Buffer, err error) {
	if len(params) > 0 {
		multipartData = new(bytes.Buffer)
		bufferWriter := multipart.NewWriter(multipartData) // type *bytes.Buffer
		defer bufferWriter.Close()
		for key, value := range params {
			switch value.(type) {
			case *os.File:
				var picdata io.Writer
				picdata, err = bufferWriter.CreateFormFile(key, value.(*os.File).Name())
				multipartContentType = bufferWriter.FormDataContentType()
				if err != nil {
					return "", nil, err
				}
				io.Copy(picdata, value.(*os.File))
			default:
				bufferWriter.WriteField(key, to.String(value))
			}
		}
		return
	}
	return
}
Exemplo n.º 4
0
func (http *HttpObject) call(uri string, params map[string]interface{}, result interface{}) (err error) {
	var body string
	var url = fmt.Sprintf("%s%s.json", http.client.api_url, uri)
	if http.client.is_expires() {
		err = &APIError{When: time.Now(), ErrorCode: 21327, Message: "expired_token"}
		return err
	}
	body, err = httpCall(url, http.method, http.client.access_token, params)
	if err != nil {
		return
	}
	if strings.Trim(body, " ") == "" {
		return errors.New("Nothing Return From Http Requests!")
	}
	jsonbody, err := simplejson.NewJson([]byte(body))
	if err != nil {
		return
	}
	_, ok := jsonbody.CheckGet("error_code")
	if ok {
		err = &APIError{When: time.Now(), ErrorCode: to.Int64(jsonbody.Get("error_code")), Message: to.String(jsonbody.Get("error"))}
		return
	}
	err = JSONParser(body, result)
	if err != nil {
		return
	}
	return nil
}