lmaxapi.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. package lmaxapi
  2. import (
  3. "crypto/tls"
  4. "io/ioutil"
  5. "tickserver/api/lmaxapi/response"
  6. "net"
  7. "net/http"
  8. "os"
  9. "strings"
  10. "time"
  11. )
  12. var lmax_url string
  13. //MaxIdleConnsPerHost 最多允许10个闲置连接
  14. //这个数目用来应付频繁的下单过程
  15. //trade.lmax.com 证书错误,要忽略这个错误,才能交易
  16. var timeout = time.Duration(5 * time.Second)
  17. func dialTimeout(network, addr string) (net.Conn, error) {
  18. return net.DialTimeout(network, addr, timeout)
  19. }
  20. var lmax_client = http.Client{
  21. Transport: &http.Transport{
  22. Dial: dialTimeout,
  23. MaxIdleConnsPerHost: 10,
  24. TLSClientConfig: &tls.Config{InsecureSkipVerify: true}}}
  25. func SetBaseUrl(url string) {
  26. lmax_url = url
  27. }
  28. func httpPost(url string, sessionId string, req IRequest) (*http.Response, error) {
  29. // tracelog.Println("[POST]", req.Url(), req.GetRequestData().String(), "\n")
  30. creq, _ := http.NewRequest("POST", url+req.Url(), req.GetRequestData())
  31. return httpAction(creq, sessionId)
  32. }
  33. func enableLog(filename string) (*os.File, error) {
  34. logw, e := os.Create(filename)
  35. if e != nil {
  36. return nil, e
  37. }
  38. EnableLog(logw)
  39. return logw, nil
  40. }
  41. func httpGet(url string, sessionId string, req IRequest) (*http.Response, error) {
  42. // tracelog.Println("[GET]", req.Url(), "\n")
  43. creq, _ := http.NewRequest("GET", url+req.Url(), nil)
  44. return httpAction(creq, sessionId)
  45. }
  46. func httpAction(creq *http.Request, sessionId string) (*http.Response, error) {
  47. creq.Header.Add("Content-type", "text/xml")
  48. creq.Header.Add("Accept", "text/xml")
  49. creq.Header.Add("Content-Type", "text/xml; UTF-8")
  50. if sessionId != "" {
  51. creq.Header.Add("Cookie", sessionId)
  52. }
  53. res, err := lmax_client.Do(creq)
  54. if err != nil {
  55. return res, err
  56. }
  57. return res, nil
  58. }
  59. func httpOpen(url string, sessionId string) (*http.Response, error) {
  60. creq, _ := http.NewRequest("GET", url, nil)
  61. creq.Header.Add("Accept", "*/*")
  62. if sessionId != "" {
  63. creq.Header.Add("Cookie", sessionId)
  64. }
  65. res, err := lmax_client.Do(creq)
  66. if err != nil {
  67. return res, err
  68. }
  69. return res, nil
  70. }
  71. func Login(req IRequest) (*Session, error) {
  72. res, err := httpPost(lmax_url, "", req)
  73. if err != nil {
  74. code := 0
  75. if res != nil {
  76. code = res.StatusCode
  77. }
  78. return nil, NewOpError("Login", err, code, true)
  79. }
  80. defer res.Body.Close()
  81. buf, err1 := ioutil.ReadAll(res.Body)
  82. if err1 != nil {
  83. return nil, NewOpError("Login", err1, res.StatusCode, true)
  84. }
  85. bodyData := string(buf)
  86. if statusErr := parseOKStatusString(bodyData, "Login", res.StatusCode); statusErr != nil {
  87. return nil, statusErr
  88. }
  89. cookies := res.Cookies()
  90. var sid string = ""
  91. for _, v := range cookies {
  92. sid += v.Name + "=" + v.Value + "; "
  93. }
  94. sid = strings.TrimRight(sid, "; ")
  95. account := response.NewAccountDetails(bodyData)
  96. //fmt.Println(account.Username)
  97. return NewSession(lmax_url, sid, account), nil
  98. }
  99. //注册新的账户,任何人都可以注册,没有权限的限制
  100. func Register(req IRequest) error {
  101. res, err := httpPost(lmax_url, "", req)
  102. if err != nil {
  103. code := 0
  104. if res != nil {
  105. code = res.StatusCode
  106. }
  107. return NewOpError("Register", err, code, true)
  108. }
  109. defer res.Body.Close()
  110. buf, err1 := ioutil.ReadAll(res.Body)
  111. if err1 != nil {
  112. return NewOpError("Register", err1, res.StatusCode, true)
  113. }
  114. bodyData := string(buf)
  115. if statusErr := parseOKStatusString(bodyData, "Register", res.StatusCode); statusErr != nil {
  116. return statusErr
  117. }
  118. return nil
  119. }