package main

import (
    "bytes"
    "encoding/json"
    "errors"
    "log"
    "net/http"
    "time"
)

var httpClient *http.Client
var fakeClient = false

func httpPost(url string, data interface{}) error {
    log.Println("httpPost:url>", url)
    if fakeClient {
        log.Println("httpPost:data> fake client ")
        return nil
    }
    if bs, e := json.Marshal(data); nil != e {
        return e
    } else if resp, e := httpClient.Post(url, "application/json", bytes.NewBuffer(bs)); nil != e {
        return e
    } else if resp.StatusCode != http.StatusOK {
        return errors.New("invalid resp status: " + resp.Status)
    } else {
        return nil
    }
}

func httpGet(url string, resp interface{}) error {
    return nil
}

func httpPostEx(url string, data interface{}, retries int) {
    for i := 0; i < retries; i++ {
        if e := httpPost(url, data); nil != e {
            log.Println(">>>", e)
            time.Sleep(3 * time.Second)
        } else {
            return
        }
    }
}

func init() {
    httpClient = http.DefaultClient
}
