123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- package lmaxapi
- import (
- "crypto/tls"
- "io/ioutil"
- "tickserver/api/lmaxapi/response"
- "net"
- "net/http"
- "os"
- "strings"
- "time"
- )
- var lmax_url string
- //MaxIdleConnsPerHost 最多允许10个闲置连接
- //这个数目用来应付频繁的下单过程
- //trade.lmax.com 证书错误,要忽略这个错误,才能交易
- var timeout = time.Duration(5 * time.Second)
- func dialTimeout(network, addr string) (net.Conn, error) {
- return net.DialTimeout(network, addr, timeout)
- }
- var lmax_client = http.Client{
- Transport: &http.Transport{
- Dial: dialTimeout,
- MaxIdleConnsPerHost: 10,
- TLSClientConfig: &tls.Config{InsecureSkipVerify: true}}}
- func SetBaseUrl(url string) {
- lmax_url = url
- }
- func httpPost(url string, sessionId string, req IRequest) (*http.Response, error) {
- // tracelog.Println("[POST]", req.Url(), req.GetRequestData().String(), "\n")
- creq, _ := http.NewRequest("POST", url+req.Url(), req.GetRequestData())
- return httpAction(creq, sessionId)
- }
- func enableLog(filename string) (*os.File, error) {
- logw, e := os.Create(filename)
- if e != nil {
- return nil, e
- }
- EnableLog(logw)
- return logw, nil
- }
- func httpGet(url string, sessionId string, req IRequest) (*http.Response, error) {
- // tracelog.Println("[GET]", req.Url(), "\n")
- creq, _ := http.NewRequest("GET", url+req.Url(), nil)
- return httpAction(creq, sessionId)
- }
- func httpAction(creq *http.Request, sessionId string) (*http.Response, error) {
- creq.Header.Add("Content-type", "text/xml")
- creq.Header.Add("Accept", "text/xml")
- creq.Header.Add("Content-Type", "text/xml; UTF-8")
- if sessionId != "" {
- creq.Header.Add("Cookie", sessionId)
- }
- res, err := lmax_client.Do(creq)
- if err != nil {
- return res, err
- }
- return res, nil
- }
- func httpOpen(url string, sessionId string) (*http.Response, error) {
- creq, _ := http.NewRequest("GET", url, nil)
- creq.Header.Add("Accept", "*/*")
- if sessionId != "" {
- creq.Header.Add("Cookie", sessionId)
- }
- res, err := lmax_client.Do(creq)
- if err != nil {
- return res, err
- }
- return res, nil
- }
- func Login(req IRequest) (*Session, error) {
- res, err := httpPost(lmax_url, "", req)
- if err != nil {
- code := 0
- if res != nil {
- code = res.StatusCode
- }
- return nil, NewOpError("Login", err, code, true)
- }
- defer res.Body.Close()
- buf, err1 := ioutil.ReadAll(res.Body)
- if err1 != nil {
- return nil, NewOpError("Login", err1, res.StatusCode, true)
- }
- bodyData := string(buf)
- if statusErr := parseOKStatusString(bodyData, "Login", res.StatusCode); statusErr != nil {
- return nil, statusErr
- }
- cookies := res.Cookies()
- var sid string = ""
- for _, v := range cookies {
- sid += v.Name + "=" + v.Value + "; "
- }
- sid = strings.TrimRight(sid, "; ")
- account := response.NewAccountDetails(bodyData)
- //fmt.Println(account.Username)
- return NewSession(lmax_url, sid, account), nil
- }
- //注册新的账户,任何人都可以注册,没有权限的限制
- func Register(req IRequest) error {
- res, err := httpPost(lmax_url, "", req)
- if err != nil {
- code := 0
- if res != nil {
- code = res.StatusCode
- }
- return NewOpError("Register", err, code, true)
- }
- defer res.Body.Close()
- buf, err1 := ioutil.ReadAll(res.Body)
- if err1 != nil {
- return NewOpError("Register", err1, res.StatusCode, true)
- }
- bodyData := string(buf)
- if statusErr := parseOKStatusString(bodyData, "Register", res.StatusCode); statusErr != nil {
- return statusErr
- }
- return nil
- }
|