| package swwebservice |
| |
| func dailTimeout(network, addr string) (net.Conn, error) { |
| default_timeout := time.Duration(theSession.DefaultTimeout) * time.Second |
| return net.DialTimeout(network, addr, default_timeout) |
| } |
| |
| type WebSession struct { |
| AppId string |
| AppSecret string |
| BaseUrl string |
| DefaultTimeout int |
| session_key string |
| } |
| |
| func (w *WebSession) DoGet(uri string, params map[string]string) (*http.Response, error) { |
| transport := http.Transport{Dial: dailTimeout} |
| |
| client := http.Client{Transport: &transport} |
| |
| full_url := w.BaseUrl + uri |
| |
| vl := url.Values{} |
| vl.Add("app_id", w.AppId) |
| ts := w.GetTimestamp() |
| vl.Add("timestamp", ts) |
| |
| if params != nil { |
| for k, v := range params { |
| vl.Add(k, v) |
| } |
| } |
| vl.Add("sign", w.Sign(ts, vl.Encode())) |
| full_url = full_url + "?" + vl.Encode() |
| return client.Get(full_url) |
| } |
| |
| func (w *WebSession) GetTimestamp() string { |
| t := time.Now() |
| return fmt.Sprintf("%04d%02d%02d%02d%02d%02d", t.Year(), t.Month(), t.Day(), |
| t.Hour(), t.Minute(), t.Second()) |
| } |
| |
| func (w *WebSession) Sign(timestamp, query_string string) string { |
| return "122123123" |
| } |
| |
| func (w *WebSession) DoPost(uri string, param map[string]string, json_data []byte) (*http.Response, error) { |
| transport := http.Transport{Dial: dailTimeout} |
| client := &http.Client{Transport: &transport} |
| vl := url.Values{} |
| vl.Add("app_id", w.AppId) |
| ts := w.GetTimestamp() |
| vl.Add("timestamp", ts) |
| vl.Add("sign", w.Sign(ts, vl.Encode())) |
| if param != nil { |
| for k, v := range param { |
| vl.Add(k, v) |
| } |
| } |
| if json_data != nil { |
| vl.Add("request_data", string(json_data)) |
| } |
| full_url := w.BaseUrl + uri |
| log.Debugf("Url=%v", full_url) |
| r, err := client.Post(full_url, "application/x-www-form-urlencoded", bytes.NewReader([]byte(vl.Encode()))) |
| if err != nil || r.StatusCode != 200 { |
| log.Errorf("Status=%v, err=%v", r, err) |
| } |
| return r, err |
| } |
| |
| func (w *WebSession) Auth() error { |
| return nil |
| } |