SubscriptionRequest.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. package request
  2. import (
  3. "bytes"
  4. "encoding/xml"
  5. )
  6. type suburl struct {
  7. url string
  8. }
  9. func (this *suburl) SetUrl(url string) {
  10. this.url = url
  11. }
  12. func (this *suburl) Url() string {
  13. if this.url == "" {
  14. return "/secure/subscribe"
  15. } else {
  16. return this.url
  17. }
  18. }
  19. type SubscriptionRequest struct {
  20. XMLName xml.Name `xml:"req" json:"-"`
  21. SubType string `xml:"body>subscription>type" json:"type"`
  22. suburl
  23. }
  24. func (this *SubscriptionRequest) GetRequestData() *bytes.Buffer {
  25. output, _ := xml.Marshal(this)
  26. return bytes.NewBuffer(output)
  27. }
  28. // ================ AccountSubscriptionRequest ================
  29. type AccountSubscriptionRequest struct {
  30. SubscriptionRequest
  31. }
  32. func NewAccountSubscriptionRequest() *AccountSubscriptionRequest {
  33. return &AccountSubscriptionRequest{SubscriptionRequest{SubType: "account"}}
  34. }
  35. // ================= HeartbeatSubscriptionRequest
  36. type HeartbeatSubscriptionRequest struct {
  37. SubscriptionRequest
  38. }
  39. func NewHeartbeatSubscriptionRequest() *HeartbeatSubscriptionRequest {
  40. return &HeartbeatSubscriptionRequest{SubscriptionRequest{SubType: "account"}}
  41. }
  42. // ================ ExecutionSubscriptionRequest ================
  43. type ExecutionSubscriptionRequest struct {
  44. SubscriptionRequest
  45. }
  46. func NewExecutionSubscriptionRequest() *ExecutionSubscriptionRequest {
  47. return &ExecutionSubscriptionRequest{SubscriptionRequest{SubType: "account"}}
  48. }
  49. //============== OrderSubscriptionRequest ================
  50. type OrderSubscriptionRequest struct {
  51. SubscriptionRequest
  52. }
  53. func NewOrderSubscriptionRequest() *OrderSubscriptionRequest {
  54. return &OrderSubscriptionRequest{SubscriptionRequest{SubType: "order"}}
  55. }
  56. //============== PositionSubscriptionRequest ================
  57. type PositionSubscriptionRequest struct {
  58. SubscriptionRequest
  59. }
  60. func NewPositionSubscriptionRequest() *OrderSubscriptionRequest {
  61. return &OrderSubscriptionRequest{SubscriptionRequest{SubType: "position"}}
  62. }
  63. //============= HistoricMarketDataSubscriptionRequest ==========
  64. type HistoricMarketDataSubscriptionRequest struct {
  65. SubscriptionRequest
  66. }
  67. func NewHistoricSubscriptionRequest() *HistoricMarketDataSubscriptionRequest {
  68. return &HistoricMarketDataSubscriptionRequest{SubscriptionRequest{SubType: "historicMarketData"}}
  69. }
  70. //============= OrderBookSubscriptionRequest ==================
  71. type OrderBookSubscriptionRequest struct {
  72. XMLName xml.Name `xml:"req" json:"-"`
  73. InstrumentId int64 `xml:"body>subscription>ob2" json:"instrumentId"`
  74. suburl
  75. }
  76. func (this *OrderBookSubscriptionRequest) GetRequestData() *bytes.Buffer {
  77. output, _ := xml.Marshal(this)
  78. return bytes.NewBuffer(output)
  79. }
  80. func NewOrderBookSubscriptionRequest(insId int64) *OrderBookSubscriptionRequest {
  81. return &OrderBookSubscriptionRequest{InstrumentId: insId}
  82. }
  83. //============ OrderBookStatusSubscriptionRequest ======
  84. type OrderBookStatusRequest struct {
  85. XMLName xml.Name `xml:"req" json:"-"`
  86. InstrumentId int64 `xml:"body>subscription>orderBookStatus" json:"instrumentId"`
  87. suburl
  88. }
  89. func (this *OrderBookStatusRequest) GetRequestData() *bytes.Buffer {
  90. output, _ := xml.Marshal(this)
  91. return bytes.NewBuffer(output)
  92. }
  93. func NewOrderBookStatusRequest(insId int64) *OrderBookStatusRequest {
  94. return &OrderBookStatusRequest{InstrumentId : insId}
  95. }
  96. //======= exchange rate ===========
  97. type ExchangeRateRequest struct {
  98. XMLName xml.Name `xml:"req" json:"-"`
  99. From string `xml:"body>subscription>exchangeRate>from" json:"from"`
  100. To string `xml:"body>subscription>exchangeRate>to" json:"to"`
  101. suburl
  102. }
  103. func (this *ExchangeRateRequest) GetRequestData() *bytes.Buffer {
  104. output, _ := xml.Marshal(this)
  105. return bytes.NewBuffer(output)
  106. }
  107. func NewExchangeRateRequest(from string, to string) *ExchangeRateRequest {
  108. return &ExchangeRateRequest{xml.Name{}, from, to, suburl{}}
  109. }
  110. //============= instrument info ============
  111. type InstrumentRequest struct {
  112. XMLName xml.Name `xml:"req" json:"-"`
  113. InstrumentId int64 `xml:"body>subscription>instrument" json:"instrumentId"`
  114. suburl
  115. }
  116. func NewInstrumentRequest(instrumentId int64) *InstrumentRequest {
  117. return &InstrumentRequest{xml.Name{}, instrumentId, suburl{}}
  118. }
  119. func (this *InstrumentRequest) GetRequestData() *bytes.Buffer {
  120. output, _ := xml.Marshal(this)
  121. return bytes.NewBuffer(output)
  122. }