IRequest.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. package lmaxapi
  2. import (
  3. "bytes"
  4. "tickserver/api/lmaxapi/request"
  5. )
  6. type IRequest interface {
  7. Url() string
  8. GetRequestData() *bytes.Buffer
  9. }
  10. type UrlSetter interface {
  11. SetUrl(url string)
  12. }
  13. //============= multi sub ==============//
  14. type MultiSubscribe struct {
  15. req []IRequest
  16. url string
  17. }
  18. func NewMultiSubscribe() *MultiSubscribe {
  19. return &MultiSubscribe{}
  20. }
  21. func (multi *MultiSubscribe) Add(req IRequest) {
  22. multi.req = append(multi.req, req)
  23. }
  24. func (multi *MultiSubscribe) SetUrl(url string) {
  25. multi.url = url
  26. }
  27. func (multi *MultiSubscribe) Url() string {
  28. if multi.url == "" {
  29. return "/secure/subscribe"
  30. }
  31. return multi.url
  32. }
  33. func (multi *MultiSubscribe) GetRequestData() *bytes.Buffer {
  34. buf := bytes.NewBufferString("")
  35. buf.WriteString("<req><body>")
  36. for i := 0; i < len(multi.req); i++ {
  37. data2 := multi.req[i].GetRequestData().Bytes()
  38. data2 = data2[len("<req><body>") : len(data2)-len("</body></req>")]
  39. buf.Write(data2)
  40. }
  41. buf.WriteString("</body></req>")
  42. return buf
  43. }
  44. type OrderActionsRequest struct {
  45. req []IRequest
  46. url string
  47. }
  48. func NewOrderActionsRequest() *OrderActionsRequest {
  49. actions := &OrderActionsRequest{}
  50. return actions
  51. }
  52. func (this *OrderActionsRequest) Url() string {
  53. return "/secure/trade/orderActions"
  54. }
  55. func (this *OrderActionsRequest) Add(req IRequest) {
  56. this.req = append(this.req, req)
  57. }
  58. func (this *OrderActionsRequest) GetRequestData() *bytes.Buffer {
  59. buf := bytes.NewBufferString("")
  60. buf.WriteString("<req><body>")
  61. for i := 0; i < len(this.req); i++ {
  62. data2 := this.req[i].GetRequestData().Bytes()
  63. data2 = data2[len("<req><body>") : len(data2)-len("</body></req>")]
  64. send := string(data2)
  65. switch this.req[i].(type) {
  66. case *request.AmendStopsOrderRequest:
  67. send = "<amend>" + send + "</amend>"
  68. case *request.CancelOrderRequest:
  69. send = "<cancel>" + send + "</cancel>"
  70. case *request.ClosingOrderRequest:
  71. send = "<close>" + send + "</close>"
  72. }
  73. buf.WriteString(send)
  74. }
  75. buf.WriteString("</body></req>")
  76. return buf
  77. }